Skip to content

An AI-powered search engine with a generative UI

refactor: Improve related search form rendering and filtering

Yoshiki Miura9e5793a53bce
diff --git a/app/actions.tsx b/app/actions.tsx
index d0466fb0..781033cd 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -156,7 +156,7 @@ async function submit(
while (
useSpecificAPI
? toolOutputs.length === 0 && answer.length === 0 && !errorOccurred
- : stopReason !== 'stop' && !errorOccurred
+ : (stopReason !== 'stop' || answer.length === 0) && !errorOccurred
) {
// Search the web and generate the answer
const { fullResponse, hasError, toolResponses, finishReason } =
diff --git a/components/search-related.tsx b/components/search-related.tsx
index ca44bfcb..840f1bdc 100644
--- a/components/search-related.tsx
+++ b/components/search-related.tsx
@@ -61,22 +61,26 @@ export const SearchRelated: React.FC<SearchRelatedProps> = ({
return related ? (
<Section title="Related" separator={true}>
<form onSubmit={handleSubmit} className="flex flex-wrap">
- {related?.items
- ?.filter(item => item?.query !== '')
- .map((item, index) => (
- <div className="flex items-start w-full" key={index}>
- <ArrowRight className="h-4 w-4 mr-2 mt-1 flex-shrink-0 text-accent-foreground/50" />
- <Button
- variant="link"
- className="flex-1 justify-start px-0 py-1 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
- type="submit"
- name={'related_query'}
- value={item?.query}
- >
- {item?.query}
- </Button>
- </div>
- ))}
+ {Array.isArray(related.items) ? (
+ related.items
+ ?.filter(item => item?.query !== '')
+ .map((item, index) => (
+ <div className="flex items-start w-full" key={index}>
+ <ArrowRight className="h-4 w-4 mr-2 mt-1 flex-shrink-0 text-accent-foreground/50" />
+ <Button
+ variant="link"
+ className="flex-1 justify-start px-0 py-1 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"
+ type="submit"
+ name={'related_query'}
+ value={item?.query}
+ >
+ {item?.query}
+ </Button>
+ </div>
+ ))
+ ) : (
+ <div>Not an array</div>
+ )}
</form>
</Section>
) : error ? null : (
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index 78d1b3a4..f6fedbf7 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -18,6 +18,7 @@ export async function researcher(
const useOllamaProvider = !!(
process.env.OLLAMA_MODEL && process.env.OLLAMA_BASE_URL
)
+ const useAnthropicProvider = !!process.env.ANTHROPIC_API_KEY
if (useOllamaProvider) {
processedMessages = transformToolMessages(messages)
}
@@ -64,7 +65,7 @@ export async function researcher(
}
const hasToolResult = messages.some(message => message.role === 'tool')
- if (hasToolResult) {
+ if (!useAnthropicProvider || hasToolResult) {
uiStream.append(answerSection)
}
@@ -76,10 +77,10 @@ export async function researcher(
case 'text-delta':
if (delta.textDelta) {
fullResponse += delta.textDelta
- if (hasToolResult) {
- streambleAnswer.update(fullResponse)
- } else {
+ if (useAnthropicProvider && !hasToolResult) {
streamableText.update(fullResponse)
+ } else {
+ streambleAnswer.update(fullResponse)
}
}
break