Skip to content

An AI-powered search engine with a generative UI

fix: Refactor inquire and querySuggestor functions

Yoshiki Miura046b4142d8d8
diff --git a/lib/agents/inquire.tsx b/lib/agents/inquire.tsx
index 67ba359f..55953b11 100644
--- a/lib/agents/inquire.tsx
+++ b/lib/agents/inquire.tsx
@@ -13,7 +13,8 @@ export async function inquire(
uiStream.update(<Copilot inquiry={objectStream.value} />)
let finalInquiry: PartialInquiry = {}
- await streamObject({
+
+ const result = await streamObject({
model: getModel(model),
system: `As a professional web researcher, your role is to deepen your understanding of the user's input by conducting further inquiries when necessary.
After receiving an initial response from the user, carefully assess whether additional questions are absolutely essential to provide a comprehensive and accurate answer. Only proceed with further inquiries if the available information is insufficient or ambiguous.
@@ -55,17 +56,17 @@ export async function inquire(
messages,
schema: inquirySchema
})
- .then(async result => {
- for await (const obj of result.partialObjectStream) {
- if (obj) {
- objectStream.update(obj)
- finalInquiry = obj
- }
+
+ try {
+ for await (const obj of result.partialObjectStream) {
+ if (obj) {
+ objectStream.update(obj)
+ finalInquiry = obj
}
- })
- .finally(() => {
- objectStream.done()
- })
+ }
+ } finally {
+ objectStream.done()
+ }
return finalInquiry
}
diff --git a/lib/agents/query-suggestor.tsx b/lib/agents/query-suggestor.tsx
index 760f5f90..79d57482 100644
--- a/lib/agents/query-suggestor.tsx
+++ b/lib/agents/query-suggestor.tsx
@@ -20,7 +20,7 @@ export async function querySuggestor(
}) as CoreMessage[]
let finalRelatedQueries: PartialRelated = {}
- await streamObject({
+ const result = await streamObject({
model: getModel(model),
system: `As a professional web researcher, your task is to generate a set of three queries that explore the subject matter more deeply, building upon the initial query and the information uncovered in its search results.
@@ -31,17 +31,13 @@ export async function querySuggestor(
messages: lastMessages,
schema: relatedSchema
})
- .then(async result => {
- for await (const obj of result.partialObjectStream) {
- if (obj.items) {
- objectStream.update(obj)
- finalRelatedQueries = obj
- }
- }
- })
- .finally(() => {
- objectStream.done()
- })
+
+ for await (const obj of result.partialObjectStream) {
+ if (obj.items) {
+ objectStream.update(obj)
+ finalRelatedQueries = obj
+ }
+ }
return finalRelatedQueries
}