Skip to content

An AI-powered search engine with a generative UI

feat: replace related questions LLM call with inline spec fences (#803)

Yoshiki Miura7883b842f93a
diff --git a/bun.lock b/bun.lock
index 15fd98a7..8263ecde 100644
--- a/bun.lock
+++ b/bun.lock
@@ -11,6 +11,8 @@
"@ai-sdk/openai": "^3.0.39",
"@ai-sdk/react": "^3.0.113",
"@aws-sdk/client-s3": "^3.850.0",
+ "@json-render/core": "^0.16.0",
+ "@json-render/react": "^0.16.0",
"@opentelemetry/api-logs": "0.57.2",
"@opentelemetry/instrumentation": "0.57.2",
"@opentelemetry/sdk-logs": "0.57.2",
@@ -70,7 +72,7 @@
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "zod": "^4.0.5",
+ "zod": "^4.3.6",
},
"devDependencies": {
"@tailwindcss/postcss": "^4.1.11",
@@ -413,6 +415,10 @@
"@jridgewell/trace-mapping": ["@jridgewell/[email protected]", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" } }, "sha512-uw6guiW/gcAGPDhLmd77/6lW8QLeiV5RUTsAX46Db6oLhGaVj4lhnPwb184s1bkc8kdVg/+h988dro8GRDpmYQ=="],
+ "@json-render/core": ["@json-render/[email protected]", "", { "dependencies": { "zod": "^4.3.6" } }, "sha512-qQp8BB/3pWYapTGXBDSBMXRCdrC05VJPLL3drXMPX/QbUB3nuvtyXUGmAZFUz8eLUy7JImODvb3GNIq38dGzhQ=="],
+
+ "@json-render/react": ["@json-render/[email protected]", "", { "dependencies": { "@json-render/core": "0.16.0" }, "peerDependencies": { "react": "^19.2.3" } }, "sha512-PJQjLftGYZ5LNlA1IFww+yNOAJZRW2feEcH/L10u/8M/b/zImr9wfqQVMDyC87myoEzhyucpkQ6joISeZdKxsw=="],
+
"@mermaid-js/parser": ["@mermaid-js/[email protected]", "", { "dependencies": { "langium": "^4.0.0" } }, "sha512-opmV19kN1JsK0T6HhhokHpcVkqKpF+x2pPDKKM2ThHtZAB5F4PROopk0amuVYK5qMrIA4erzpNm8gmPNJgMDxQ=="],
"@napi-rs/wasm-runtime": ["@napi-rs/[email protected]", "", { "dependencies": { "@emnapi/core": "^1.4.3", "@emnapi/runtime": "^1.4.3", "@tybys/wasm-util": "^0.10.0" } }, "sha512-ZVWUcfwY4E/yPitQJl481FjFo3K22D6qF0DuFH6Y/nbnE11GY5uguDxZMGXPQ8WQ0128MXQD7TnfHyK4oWoIJQ=="],
@@ -2217,7 +2223,7 @@
"yocto-queue": ["[email protected]", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
- "zod": ["[email protected]", "", {}, "sha512-nGFJTnJN6cM2v9kXL+SOBq3AtjQby3Mv5ySGFof5UGRHrRioSJ5iG680cYNjE/yWk671nROcpPj4hAS8nyLhSw=="],
+ "zod": ["[email protected]", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],
"zod-to-json-schema": ["[email protected]", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-h/z3PKvcTcTetyjl1fkj79MHNEjm+HpD6NXheWjzOekY7kV+lwDYnHw+ivHkijnCSMz1yJaWBD9vu/Fcmk+vEg=="],
@@ -2351,6 +2357,8 @@
"eslint-plugin-react/semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA=="],
+ "eslint-plugin-react-hooks/zod": ["[email protected]", "", {}, "sha512-nGFJTnJN6cM2v9kXL+SOBq3AtjQby3Mv5ySGFof5UGRHrRioSJ5iG680cYNjE/yWk671nROcpPj4hAS8nyLhSw=="],
+
"exa-js/dotenv": ["[email protected]", "", {}, "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ=="],
"exa-js/zod": ["[email protected]", "", {}, "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ=="],
diff --git a/components/__tests__/render-message.test.tsx b/components/__tests__/render-message.test.tsx
new file mode 100644
index 00000000..a9d0d845
--- /dev/null
+++ b/components/__tests__/render-message.test.tsx
@@ -0,0 +1,83 @@
+import React from 'react'
+
+import { render, screen } from '@testing-library/react'
+import { describe, expect, test, vi } from 'vitest'
+
+import type { UIMessage } from '@/lib/types/ai'
+
+import { RenderMessage } from '../render-message'
+
+vi.mock('../answer-section', () => ({
+ AnswerSection: ({ content }: { content: string }) => (
+ <div data-testid="answer-section">{content}</div>
+ )
+}))
+
+vi.mock('../research-process-section', () => ({
+ __esModule: true,
+ default: ({ parts }: { parts: Array<{ type: string }> }) => (
+ <div data-testid="research-process">
+ {parts.map(part => part.type).join(',')}
+ </div>
+ )
+}))
+
+vi.mock('../dynamic-tool-display', () => ({
+ DynamicToolDisplay: () => <div data-testid="dynamic-tool" />
+}))
+
+vi.mock('../user-file-section', () => ({
+ UserFileSection: () => <div data-testid="user-file" />
+}))
+
+vi.mock('../user-text-section', () => ({
+ UserTextSection: () => <div data-testid="user-text" />
+}))
+
+describe('RenderMessage', () => {
+ test('ignores empty text parts so research process is not split early', () => {
+ const message: UIMessage = {
+ id: 'assistant-msg',
+ role: 'assistant',
+ parts: [
+ { type: 'reasoning', text: 'First reasoning' } as any,
+ {
+ type: 'tool-search',
+ toolCallId: 'tool-1',
+ state: 'output-available',
+ input: {},
+ output: {}
+ } as any,
+ { type: 'text', text: '' } as any,
+ { type: 'reasoning', text: 'Second reasoning' } as any,
+ { type: 'text', text: 'Final answer' } as any
+ ]
+ } as UIMessage
+
+ const { container } = render(
+ <RenderMessage
+ message={message}
+ messageId={message.id}
+ getIsOpen={() => true}
+ onOpenChange={() => {}}
+ />
+ )
+
+ const processSections = screen.getAllByTestId('research-process')
+ expect(processSections).toHaveLength(1)
+ expect(processSections[0]).toHaveTextContent(
+ 'reasoning,tool-search,reasoning'
+ )
+
+ const answerSections = screen.getAllByTestId('answer-section')
+ expect(answerSections).toHaveLength(1)
+ expect(answerSections[0]).toHaveTextContent('Final answer')
+
+ const order = Array.from(
+ container.querySelectorAll(
+ '[data-testid="research-process"], [data-testid="answer-section"]'
+ )
+ ).map(node => node.getAttribute('data-testid'))
+ expect(order).toEqual(['research-process', 'answer-section'])
+ })
+})
diff --git a/components/__tests__/research-process-section.test.tsx b/components/__tests__/research-process-section.test.tsx
index dc9f8cc2..de6598ff 100644
--- a/components/__tests__/research-process-section.test.tsx
+++ b/components/__tests__/research-process-section.test.tsx
@@ -34,7 +34,6 @@ vi.mock('../tool-section', () => ({
describe('ResearchProcessSection', () => {
const mockGetIsOpen = vi.fn()
const mockOnOpenChange = vi.fn()
- const mockOnQuerySelect = vi.fn()
const mockAddToolResult = vi.fn()
beforeEach(() => {
@@ -61,7 +60,6 @@ describe('ResearchProcessSection', () => {
messageId="test-1"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -88,7 +86,6 @@ describe('ResearchProcessSection', () => {
messageId="test-2"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -118,7 +115,6 @@ describe('ResearchProcessSection', () => {
messageId="test-3"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -154,7 +150,6 @@ describe('ResearchProcessSection', () => {
messageId="test-4"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -200,7 +195,6 @@ describe('ResearchProcessSection', () => {
messageId="test-5"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -228,7 +222,6 @@ describe('ResearchProcessSection', () => {
messageId="test-6"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -249,7 +242,6 @@ describe('ResearchProcessSection', () => {
messageId="test-6"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
})
@@ -271,7 +263,6 @@ describe('ResearchProcessSection', () => {
messageId="test-7"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -306,7 +297,6 @@ describe('ResearchProcessSection', () => {
messageId="test-8"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -333,7 +323,6 @@ describe('ResearchProcessSection', () => {
messageId="test-9"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
@@ -360,7 +349,6 @@ describe('ResearchProcessSection', () => {
messageId="test-10"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
parts={overrideParts}
/>
)
@@ -371,7 +359,7 @@ describe('ResearchProcessSection', () => {
expect(screen.getByText('Override')).toBeInTheDocument()
})
- test('handles data parts correctly', () => {
+ test('ignores unknown part types', () => {
const parts: any[] = [{ type: 'data-test', data: 'test' }]
const message: UIMessage = {
@@ -386,13 +374,12 @@ describe('ResearchProcessSection', () => {
messageId="test-11"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
/>
)
- // Data parts should render the DataSection component
- // Check that the component renders (not null)
- expect(container.firstChild).toBeInTheDocument()
+ // Unknown part types render no meaningful content (wrapper may exist)
+ expect(screen.queryByTestId('reasoning-section')).not.toBeInTheDocument()
+ expect(screen.queryByTestId('tool-section')).not.toBeInTheDocument()
})
})
@@ -417,7 +404,6 @@ describe('ResearchProcessSection', () => {
messageId="test-12"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
status="streaming"
/>
)
@@ -445,7 +431,6 @@ describe('ResearchProcessSection', () => {
messageId="test-13"
getIsOpen={mockGetIsOpen}
onOpenChange={mockOnOpenChange}
- onQuerySelect={mockOnQuerySelect}
addToolResult={mockAddToolResult}
/>
)
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 9ced9fac..cc102879 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -22,7 +22,6 @@ interface ChatSection {
interface ChatMessagesProps {
sections: ChatSection[] // Changed from messages to sections
- onQuerySelect: (query: string) => void
status: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
chatId?: string
isGuest?: boolean
@@ -36,7 +35,6 @@ interface ChatMessagesProps {
export function ChatMessages({
sections,
- onQuerySelect,
status,
chatId,
isGuest = false,
@@ -56,12 +54,7 @@ export function ChatMessages({
const isMobile = useMediaQuery('(max-width: 767px)')
// Tool types definition - moved outside function for performance
- const toolTypes = [
- 'tool-search',
- 'tool-fetch',
- 'tool-askQuestion',
- 'tool-relatedQuestions'
- ]
+ const toolTypes = ['tool-search', 'tool-fetch', 'tool-askQuestion']
// Clear cache during streaming to ensure accurate tool counts
useEffect(() => {
@@ -194,7 +187,6 @@ export function ChatMessages({
getIsOpen(id, partType, hasNextPart, section.userMessage)
}
onOpenChange={handleOpenChange}
- onQuerySelect={onQuerySelect}
chatId={chatId}
isGuest={isGuest}
status={status}
@@ -221,7 +213,6 @@ export function ChatMessages({
getIsOpen(id, partType, hasNextPart, assistantMessage)
}
onOpenChange={handleOpenChange}
- onQuerySelect={onQuerySelect}
chatId={chatId}
isGuest={isGuest}
status={status}
diff --git a/components/chat.tsx b/components/chat.tsx
index 0570c5fb..d66f8c8f 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -7,6 +7,7 @@ import { useChat } from '@ai-sdk/react'
import { DefaultChatTransport } from 'ai'
import { toast } from 'sonner'
+import { ChatProvider } from '@/lib/contexts/chat-context'
import { generateId } from '@/lib/db/schema'
import { UploadedFile } from '@/lib/types'
import type { UIMessage } from '@/lib/types/ai'
@@ -300,13 +301,6 @@ export function Chat({
}
}, [sections, messages, chatId])
- const onQuerySelect = (query: string) => {
- sendMessage({
- role: 'user',
- parts: [{ type: 'text', text: query }]
- })
- }
-
const handleUpdateAndReloadMessage = async (
editedMessageId: string,
newContentText: string
@@ -418,111 +412,112 @@ export function Chat({
: { isDragging, handleDragOver, handleDragLeave, handleDrop }
return (
- <div
- className={cn(
- 'relative flex h-full min-w-0 flex-1 flex-col',
- messages.length === 0 ? 'items-center justify-center' : ''
- )}
- data-testid="full-chat"
- onDragOver={dragHandlers.handleDragOver}
- onDragLeave={dragHandlers.handleDragLeave}
- onDrop={dragHandlers.handleDrop}
- >
- <ChatMessages
- sections={sections}
- onQuerySelect={onQuerySelect}
- status={status}
- chatId={chatId}
- isGuest={isGuest}
- addToolResult={({
- toolCallId,
- result
- }: {
- toolCallId: string
- result: any
- }) => {
- // Find the tool name from the message parts
- let toolName = 'unknown'
-
- // Optimize by breaking early once found
- outerLoop: for (const message of messages) {
- if (!message.parts) continue
-
- for (const part of message.parts) {
- if (isToolCallPart(part) && part.toolCallId === toolCallId) {
- toolName = part.toolName
- break outerLoop
- } else if (
- isToolTypePart(part) &&
- part.toolCallId === toolCallId
- ) {
- toolName = part.type.substring(5) // Remove 'tool-' prefix
- break outerLoop
- } else if (
- isDynamicToolPart(part) &&
- part.toolCallId === toolCallId
- ) {
- toolName = part.toolName
- break outerLoop
+ <ChatProvider sendMessage={sendMessage}>
+ <div
+ className={cn(
+ 'relative flex h-full min-w-0 flex-1 flex-col',
+ messages.length === 0 ? 'items-center justify-center' : ''
+ )}
+ data-testid="full-chat"
+ onDragOver={dragHandlers.handleDragOver}
+ onDragLeave={dragHandlers.handleDragLeave}
+ onDrop={dragHandlers.handleDrop}
+ >
+ <ChatMessages
+ sections={sections}
+ status={status}
+ chatId={chatId}
+ isGuest={isGuest}
+ addToolResult={({
+ toolCallId,
+ result
+ }: {
+ toolCallId: string
+ result: any
+ }) => {
+ // Find the tool name from the message parts
+ let toolName = 'unknown'
+
+ // Optimize by breaking early once found
+ outerLoop: for (const message of messages) {
+ if (!message.parts) continue
+
+ for (const part of message.parts) {
+ if (isToolCallPart(part) && part.toolCallId === toolCallId) {
+ toolName = part.toolName
+ break outerLoop
+ } else if (
+ isToolTypePart(part) &&
+ part.toolCallId === toolCallId
+ ) {
+ toolName = part.type.substring(5) // Remove 'tool-' prefix
+ break outerLoop
+ } else if (
+ isDynamicToolPart(part) &&
+ part.toolCallId === toolCallId
+ ) {
+ toolName = part.toolName
+ break outerLoop
+ }
}
}
- }
- addToolResult({ tool: toolName, toolCallId, output: result })
- }}
- scrollContainerRef={scrollContainerRef}
- onUpdateMessage={handleUpdateAndReloadMessage}
- reload={handleReloadFrom}
- error={error}
- />
- <ChatPanel
- chatId={chatId}
- input={input}
- handleInputChange={handleInputChange}
- handleSubmit={onSubmit}
- status={status}
- messages={messages}
- setMessages={setMessages}
- stop={stop}
- query={query}
- append={(message: any) => {
- sendMessage(message)
- }}
- showScrollToBottomButton={!isAtBottom}
- uploadedFiles={uploadedFiles}
- setUploadedFiles={setUploadedFiles}
- scrollContainerRef={scrollContainerRef}
- onNewChat={handleNewChat}
- isGuest={isGuest}
- isCloudDeployment={isCloudDeployment}
- modelSelectorData={modelSelectorData}
- />
- <DragOverlay visible={dragHandlers.isDragging} />
- <ErrorModal
- open={errorModal.open}
- onOpenChange={open => setErrorModal(prev => ({ ...prev, open }))}
- error={errorModal}
- onRetry={
- errorModal.type !== 'rate-limit'
- ? () => {
- // Retry the last message if not rate limited
- if (messages.length > 0) {
- const lastUserMessage = messages
- .filter(m => m.role === 'user')
- .pop()
- if (lastUserMessage) {
- sendMessage(lastUserMessage)
+ addToolResult({ tool: toolName, toolCallId, output: result })
+ }}
+ scrollContainerRef={scrollContainerRef}
+ onUpdateMessage={handleUpdateAndReloadMessage}
+ reload={handleReloadFrom}
+ error={error}
+ />
+ <ChatPanel
+ chatId={chatId}
+ input={input}
+ handleInputChange={handleInputChange}
+ handleSubmit={onSubmit}
+ status={status}
+ messages={messages}
+ setMessages={setMessages}
+ stop={stop}
+ query={query}
+ append={(message: any) => {
+ sendMessage(message)
+ }}
+ showScrollToBottomButton={!isAtBottom}
+ uploadedFiles={uploadedFiles}
+ setUploadedFiles={setUploadedFiles}
+ scrollContainerRef={scrollContainerRef}
+ onNewChat={handleNewChat}
+ isGuest={isGuest}
+ isCloudDeployment={isCloudDeployment}
+ modelSelectorData={modelSelectorData}
+ />
+ <DragOverlay visible={dragHandlers.isDragging} />
+ <ErrorModal
+ open={errorModal.open}
+ onOpenChange={open => setErrorModal(prev => ({ ...prev, open }))}
+ error={errorModal}
+ onRetry={
+ errorModal.type !== 'rate-limit'
+ ? () => {
+ // Retry the last message if not rate limited
+ if (messages.length > 0) {
+ const lastUserMessage = messages
+ .filter(m => m.role === 'user')
+ .pop()
+ if (lastUserMessage) {
+ sendMessage(lastUserMessage)
+ }
}
}
- }
- : undefined
- }
- onAuthClose={() => {
- // Clear messages and navigate to root
- setMessages([])
- router.push('/')
- }}
- />
- </div>
+ : undefined
+ }
+ onAuthClose={() => {
+ // Clear messages and navigate to root
+ setMessages([])
+ router.push('/')
+ }}
+ />
+ </div>
+ </ChatProvider>
)
}
diff --git a/components/data-section.tsx b/components/data-section.tsx
deleted file mode 100644
index ecebe4ad..00000000
--- a/components/data-section.tsx
+++ /dev/null
@@ -1,27 +0,0 @@
-'use client'
-
-import React from 'react'
-
-import type { DataPart } from '@/lib/types/ai'
-
-import { RelatedQuestions } from './related-questions'
-
-interface DataSectionProps {
- part: DataPart
- onQuerySelect?: (query: string) => void
-}
-
-export function DataSection({ part, onQuerySelect }: DataSectionProps) {
- switch (part.type) {
- case 'data-relatedQuestions':
- if (onQuerySelect) {
- return (
- <RelatedQuestions data={part.data} onQuerySelect={onQuerySelect} />
- )
- }
- return null
-
- default:
- return null
- }
-}
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
index 5752ae0b..0c1d78f4 100644
--- a/components/message-actions.tsx
+++ b/components/message-actions.tsx
@@ -6,6 +6,7 @@ import { UseChatHelpers } from '@ai-sdk/react'
import { Copy, ThumbsDown, ThumbsUp } from 'lucide-react'
import { toast } from 'sonner'
+import { stripSpecBlocks } from '@/lib/render/strip-spec-blocks'
import type { SearchResultItem } from '@/lib/types'
import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { cn } from '@/lib/utils'
@@ -59,7 +60,7 @@ export function MessageActions({
}
async function handleCopy() {
- await navigator.clipboard.writeText(mappedMessage)
+ await navigator.clipboard.writeText(stripSpecBlocks(mappedMessage))
toast.success('Message copied to clipboard')
}
diff --git a/components/message.tsx b/components/message.tsx
index 8326bfb0..e8247886 100644
--- a/components/message.tsx
+++ b/components/message.tsx
@@ -1,8 +1,15 @@
'use client'
+import { useMemo } from 'react'
+
import { math } from '@streamdown/math'
-import { defaultRehypePlugins, Streamdown } from 'streamdown'
+import {
+ defaultRehypePlugins,
+ Streamdown,
+ type StreamdownProps
+} from 'streamdown'
+import { mergeStreamdownSpecRenderer } from '@/lib/render/streamdown-spec'
import type { SearchResultItem } from '@/lib/types'
import { cn } from '@/lib/utils'
import { processCitations } from '@/lib/utils/citation'
@@ -12,6 +19,12 @@ import { Citing } from './custom-link'
import 'katex/dist/katex.min.css'
+const rehypePlugins = Object.values(defaultRehypePlugins)
+
+const customComponents = {
+ a: Citing
+}
+
export function MarkdownMessage({
message,
className,
@@ -24,12 +37,13 @@ export function MarkdownMessage({
// Process citations to replace [number](#toolCallId) with [number](actual-url)
const processedMessage = processCitations(message || '', citationMaps || {})
- // Define custom components for links (use Streamdown defaults for code blocks)
- const customComponents = {
- a: Citing
- }
-
- const rehypePlugins = Object.values(defaultRehypePlugins)
+ const streamdownProps = useMemo<Partial<StreamdownProps>>(
+ () => ({
+ mode: 'streaming' as const,
+ plugins: mergeStreamdownSpecRenderer({ math })
+ }),
+ []
+ )
return (
<CitationProvider citationMaps={citationMaps}>
@@ -40,7 +54,7 @@ export function MarkdownMessage({
)}
>
<Streamdown
- plugins={{ math }}
+ {...streamdownProps}
rehypePlugins={rehypePlugins}
components={customComponents}
>
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
deleted file mode 100644
index fc1cebfb..00000000
--- a/components/related-questions.tsx
+++ /dev/null
@@ -1,95 +0,0 @@
-'use client'
-
-import React from 'react'
-
-import { ArrowRight } from 'lucide-react'
-
-import type { RelatedQuestionsData } from '@/lib/types/ai'
-
-import { Button } from './ui/button'
-import { Skeleton } from './ui/skeleton'
-import { CollapsibleMessage } from './collapsible-message'
-import { Section } from './section'
-
-interface RelatedQuestionsProps {
- data: RelatedQuestionsData
- onQuerySelect: (query: string) => void
-}
-
-export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
- data,
- onQuerySelect
-}) => {
- const renderQuestionButtons = (questions: Array<{ question: string }>) =>
- questions.map((item, index) => (
- <div className="flex items-start w-full" key={index}>
- <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />
- <Button
- variant="link"
- className="flex-1 justify-start px-0 py-0 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
- type="submit"
- name={'related_query'}
- value={item.question}
- onClick={() => onQuerySelect(item.question)}
- >
- {item.question}
- </Button>
- </div>
- ))
-
- return (
- <CollapsibleMessage
- role="assistant"
- isCollapsible={false}
- isOpen={true}
- onOpenChange={() => {}}
- showIcon={false}
- showBorder={false}
- >
- <Section title="Related" className="pt-0 pb-4">
- <div className="flex flex-col gap-2">
- {data.status === 'streaming' && data.questions && (
- // Show received questions immediately while the rest stream
- <>
- {renderQuestionButtons(data.questions)}
- {Array.from({
- length: Math.max(0, 3 - data.questions.length)
- }).map((_, index) => (
- <div
- className="flex items-start w-full"
- key={`placeholder-${index}`}
- >
- <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />
- <Skeleton className="h-6 w-full" />
- </div>
- ))}
- </>
- )}
-
- {data.status === 'loading' && (
- <>
- {[1, 2, 3].map((_, index) => (
- <div className="flex items-start w-full" key={index}>
- <ArrowRight className="h-4 w-4 mr-2 mt-0.5 shrink-0 text-accent-foreground/50" />
- <Skeleton className="h-6 w-full" />
- </div>
- ))}
- </>
- )}
-
- {data.status === 'error' && (
- <div className="text-sm text-muted-foreground">
- Failed to generate related questions
- </div>
- )}
-
- {data.status === 'success' && data.questions && (
- <>{renderQuestionButtons(data.questions)}</>
- )}
- </div>
- </Section>
- </CollapsibleMessage>
- )
-}
-
-export default RelatedQuestions
diff --git a/components/render-message.tsx b/components/render-message.tsx
index 8bd0bf07..3cc63d8e 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -20,7 +20,6 @@ interface RenderMessageProps {
messageId: string
getIsOpen: (id: string, partType?: string, hasNextPart?: boolean) => boolean
onOpenChange: (id: string, open: boolean) => void
- onQuerySelect: (query: string) => void
chatId?: string
isGuest?: boolean
status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
@@ -36,7 +35,6 @@ export function RenderMessage({
messageId,
getIsOpen,
onOpenChange,
- onQuerySelect,
chatId,
isGuest = false,
status,
@@ -46,6 +44,11 @@ export function RenderMessage({
isLatestMessage = false,
citationMaps = {}
}: RenderMessageProps) {
+ const isNonEmptyTextPart = (part: any) =>
+ part?.type === 'text' &&
+ typeof part.text === 'string' &&
+ part.text.trim().length > 0
+
// Use provided citation maps (from all messages)
if (message.role === 'user') {
return (
@@ -93,7 +96,6 @@ export function RenderMessage({
parts={buffer}
getIsOpen={getIsOpen}
onOpenChange={onOpenChange}
- onQuerySelect={onQuerySelect}
status={status}
addToolResult={addToolResult}
/>
@@ -103,6 +105,11 @@ export function RenderMessage({
message.parts?.forEach((part: any, index: number) => {
if (part.type === 'text') {
+ // Ignore empty text chunks (some providers emit them before reasoning/tool parts).
+ if (!isNonEmptyTextPart(part)) {
+ return
+ }
+
// Check if there's buffered content before this text part
const hasBufferedContent = buffer.length > 0
@@ -118,7 +125,6 @@ export function RenderMessage({
parts={buffer}
getIsOpen={getIsOpen}
onOpenChange={onOpenChange}
- onQuerySelect={onQuerySelect}
status={status}
addToolResult={addToolResult}
hasSubsequentText={true}
@@ -129,7 +135,7 @@ export function RenderMessage({
}
const remainingParts = message.parts?.slice(index + 1) || []
- const hasMoreTextParts = remainingParts.some(p => p.type === 'text')
+ const hasMoreTextParts = remainingParts.some(isNonEmptyTextPart)
const isLastTextPart = !hasMoreTextParts
const isStreamingComplete =
status !== 'streaming' && status !== 'submitted'
@@ -156,11 +162,7 @@ export function RenderMessage({
citationMaps={citationMaps}
/>
)
- } else if (
- part.type === 'reasoning' ||
- part.type?.startsWith?.('tool-') ||
- part.type?.startsWith?.('data-')
- ) {
+ } else if (part.type === 'reasoning' || part.type?.startsWith?.('tool-')) {
buffer.push(part)
} else if (part.type === 'dynamic-tool') {
flushBuffer(`seg-${index}`)
diff --git a/components/research-process-section.tsx b/components/research-process-section.tsx
index 3f27ea9a..23a5fcce 100644
--- a/components/research-process-section.tsx
+++ b/components/research-process-section.tsx
@@ -6,13 +6,7 @@ import type { ReasoningPart } from '@ai-sdk/provider-utils'
import { UseChatHelpers } from '@ai-sdk/react'
import { ChevronDown } from 'lucide-react'
-import type {
- DataPart as UIDataPart,
- ToolPart,
- UIDataTypes,
- UIMessage,
- UITools
-} from '@/lib/types/ai'
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import type { DynamicToolPart } from '@/lib/types/dynamic-tools'
import { cn } from '@/lib/utils'
@@ -21,7 +15,6 @@ import {
CollapsibleContent,
CollapsibleTrigger
} from './ui/collapsible'
-import { DataSection } from './data-section'
import { ReasoningSection } from './reasoning-section'
import { ToolSection } from './tool-section'
@@ -31,14 +24,7 @@ type TextPart = {
text: string
}
-type DataPart = UIDataPart
-
-type MessagePart =
- | ReasoningPart
- | ToolPart
- | TextPart
- | DataPart
- | DynamicToolPart
+type MessagePart = ReasoningPart | ToolPart | TextPart | DynamicToolPart
// Type guards
function isReasoningPart(part: MessagePart): part is ReasoningPart {
@@ -55,8 +41,15 @@ function isTextPart(part: MessagePart): part is TextPart {
return part.type === 'text'
}
-function isDataPart(part: MessagePart): part is DataPart {
- return part.type?.startsWith?.('data-') ?? false
+function isNonEmptyTextPart(part: MessagePart): part is TextPart {
+ return isTextPart(part) && part.text.trim().length > 0
+}
+
+function isRenderablePart(part: MessagePart): boolean {
+ if (isReasoningPart(part) || isTextPart(part)) {
+ return part.text.trim().length > 0
+ }
+ return true
}
type Props = {
@@ -64,7 +57,6 @@ type Props = {
messageId: string
getIsOpen: (id: string, partType?: string, hasNextPart?: boolean) => boolean
onOpenChange: (id: string, open: boolean) => void
- onQuerySelect: (query: string) => void
status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
addToolResult?: (params: { toolCallId: string; result: any }) => void
parts?: MessagePart[]
@@ -82,7 +74,7 @@ function splitByText(parts: MessagePart[]): MessagePart[][] {
let currentSegment: MessagePart[] = []
for (const part of parts || []) {
- if (isTextPart(part)) {
+ if (isNonEmptyTextPart(part)) {
// When we hit a text part, save the current segment if it has content
if (currentSegment.length > 0) {
segments.push(currentSegment)
@@ -187,8 +179,7 @@ function RenderPart({
openSectionId,
handleAccordionChange,
status,
- addToolResult,
- onQuerySelect
+ addToolResult
}: {
part: MessagePart
partId: string
@@ -204,7 +195,6 @@ function RenderPart({
handleAccordionChange: (id: string, open: boolean, isSingle: boolean) => void
status?: any
addToolResult?: (params: { toolCallId: string; result: any }) => void
- onQuerySelect: (query: string) => void
}) {
const hasSubsequent = hasNext || hasSubsequentContent
@@ -239,7 +229,6 @@ function RenderPart({
}
status={status}
addToolResult={addToolResult}
- onQuerySelect={onQuerySelect}
borderless={!isSingle}
isFirst={isFirstGroup && partIndex === 0}
isLast={isLastGroup && partIndex === groupLength - 1}
@@ -247,10 +236,6 @@ function RenderPart({
)
}
- if (isDataPart(part)) {
- return <DataSection part={part} onQuerySelect={onQuerySelect} />
- }
-
return null
}
@@ -295,7 +280,6 @@ export function ResearchProcessSection({
messageId,
getIsOpen,
onOpenChange,
- onQuerySelect,
status,
addToolResult,
parts: partsOverride,
@@ -303,8 +287,11 @@ export function ResearchProcessSection({
}: Props) {
const allParts = (partsOverride ?? (message.parts || [])) as MessagePart[]
- // Filter out empty reasoning parts to avoid incorrect grouping
- const filteredParts = allParts.filter(p => !(isReasoningPart(p) && !p.text))
+ // Filter out empty reasoning/text parts to avoid incorrect grouping
+ const filteredParts = allParts.filter(isRenderablePart)
+ const filteredMessageParts = ((message.parts || []) as MessagePart[]).filter(
+ isRenderablePart
+ )
const segments = partsOverride ? [filteredParts] : splitByText(filteredParts)
@@ -315,7 +302,7 @@ export function ResearchProcessSection({
// Use custom hook for subsequent content detection
const hasSubsequentContent = useHasSubsequentContent(
segments,
- message.parts as MessagePart[] | undefined
+ filteredMessageParts
)
// State for parent collapsible (when segment has 5+ parts)
@@ -370,7 +357,6 @@ export function ResearchProcessSection({
handleAccordionChange={handleAccordionChange}
status={status}
addToolResult={addToolResult}
- onQuerySelect={onQuerySelect}
/>
)
})}
diff --git a/components/spec-block.tsx b/components/spec-block.tsx
new file mode 100644
index 00000000..c199a203
--- /dev/null
+++ b/components/spec-block.tsx
@@ -0,0 +1,54 @@
+'use client'
+
+import { useMemo } from 'react'
+
+import { ActionProvider, JSONUIProvider, Renderer } from '@json-render/react'
+
+import { useChatContext } from '@/lib/contexts/chat-context'
+import { registry } from '@/lib/render/registry'
+import type { SpecFenceResult } from '@/lib/render/spec-fence'
+
+type SpecBlockProps = {
+ result: SpecFenceResult
+}
+
+export function SpecBlock({ result }: SpecBlockProps) {
+ const chatContext = useChatContext()
+
+ const handlers = useMemo(
+ () => ({
+ submitQuery: (params: Record<string, unknown>) => {
+ const query = (params as { query?: string }).query
+ if (typeof query === 'string' && query.trim()) {
+ chatContext.sendMessage({
+ role: 'user',
+ parts: [{ type: 'text', text: query }]
+ })
+ }
+ }
+ }),
+ [chatContext]
+ )
+
+ const content = useMemo(() => {
+ if (result.status !== 'ready') {
+ return null
+ }
+
+ if (!result.spec.root || !result.spec.elements) {
+ return null
+ }
+
+ return (
+ <div className="pt-2 pb-4">
+ <JSONUIProvider registry={registry} initialState={result.spec.state}>
+ <ActionProvider handlers={handlers}>
+ <Renderer spec={result.spec} registry={registry} />
+ </ActionProvider>
+ </JSONUIProvider>
+ </div>
+ )
+ }, [result, handlers])
+
+ return content
+}
diff --git a/components/spec-fence-block.tsx b/components/spec-fence-block.tsx
new file mode 100644
index 00000000..c4af4b32
--- /dev/null
+++ b/components/spec-fence-block.tsx
@@ -0,0 +1,14 @@
+'use client'
+
+import { useMemo, useState } from 'react'
+
+import { createSpecFenceEvaluator } from '@/lib/render/spec-fence'
+
+import { SpecBlock } from '@/components/spec-block'
+
+export function SpecFenceBlock({ source }: { source: string }) {
+ const [evaluator] = useState(() => createSpecFenceEvaluator())
+ const result = useMemo(() => evaluator.evaluate(source), [evaluator, source])
+
+ return <SpecBlock result={result} />
+}
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index 97ef557e..c7be5faf 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -15,7 +15,6 @@ interface ToolSectionProps {
onOpenChange: (open: boolean) => void
status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
addToolResult?: (params: { toolCallId: string; result: any }) => void
- onQuerySelect: (query: string) => void
borderless?: boolean
isFirst?: boolean
isLast?: boolean
@@ -27,7 +26,6 @@ export function ToolSection({
onOpenChange,
status,
addToolResult,
- onQuerySelect,
borderless = false,
isFirst = false,
isLast = false
diff --git a/config/models/cloud.json b/config/models/cloud.json
index 11e7e805..a537c8eb 100644
--- a/config/models/cloud.json
+++ b/config/models/cloud.json
@@ -26,12 +26,6 @@
}
}
}
- },
- "relatedQuestions": {
- "id": "gemini-2.5-flash-lite",
- "name": "Gemini 2.5 Flash Lite",
- "provider": "Google",
- "providerId": "google"
}
}
}
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
deleted file mode 100644
index 3ecc7cf2..00000000
--- a/lib/agents/generate-related-questions.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import { type ModelMessage, Output, streamText } from 'ai'
-
-import { getRelatedQuestionsModel } from '../config/model-types'
-import { relatedQuestionSchema } from '../schema/related'
-import { Model } from '../types/models'
-import { getModel } from '../utils/registry'
-import { isTracingEnabled } from '../utils/telemetry'
-
-import { RELATED_QUESTIONS_PROMPT } from './prompts/related-questions-prompt'
-
-export function createRelatedQuestionsStream(
- messages: ModelMessage[],
- abortSignal?: AbortSignal,
- parentTraceId?: string,
- localSelectedModel?: Model
-) {
- // Use cloud related-questions model when available, otherwise local default model.
- const relatedModel = getRelatedQuestionsModel(localSelectedModel)
- const modelId = `${relatedModel.providerId}:${relatedModel.id}`
-
- return streamText({
- model: getModel(modelId),
- output: Output.array({
- element: relatedQuestionSchema,
- name: 'RelatedQuestion',
- description: 'Generate a concise follow-up question (max 10-12 words)'
- }),
- system: RELATED_QUESTIONS_PROMPT,
- messages: [
- ...messages,
- {
- role: 'user',
- content:
- 'Based on the conversation history and search results, generate 3 unique follow-up questions that would help the user explore different aspects of the topic. Focus on questions that dig deeper into specific findings or explore related areas not yet covered.'
- }
- ],
- abortSignal,
- experimental_telemetry: {
- isEnabled: isTracingEnabled(),
- functionId: 'related-questions',
- metadata: {
- modelId,
- agentType: 'related-questions-generator',
- messageCount: messages.length,
- ...(parentTraceId && {
- langfuseTraceId: parentTraceId,
- langfuseUpdateParent: false
- })
- }
- }
- })
-}
diff --git a/lib/agents/prompts/related-questions-prompt.ts b/lib/agents/prompts/related-questions-prompt.ts
deleted file mode 100644
index 9a0e6a61..00000000
--- a/lib/agents/prompts/related-questions-prompt.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-export const RELATED_QUESTIONS_PROMPT = `You are a professional web researcher tasked with generating follow-up questions.
-
-Language:
-- ALWAYS generate questions in the user's language.
-
-Based on the conversation history and search results, create 3 CONCISE related questions that:
-
-1. Explore NEW aspects not covered in the original query
-2. Dig deeper into specific details from the search results
-3. Connect to related topics or implications
-
-Guidelines:
-- Keep questions SHORT and CONCISE (max 10-12 words)
-- NEVER repeat or rephrase the original question
-- Each question should explore a UNIQUE angle
-- Be specific and focused
-- Use clear, simple language
-
-Example:
-Original: "Why is Nvidia growing rapidly?"
-Good follow-ups:
-- "What are Nvidia's main AI chip competitors?"
-- "How much revenue comes from data centers?"
-- "Which companies buy Nvidia's AI chips?"
-
-Bad follow-ups (avoid these):
-- "Besides Broadcom, which other specific companies are emerging as significant competitors..." (too long)
-- "Why is Nvidia growing so fast?" (rephrases original)
-- "Tell me about Nvidia" (too general)`
diff --git a/lib/agents/prompts/search-mode-prompts.ts b/lib/agents/prompts/search-mode-prompts.ts
index ff1a7b51..eb68b0b5 100644
--- a/lib/agents/prompts/search-mode-prompts.ts
+++ b/lib/agents/prompts/search-mode-prompts.ts
@@ -1,3 +1,4 @@
+import { getRelatedQuestionsSpecPrompt } from '@/lib/render/prompt'
import {
getContentTypesGuidance,
isGeneralSearchProviderAvailable
@@ -107,10 +108,9 @@ OUTPUT FORMAT (MANDATORY):
- Use bullets with bolded keywords for key points: \`- **Point:** concise explanation\`.
- **Use tables for comparisons** (pricing, specs, features, pros/cons) - they're clearer than bullets for side-by-side data
- Focus on delivering clear information with natural flow, avoiding rigid templates.
-- Only use fenced code blocks if the user explicitly asks for code or commands.
+- Only use fenced code blocks if the user explicitly asks for code or commands (the mandatory \`\`\`spec block for related questions is an exception).
- Prefer natural, conversational tone while maintaining informativeness.
- Always end with a brief conclusion that synthesizes the main points into a cohesive summary.
-- **CRITICAL: Do NOT include follow-up suggestions or questions at the end** (e.g., "If you want, I can..." or "Would you like me to..."). The application provides related questions separately.
- Response length guidance:
- Simple definitions or facts: Keep concise and direct
- Comparisons or multi-faceted topics: Provide comprehensive coverage
@@ -138,6 +138,8 @@ Example approach:
- **Consideration:** Practical implications with real-world context
End with a synthesizing conclusion that ties the main points together into a clear overall picture.
+
+${getRelatedQuestionsSpecPrompt()}
`
}
@@ -288,7 +290,6 @@ OUTPUT FORMAT (MANDATORY):
- Adapt length and structure to query complexity: simple topics can be concise, complex topics should be thorough.
- Place all citations at the end of the sentence they support.
- Always include a brief conclusion that synthesizes the key points.
-- **CRITICAL: Do NOT include follow-up suggestions or questions at the end** (e.g., "If you want, I can..." or "Would you like me to..."). The application provides related questions separately.
- Response length guidance:
- Scale naturally with query complexity
- Simple queries: Concise and direct answers
@@ -309,6 +310,8 @@ Flexible example:
- **Context:** Relevant supporting details
Conclude with a brief synthesis that ties together the main insights into a clear overall understanding.
+
+${getRelatedQuestionsSpecPrompt()}
`
}
diff --git a/lib/config/load-models-config.ts b/lib/config/load-models-config.ts
index debd9fc8..28acc328 100644
--- a/lib/config/load-models-config.ts
+++ b/lib/config/load-models-config.ts
@@ -8,7 +8,6 @@ export interface ModelsConfig {
models: {
quick: Model
adaptive: Model
- relatedQuestions: Model
}
}
@@ -30,13 +29,6 @@ function validateModelsConfigStructure(
if (!parsed.models || typeof parsed.models !== 'object') {
throw new Error('Invalid models config: missing models')
}
- if (!parsed.models.relatedQuestions) {
- throw new Error('Invalid models config: missing required sections')
- }
- if (typeof parsed.models.relatedQuestions !== 'object') {
- throw new Error('Invalid models config: relatedQuestions must be an object')
- }
-
for (const searchMode of VALID_SEARCH_MODES) {
const modeEntry = parsed.models[searchMode]
if (!modeEntry || typeof modeEntry !== 'object') {
diff --git a/lib/config/model-types.ts b/lib/config/model-types.ts
index 4a11293d..c1f74dfc 100644
--- a/lib/config/model-types.ts
+++ b/lib/config/model-types.ts
@@ -1,4 +1,3 @@
-import { DEFAULT_MODEL } from '@/lib/config/default-model'
import { Model } from '@/lib/types/models'
import { SearchMode } from '@/lib/types/search'
@@ -13,15 +12,3 @@ export function getModelForMode(mode: SearchMode): Model | undefined {
const cfg = getModelsConfig()
return cfg.models?.[mode]
}
-
-// Accessor for related questions model.
-// In local/docker, reuse the selected local model when available to keep
-// provider routing consistent with the main chat flow.
-export function getRelatedQuestionsModel(localSelectedModel?: Model): Model {
- if (!isCloudDeployment()) {
- return localSelectedModel ?? DEFAULT_MODEL
- }
-
- const cfg = getModelsConfig()
- return cfg.models.relatedQuestions
-}
diff --git a/lib/config/ollama-validator.ts b/lib/config/ollama-validator.ts
index b05062b4..2b817ab6 100644
--- a/lib/config/ollama-validator.ts
+++ b/lib/config/ollama-validator.ts
@@ -26,11 +26,7 @@ async function getConfiguredOllamaModels(): Promise<Model[]> {
const config = await loadModelsConfig()
// Check quick/adaptive models
- const cloudModels = [
- config.models.quick,
- config.models.adaptive,
- config.models.relatedQuestions
- ]
+ const cloudModels = [config.models.quick, config.models.adaptive]
for (const model of cloudModels) {
if (model?.providerId === 'ollama') {
diff --git a/lib/contexts/chat-context.tsx b/lib/contexts/chat-context.tsx
new file mode 100644
index 00000000..718dafcc
--- /dev/null
+++ b/lib/contexts/chat-context.tsx
@@ -0,0 +1,31 @@
+'use client'
+
+import { createContext, useContext } from 'react'
+
+type ChatContextValue = {
+ sendMessage: (message: {
+ role: 'user'
+ parts: Array<{ type: 'text'; text: string }>
+ }) => void
+}
+
+const ChatContext = createContext<ChatContextValue | null>(null)
+
+export function ChatProvider({
+ sendMessage,
+ children
+}: ChatContextValue & { children: React.ReactNode }) {
+ return (
+ <ChatContext.Provider value={{ sendMessage }}>
+ {children}
+ </ChatContext.Provider>
+ )
+}
+
+export function useChatContext(): ChatContextValue {
+ const context = useContext(ChatContext)
+ if (!context) {
+ throw new Error('useChatContext must be used within a ChatProvider')
+ }
+ return context
+}
diff --git a/lib/render/__tests__/parse-spec-block.test.ts b/lib/render/__tests__/parse-spec-block.test.ts
new file mode 100644
index 00000000..c8ad23a0
--- /dev/null
+++ b/lib/render/__tests__/parse-spec-block.test.ts
@@ -0,0 +1,117 @@
+import { describe, expect, test } from 'vitest'
+
+import { createPartialSpecParser, parseSpecBlock } from '../parse-spec-block'
+
+// Helper to build JSONL source for related questions spec
+function buildRelatedQuestionsSource(questions: string[]): string {
+ const lines = [
+ '{"op":"add","path":"/root","value":"main"}',
+ `{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"direction":"vertical","gap":"sm"},"children":[${questions.map((_, i) => `"q${i + 1}"`).join(',')}]}}`
+ ]
+ questions.forEach((q, i) => {
+ lines.push(
+ `{"op":"add","path":"/elements/q${i + 1}","value":{"type":"QuestionButton","props":{"text":"${q}"},"on":{"press":{"action":"submitQuery","params":{"query":"${q}"}}},"children":[]}}`
+ )
+ })
+ return lines.join('\n')
+}
+
+describe('createPartialSpecParser', () => {
+ test('returns null for empty source', () => {
+ const parser = createPartialSpecParser()
+ expect(parser.parse('')).toBeNull()
+ expect(parser.parse(' ')).toBeNull()
+ })
+
+ test('parses a single root patch', () => {
+ const parser = createPartialSpecParser()
+ const source = '{"op":"add","path":"/root","value":"main"}'
+ const result = parser.parse(source)
+
+ expect(result).not.toBeNull()
+ expect(result!.root).toBe('main')
+ })
+
+ test('incrementally parses streamed content', () => {
+ const parser = createPartialSpecParser()
+
+ const line1 = '{"op":"add","path":"/root","value":"main"}'
+ const result1 = parser.parse(line1)
+ expect(result1).not.toBeNull()
+ expect(result1!.root).toBe('main')
+ expect(Object.keys(result1!.elements)).toHaveLength(0)
+
+ const line2 =
+ '\n{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"gap":"sm"},"children":[]}}'
+ const result2 = parser.parse(line1 + line2)
+ expect(result2).not.toBeNull()
+ expect(result2!.elements['main']).toBeDefined()
+ expect(result2!.elements['main'].type).toBe('Stack')
+ })
+
+ test('prunes references to missing children', () => {
+ const parser = createPartialSpecParser()
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{},"children":["q1","q2"]}}'
+ // q1 and q2 are not defined yet
+ ].join('\n')
+
+ const result = parser.parse(source)
+ expect(result).not.toBeNull()
+ // Children referencing missing elements should be pruned
+ expect(result!.elements['main'].children).toEqual([])
+ })
+
+ test('returns cached result for same source', () => {
+ const parser = createPartialSpecParser()
+ const source = '{"op":"add","path":"/root","value":"main"}'
+
+ const result1 = parser.parse(source)
+ const result2 = parser.parse(source)
+ expect(result1).toEqual(result2)
+ })
+
+ test('resets state', () => {
+ const parser = createPartialSpecParser()
+ const source = '{"op":"add","path":"/root","value":"main"}'
+ parser.parse(source)
+
+ parser.reset()
+ expect(parser.parse('')).toBeNull()
+ })
+})
+
+describe('parseSpecBlock', () => {
+ test('parses a valid complete related questions spec', () => {
+ const source = buildRelatedQuestionsSource([
+ 'Question 1',
+ 'Question 2',
+ 'Question 3'
+ ])
+
+ const spec = parseSpecBlock(source)
+ expect(spec.root).toBe('main')
+ expect(Object.keys(spec.elements)).toHaveLength(4) // main + q1 + q2 + q3
+ expect(spec.elements['q1'].type).toBe('QuestionButton')
+ expect(spec.elements['q1'].props).toEqual({ text: 'Question 1' })
+ })
+
+ test('throws for invalid component types', () => {
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"InvalidComponent","props":{},"children":[]}}'
+ ].join('\n')
+
+ expect(() => parseSpecBlock(source)).toThrow()
+ })
+
+ test('throws for dangling child references', () => {
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{},"children":["missing"]}}'
+ ].join('\n')
+
+ expect(() => parseSpecBlock(source)).toThrow()
+ })
+})
diff --git a/lib/render/__tests__/spec-fence.test.ts b/lib/render/__tests__/spec-fence.test.ts
new file mode 100644
index 00000000..85880300
--- /dev/null
+++ b/lib/render/__tests__/spec-fence.test.ts
@@ -0,0 +1,95 @@
+import { describe, expect, test } from 'vitest'
+
+import { createSpecFenceEvaluator } from '../spec-fence'
+
+describe('createSpecFenceEvaluator', () => {
+ test('returns pending for empty source', () => {
+ const evaluator = createSpecFenceEvaluator()
+
+ expect(evaluator.evaluate('')).toEqual({
+ status: 'pending',
+ source: ''
+ })
+ })
+
+ test('returns ready for partial spec with valid patches', () => {
+ const evaluator = createSpecFenceEvaluator()
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"gap":"sm"},"children":[]}}'
+ ].join('\n')
+
+ const result = evaluator.evaluate(source)
+
+ expect(result.status).toBe('ready')
+ if (result.status === 'ready') {
+ expect(result.spec.root).toBe('main')
+ expect(result.spec.elements['main'].type).toBe('Stack')
+ }
+ })
+
+ test('returns ready for complete valid spec', () => {
+ const evaluator = createSpecFenceEvaluator()
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"gap":"sm"},"children":["q1"]}}',
+ '{"op":"add","path":"/elements/q1","value":{"type":"QuestionButton","props":{"text":"Follow up?"},"on":{"press":{"action":"submitQuery","params":{"query":"Follow up?"}}},"children":[]}}'
+ ].join('\n')
+
+ const result = evaluator.evaluate(source)
+
+ expect(result.status).toBe('ready')
+ if (result.status === 'ready') {
+ expect(result.spec.root).toBe('main')
+ expect(result.spec.elements['q1'].type).toBe('QuestionButton')
+ }
+ })
+
+ test('returns ready with pruned children for dangling references', () => {
+ const evaluator = createSpecFenceEvaluator()
+ const source = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{},"children":["missing"]}}'
+ ].join('\n')
+
+ const result = evaluator.evaluate(source)
+
+ expect(result.status).toBe('ready')
+ if (result.status === 'ready') {
+ expect(result.spec.elements['main'].children).toEqual([])
+ }
+ })
+
+ test('handles incremental streaming correctly', () => {
+ const evaluator = createSpecFenceEvaluator()
+
+ // First chunk: just root
+ const result1 = evaluator.evaluate(
+ '{"op":"add","path":"/root","value":"main"}'
+ )
+ expect(result1.status).toBe('ready')
+
+ // Second chunk: add element
+ const source2 = [
+ '{"op":"add","path":"/root","value":"main"}',
+ '{"op":"add","path":"/elements/main","value":{"type":"QuestionButton","props":{"text":"Test"},"children":[]}}'
+ ].join('\n')
+ const result2 = evaluator.evaluate(source2)
+ expect(result2.status).toBe('ready')
+ if (result2.status === 'ready') {
+ expect(result2.spec.elements['main'].type).toBe('QuestionButton')
+ }
+ })
+
+ test('resets evaluator state', () => {
+ const evaluator = createSpecFenceEvaluator()
+ evaluator.evaluate('{"op":"add","path":"/root","value":"main"}')
+
+ evaluator.reset()
+
+ expect(evaluator.evaluate('')).toEqual({
+ status: 'pending',
+ source: ''
+ })
+ })
+})
diff --git a/lib/render/catalog.ts b/lib/render/catalog.ts
new file mode 100644
index 00000000..b1542a61
--- /dev/null
+++ b/lib/render/catalog.ts
@@ -0,0 +1,39 @@
+import { z } from 'zod'
+
+import { schema } from './schema'
+
+export const catalog = schema.createCatalog({
+ components: {
+ SectionHeader: {
+ props: z.object({
+ title: z.string(),
+ icon: z.enum(['related']).optional()
+ }),
+ description: 'A section heading label with optional icon'
+ },
+ Stack: {
+ props: z
+ .object({
+ direction: z.enum(['vertical', 'horizontal']).optional(),
+ gap: z.enum(['xs', 'sm', 'md', 'lg']).optional()
+ })
+ .partial(),
+ description:
+ 'A layout container that stacks children vertically or horizontally'
+ },
+ QuestionButton: {
+ props: z.object({
+ text: z.string()
+ }),
+ description: 'A related follow-up question the user can click to ask'
+ }
+ },
+ actions: {
+ submitQuery: {
+ params: z.object({
+ query: z.string()
+ }),
+ description: 'Submit a follow-up query to the chat'
+ }
+ }
+})
diff --git a/lib/render/parse-spec-block.ts b/lib/render/parse-spec-block.ts
new file mode 100644
index 00000000..ade01412
--- /dev/null
+++ b/lib/render/parse-spec-block.ts
@@ -0,0 +1,106 @@
+import {
+ compileSpecStream,
+ createSpecStreamCompiler,
+ formatSpecIssues,
+ type Spec,
+ validateSpec
+} from '@json-render/core'
+
+import { catalog } from './catalog'
+
+function compileSource(source: string): Spec {
+ return compileSpecStream(source, {
+ root: '',
+ elements: {}
+ }) as Spec
+}
+
+function prunePartialSpec(spec: Spec): Spec {
+ const elementKeys = new Set(Object.keys(spec.elements ?? {}))
+ const elements = Object.fromEntries(
+ Object.entries(spec.elements ?? {}).map(([key, element]) => [
+ key,
+ {
+ ...element,
+ children: element.children?.filter((childKey: string) =>
+ elementKeys.has(childKey)
+ )
+ }
+ ])
+ )
+
+ return {
+ ...spec,
+ elements
+ }
+}
+
+type PartialSpecCompiler = ReturnType<typeof createSpecStreamCompiler>
+
+function createCompiler(): PartialSpecCompiler {
+ return createSpecStreamCompiler({
+ root: '',
+ elements: {}
+ })
+}
+
+export type PartialSpecParser = {
+ parse(source: string): Spec | null
+ reset(): void
+}
+
+export function createPartialSpecParser(): PartialSpecParser {
+ let compiler = createCompiler()
+ let lastSource = ''
+
+ return {
+ parse(source: string): Spec | null {
+ if (!source.trim()) {
+ compiler = createCompiler()
+ lastSource = ''
+ return null
+ }
+
+ if (source === lastSource) {
+ const result = prunePartialSpec(compiler.getResult() as Spec)
+ return result.root ? result : null
+ }
+
+ if (source.startsWith(lastSource)) {
+ compiler.push(source.slice(lastSource.length))
+ lastSource = source
+ const result = prunePartialSpec(compiler.getResult() as Spec)
+ return result.root ? result : null
+ }
+
+ compiler = createCompiler()
+ compiler.push(source)
+ lastSource = source
+ const result = prunePartialSpec(compiler.getResult() as Spec)
+ return result.root ? result : null
+ },
+ reset() {
+ compiler = createCompiler()
+ lastSource = ''
+ }
+ }
+}
+
+export function parseSpecBlock(source: string): Spec {
+ const validation = catalog.validate(compileSource(source))
+
+ if (!validation.success || !validation.data) {
+ const issues = validation.error?.issues
+ .map((issue: { message: string }) => issue.message)
+ .join(', ')
+ throw new Error(issues || 'Invalid spec block')
+ }
+
+ const validatedSpec = validation.data as Spec
+ const specIssues = validateSpec(validatedSpec)
+ if (!specIssues.valid) {
+ throw new Error(formatSpecIssues(specIssues.issues))
+ }
+
+ return validatedSpec
+}
diff --git a/lib/render/prompt.ts b/lib/render/prompt.ts
new file mode 100644
index 00000000..6326835f
--- /dev/null
+++ b/lib/render/prompt.ts
@@ -0,0 +1,42 @@
+/**
+ * Returns the prompt section that instructs the LLM to output
+ * related questions as a ```spec fenced block at the end of its response.
+ */
+export function getRelatedQuestionsSpecPrompt(): string {
+ return `
+RELATED QUESTIONS (MANDATORY):
+After your conclusion, you MUST generate exactly 3 follow-up questions in a \`\`\`spec fenced code block.
+Each question should explore a different aspect of the topic not yet covered.
+Questions must be concise (max 10-12 words) and in the user's language.
+
+The spec block uses JSONL (one JSON object per line) with RFC 6902 JSON Patch operations.
+Always include a SectionHeader with title "Related" as the first child element.
+
+Example output (always at the very end of your response):
+
+\`\`\`spec
+{"op":"add","path":"/root","value":"main"}
+{"op":"add","path":"/elements/main","value":{"type":"Stack","props":{"direction":"vertical","gap":"sm"},"children":["header","q1","q2","q3"]}}
+{"op":"add","path":"/elements/header","value":{"type":"SectionHeader","props":{"title":"Related","icon":"related"},"children":[]}}
+{"op":"add","path":"/elements/q1","value":{"type":"QuestionButton","props":{"text":"First follow-up question here"},"on":{"press":{"action":"submitQuery","params":{"query":"First follow-up question here"}}},"children":[]}}
+{"op":"add","path":"/elements/q2","value":{"type":"QuestionButton","props":{"text":"Second follow-up question here"},"on":{"press":{"action":"submitQuery","params":{"query":"Second follow-up question here"}}},"children":[]}}
+{"op":"add","path":"/elements/q3","value":{"type":"QuestionButton","props":{"text":"Third follow-up question here"},"on":{"press":{"action":"submitQuery","params":{"query":"Third follow-up question here"}}},"children":[]}}
+\`\`\`
+
+AVAILABLE COMPONENTS:
+- SectionHeader: { title: string, icon?: "related" } - A section heading label with optional icon
+- Stack: { direction?: "vertical" | "horizontal", gap?: "xs" | "sm" | "md" | "lg" } - Layout container
+- QuestionButton: { text: string } - A clickable follow-up question button
+
+AVAILABLE ACTIONS:
+- submitQuery: { query: string } - Submit a follow-up query
+
+SPEC RULES:
+1. Always wrap JSONL patches in a single \`\`\`spec fence at the END of your response.
+2. The \`\`\`spec block must contain ONLY JSONL patches — no commentary inside.
+3. Keep each JSON object on a single line.
+4. The "text" prop and "query" param must be identical for each question.
+5. Do NOT open more than one \`\`\`spec block in a single answer.
+6. Do NOT include follow-up suggestions or questions in your markdown text. Only use the spec block for them.
+`
+}
diff --git a/lib/render/registry.tsx b/lib/render/registry.tsx
new file mode 100644
index 00000000..bf6b6d60
--- /dev/null
+++ b/lib/render/registry.tsx
@@ -0,0 +1,81 @@
+'use client'
+
+import type { ComponentFn } from '@json-render/react'
+import { defineRegistry } from '@json-render/react'
+import { ArrowRight, Repeat2 } from 'lucide-react'
+
+import { cn } from '@/lib/utils'
+
+import { catalog } from './catalog'
+
+type CatalogType = typeof catalog
+
+const iconMap = {
+ related: Repeat2
+} as const
+
+const SectionHeader: ComponentFn<CatalogType, 'SectionHeader'> = ({
+ props
+}) => {
+ const Icon = props.icon ? iconMap[props.icon] : null
+ return (
+ <div className="flex items-center gap-2 text-lg font-bold text-foreground">
+ {Icon && <Icon className="size-5" />}
+ {props.title}
+ </div>
+ )
+}
+
+const stackGap = {
+ xs: 'gap-1',
+ sm: 'gap-2',
+ md: 'gap-3',
+ lg: 'gap-4'
+}
+
+const Stack: ComponentFn<CatalogType, 'Stack'> = ({ props, children }) => {
+ const { direction = 'vertical', gap = 'md' } = props
+ return (
+ <div
+ className={cn(
+ 'flex',
+ direction === 'horizontal' ? 'flex-row flex-wrap' : 'flex-col',
+ stackGap[gap]
+ )}
+ >
+ {children}
+ </div>
+ )
+}
+
+const QuestionButton: ComponentFn<CatalogType, 'QuestionButton'> = ({
+ props,
+ on
+}) => {
+ const handle = on('press')
+ return (
+ <div className="flex items-center w-fit">
+ <ArrowRight className="h-4 w-4 mr-2 shrink-0 text-accent-foreground/50" />
+ <button
+ type="button"
+ onClick={handle.emit}
+ className="flex-1 justify-start px-0 py-0.5 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left hover:text-accent-foreground/70 transition-colors cursor-pointer"
+ >
+ {props.text}
+ </button>
+ </div>
+ )
+}
+
+export const { registry } = defineRegistry(catalog, {
+ components: {
+ SectionHeader,
+ Stack,
+ QuestionButton
+ },
+ actions: {
+ submitQuery: async () => {
+ // Handled by ActionProvider at runtime
+ }
+ }
+})
diff --git a/lib/render/schema.ts b/lib/render/schema.ts
new file mode 100644
index 00000000..3810e4e6
--- /dev/null
+++ b/lib/render/schema.ts
@@ -0,0 +1,42 @@
+import { defineSchema } from '@json-render/core'
+import { schema as reactSchema } from '@json-render/react/schema'
+
+export const schema = defineSchema(
+ s => ({
+ spec: s.object({
+ root: s.string(),
+ elements: s.record(
+ s.object({
+ type: s.ref('catalog.components'),
+ props: {
+ ...s.propsOf('catalog.components'),
+ ...s.optional()
+ },
+ children: {
+ ...s.array(s.string()),
+ ...s.optional()
+ },
+ on: {
+ ...s.any(),
+ ...s.optional()
+ }
+ })
+ )
+ }),
+ catalog: s.object({
+ components: s.map({
+ props: s.zod(),
+ slots: s.array(s.string()),
+ description: s.string()
+ }),
+ actions: s.map({
+ params: s.zod(),
+ description: s.string()
+ })
+ })
+ }),
+ {
+ builtInActions: reactSchema.builtInActions,
+ defaultRules: reactSchema.defaultRules
+ }
+)
diff --git a/lib/render/spec-fence.ts b/lib/render/spec-fence.ts
new file mode 100644
index 00000000..674aa260
--- /dev/null
+++ b/lib/render/spec-fence.ts
@@ -0,0 +1,55 @@
+import type { Spec } from '@json-render/core'
+
+import { createPartialSpecParser } from './parse-spec-block'
+
+export type SpecFenceResult =
+ | {
+ status: 'pending'
+ source: string
+ }
+ | {
+ status: 'ready'
+ source: string
+ spec: Spec
+ }
+
+export type SpecFenceEvaluator = {
+ evaluate(source: string): SpecFenceResult
+ reset(): void
+}
+
+export function createSpecFenceEvaluator(): SpecFenceEvaluator {
+ const partialParser = createPartialSpecParser()
+
+ return {
+ evaluate(source: string): SpecFenceResult {
+ try {
+ // Always use partial parser for incremental rendering.
+ // streamdown's isIncomplete flag requires isAnimating which
+ // we don't use, so we cannot rely on it.
+ const spec = partialParser.parse(source)
+
+ if (!spec) {
+ return {
+ status: 'pending',
+ source
+ }
+ }
+
+ return {
+ status: 'ready',
+ source,
+ spec
+ }
+ } catch {
+ return {
+ status: 'pending',
+ source
+ }
+ }
+ },
+ reset() {
+ partialParser.reset()
+ }
+ }
+}
diff --git a/lib/render/streamdown-spec.tsx b/lib/render/streamdown-spec.tsx
new file mode 100644
index 00000000..0d0021eb
--- /dev/null
+++ b/lib/render/streamdown-spec.tsx
@@ -0,0 +1,31 @@
+'use client'
+
+import type { CustomRenderer, PluginConfig } from 'streamdown'
+
+import { SpecFenceBlock } from '@/components/spec-fence-block'
+
+export function createStreamdownSpecRenderer(): CustomRenderer {
+ return {
+ language: 'spec',
+ component({ code }) {
+ return <SpecFenceBlock source={code} />
+ }
+ }
+}
+
+export function mergeStreamdownSpecRenderer(
+ plugins?: PluginConfig
+): PluginConfig {
+ const userRenderers = plugins?.renderers ?? []
+ const filteredRenderers = userRenderers.filter(renderer => {
+ const languages = Array.isArray(renderer.language)
+ ? renderer.language
+ : [renderer.language]
+ return !languages.includes('spec')
+ })
+
+ return {
+ ...plugins,
+ renderers: [createStreamdownSpecRenderer(), ...filteredRenderers]
+ }
+}
diff --git a/lib/render/strip-spec-blocks.ts b/lib/render/strip-spec-blocks.ts
new file mode 100644
index 00000000..dfdee32a
--- /dev/null
+++ b/lib/render/strip-spec-blocks.ts
@@ -0,0 +1,10 @@
+/**
+ * Remove ```spec fenced blocks from text content.
+ * Used when copying message text to clipboard.
+ */
+export function stripSpecBlocks(text: string): string {
+ return text
+ .replace(/```spec[\s\S]*?```/g, '')
+ .replace(/\n{3,}/g, '\n\n')
+ .trim()
+}
diff --git a/lib/schema/related.tsx b/lib/schema/related.tsx
deleted file mode 100644
index 6a782fe0..00000000
--- a/lib/schema/related.tsx
+++ /dev/null
@@ -1,10 +0,0 @@
-import { z } from 'zod'
-
-export const relatedQuestionSchema = z.object({
- question: z.string()
-})
-
-export const relatedSchema = z.array(relatedQuestionSchema).length(3)
-
-export type RelatedQuestion = z.infer<typeof relatedQuestionSchema>
-export type Related = z.infer<typeof relatedSchema>
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index b7d3eeea..2259e6b8 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -26,8 +26,8 @@ import { perfLog, perfTime } from '../utils/perf-logging'
import { persistStreamResults } from './helpers/persist-stream-results'
import { prepareMessages } from './helpers/prepare-messages'
-import { streamRelatedQuestions } from './helpers/stream-related-questions'
import { stripReasoningParts } from './helpers/strip-reasoning-parts'
+import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
import type { StreamContext } from './helpers/types'
import { BaseStreamConfig } from './types'
@@ -137,9 +137,10 @@ export async function createChatStreamResponse(
// OpenAI's Responses API requires reasoning items and their following items to be kept together
// See: https://github.com/vercel/ai/issues/11036
const isOpenAI = context.modelId.startsWith('openai:')
+ const messagesWithoutSpec = stripSpecFromMessages(messagesToModel)
const messagesToConvert = isOpenAI
- ? stripReasoningParts(messagesToModel)
- : messagesToModel
+ ? stripReasoningParts(messagesWithoutSpec)
+ : messagesWithoutSpec
// Convert to model messages and apply context window management
let modelMessages = await convertToModelMessages(messagesToConvert)
@@ -204,26 +205,8 @@ export async function createChatStreamResponse(
})
)
- const responseMessages = (await result.response).messages
+ await result.response
perfTime('researchAgent.stream completed', llmStart)
- // Generate related questions
- if (responseMessages && responseMessages.length > 0) {
- // Find the last user message
- const lastUserMessage = [...modelMessages]
- .reverse()
- .find(msg => msg.role === 'user')
- const messagesForQuestions = lastUserMessage
- ? [lastUserMessage, ...responseMessages]
- : responseMessages
-
- await streamRelatedQuestions(
- writer,
- messagesForQuestions,
- abortSignal,
- parentTraceId,
- model
- )
- }
} catch (error) {
console.error('Stream execution error:', error)
throw error // This error will be handled by the onError callback
diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.ts
index 7dd0434f..fc525fe1 100644
--- a/lib/streaming/create-ephemeral-chat-stream-response.ts
+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts
@@ -20,8 +20,8 @@ import {
truncateMessages
} from '../utils/context-window'
-import { streamRelatedQuestions } from './helpers/stream-related-questions'
import { stripReasoningParts } from './helpers/strip-reasoning-parts'
+import { stripSpecFromMessages } from './helpers/strip-spec-from-messages'
import { BaseStreamConfig } from './types'
type EphemeralStreamConfig = Pick<
@@ -68,9 +68,10 @@ export async function createEphemeralChatStreamResponse(
execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {
try {
const isOpenAI = `${model.providerId}:${model.id}`.startsWith('openai:')
+ const messagesWithoutSpec = stripSpecFromMessages(messages)
const messagesToConvert = isOpenAI
- ? stripReasoningParts(messages)
- : messages
+ ? stripReasoningParts(messagesWithoutSpec)
+ : messagesWithoutSpec
let modelMessages = await convertToModelMessages(messagesToConvert)
@@ -113,22 +114,7 @@ export async function createEphemeralChatStreamResponse(
})
)
- const responseMessages = (await result.response).messages
- if (responseMessages && responseMessages.length > 0) {
- const lastUserMessage = [...modelMessages]
- .reverse()
- .find(msg => msg.role === 'user')
- const messagesForQuestions = lastUserMessage
- ? [lastUserMessage, ...responseMessages]
- : responseMessages
- await streamRelatedQuestions(
- writer,
- messagesForQuestions,
- abortSignal,
- parentTraceId,
- model
- )
- }
+ await result.response
} finally {
if (langfuse) {
await langfuse.flushAsync()
diff --git a/lib/streaming/helpers/stream-related-questions.ts b/lib/streaming/helpers/stream-related-questions.ts
deleted file mode 100644
index 401b4672..00000000
--- a/lib/streaming/helpers/stream-related-questions.ts
+++ /dev/null
@@ -1,107 +0,0 @@
-import { ModelMessage, UIMessageStreamWriter } from 'ai'
-
-import { createRelatedQuestionsStream } from '@/lib/agents/generate-related-questions'
-import { generateId } from '@/lib/db/schema'
-import { relatedSchema } from '@/lib/schema/related'
-import { Model } from '@/lib/types/models'
-
-/**
- * Generates and streams related questions if there are tool calls in the response
- */
-export async function streamRelatedQuestions(
- writer: UIMessageStreamWriter,
- messages: ModelMessage[],
- abortSignal?: AbortSignal,
- parentTraceId?: string,
- localSelectedModel?: Model
-): Promise<{
- questionPartId?: string
- questions?: Array<{ question: string }>
-}> {
- // Check if the last message has tool calls
- const lastMessage = messages[messages.length - 1]
- if (!lastMessage || lastMessage.role !== 'assistant') {
- return {}
- }
-
- const questionPartId = generateId()
-
- try {
- // Write loading state
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: { status: 'loading' }
- })
-
- const relatedQuestionsResult = createRelatedQuestionsStream(
- messages,
- abortSignal,
- parentTraceId,
- localSelectedModel
- )
-
- const collectedQuestions: Array<{ question: string }> = []
-
- for await (const question of relatedQuestionsResult.elementStream) {
- if (!question || typeof question.question !== 'string') {
- continue
- }
-
- collectedQuestions.push(question)
-
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: {
- status: 'streaming',
- questions: [...collectedQuestions]
- }
- })
- }
-
- let finalQuestions = collectedQuestions
-
- try {
- const completedQuestions = await relatedQuestionsResult.output
- const parsedQuestions = relatedSchema.safeParse(completedQuestions)
-
- if (parsedQuestions.success) {
- finalQuestions = parsedQuestions.data
- } else if (Array.isArray(completedQuestions)) {
- finalQuestions = completedQuestions
- console.warn(
- 'Related questions validation failed:',
- parsedQuestions.error
- )
- }
- } catch (error) {
- console.warn('Error retrieving final related questions object:', error)
- }
-
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: {
- status: 'success',
- questions: finalQuestions
- }
- })
-
- return {
- questionPartId,
- questions: finalQuestions
- }
- } catch (error) {
- console.error('Error generating related questions:', error)
-
- // Write error state
- writer.write({
- type: 'data-relatedQuestions',
- id: questionPartId,
- data: { status: 'error' }
- })
-
- return { questionPartId }
- }
-}
diff --git a/lib/streaming/helpers/strip-spec-from-messages.ts b/lib/streaming/helpers/strip-spec-from-messages.ts
new file mode 100644
index 00000000..bfe4e5c9
--- /dev/null
+++ b/lib/streaming/helpers/strip-spec-from-messages.ts
@@ -0,0 +1,29 @@
+import { UIMessage } from 'ai'
+
+import { stripSpecBlocks } from '@/lib/render/strip-spec-blocks'
+
+/**
+ * Strips ```spec fenced blocks from assistant message text parts
+ * before converting to model messages. This prevents prior spec
+ * payloads from being fed back into the model on subsequent turns,
+ * wasting context window budget.
+ */
+export function stripSpecFromMessages(messages: UIMessage[]): UIMessage[] {
+ return messages.map(msg => {
+ if (msg.role !== 'assistant' || !msg.parts) {
+ return msg
+ }
+
+ const parts = msg.parts.map(part => {
+ if (part.type === 'text' && typeof part.text === 'string') {
+ const stripped = stripSpecBlocks(part.text)
+ if (stripped !== part.text) {
+ return { ...part, text: stripped }
+ }
+ }
+ return part
+ })
+
+ return { ...msg, parts }
+ })
+}
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index ab3d2975..95c10959 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -25,25 +25,10 @@ export type UIMessage<
TTools = UITools
> = AIMessage
-export interface RelatedQuestionsData {
- status: 'loading' | 'streaming' | 'success' | 'error'
- questions?: Array<{ question: string }>
-}
-
export type UIDataTypes = {
sources?: any[]
- relatedQuestions?: RelatedQuestionsData
}
-// Data part types for DataSection
-export type DataRelatedQuestionsPart = {
- type: 'data-relatedQuestions'
- id?: string
- data: RelatedQuestionsData
-}
-
-export type DataPart = DataRelatedQuestionsPart
-
// Create todo tools instance for type inference
const todoTools = createTodoTools()
diff --git a/package.json b/package.json
index fcb07393..ed4bd3ff 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,8 @@
"@ai-sdk/openai": "^3.0.39",
"@ai-sdk/react": "^3.0.113",
"@aws-sdk/client-s3": "^3.850.0",
+ "@json-render/core": "^0.16.0",
+ "@json-render/react": "^0.16.0",
"@opentelemetry/api-logs": "0.57.2",
"@opentelemetry/instrumentation": "0.57.2",
"@opentelemetry/sdk-logs": "0.57.2",
@@ -82,7 +84,7 @@
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "zod": "^4.0.5"
+ "zod": "^4.3.6"
},
"devDependencies": {
"@tailwindcss/postcss": "^4.1.11",