Skip to content

Commit 54a5da1

Browse files
committed
fix(chat): return clean unwrapped objects from search_content and groq_query
1 parent 0a66126 commit 54a5da1

1 file changed

Lines changed: 37 additions & 75 deletions

File tree

‎apps/site/src/pages/api/chat.ts‎

Lines changed: 37 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -168,54 +168,40 @@ export const POST: APIRoute = async ({ request }) => {
168168
}),
169169
execute: async ({ query, type }) => {
170170
const filterType = type && type !== "all" ? type : null;
171+
let results: any[] = [];
171172
try {
172-
const results = await sanityClient.fetch(semanticSearchQuery, {
173+
const res = await sanityClient.fetch(semanticSearchQuery, {
173174
searchTerm: query,
174175
type: filterType,
175176
});
176-
if (Array.isArray(results) && results.length > 0) {
177-
return {
178-
content: [
179-
{
180-
type: "text",
181-
text: JSON.stringify({ results }),
182-
},
183-
],
184-
};
177+
if (Array.isArray(res) && res.length > 0) {
178+
results = res;
185179
}
186180
} catch (semanticErr) {
187181
console.warn("[search_content semantic error, falling back]", semanticErr);
188182
}
189183

190-
try {
191-
const fallback = await sanityClient.fetch(textSearchFallbackQuery, {
192-
searchTerm: query,
193-
type: filterType,
194-
});
195-
return {
196-
content: [
197-
{
198-
type: "text",
199-
text: JSON.stringify({ results: fallback || [] }),
200-
},
201-
],
202-
};
203-
} catch (fallbackErr) {
204-
return {
205-
content: [
206-
{
207-
type: "text",
208-
text: JSON.stringify({
209-
error:
210-
fallbackErr instanceof Error
211-
? fallbackErr.message
212-
: String(fallbackErr),
213-
}),
214-
},
215-
],
216-
isError: true,
217-
};
184+
if (!results.length) {
185+
try {
186+
const fallback = await sanityClient.fetch(textSearchFallbackQuery, {
187+
searchTerm: query,
188+
type: filterType,
189+
});
190+
if (Array.isArray(fallback)) {
191+
results = fallback;
192+
}
193+
} catch (fallbackErr) {
194+
console.error("[search_content text error]", fallbackErr);
195+
}
218196
}
197+
198+
return results.slice(0, 10).map((item: any) => ({
199+
title: item.title,
200+
type: item._type,
201+
slug: item.slug,
202+
url: `/${item._type === "podcast" ? "podcast" : item._type === "post" ? "post" : item._type}/${item.slug}`,
203+
excerpt: item.excerpt || item.transcriptSummary || "",
204+
}));
219205
},
220206
}),
221207
groq_query: {
@@ -236,7 +222,7 @@ export const POST: APIRoute = async ({ request }) => {
236222
? JSON.parse(parsed.content[0].text)?.result
237223
: null);
238224
if (Array.isArray(results) && results.length > 0) {
239-
return mcpResult;
225+
return results;
240226
}
241227
} catch {}
242228

@@ -259,14 +245,7 @@ export const POST: APIRoute = async ({ request }) => {
259245

260246
const directResult = await sanityClient.fetch(cleanQuery);
261247
if (Array.isArray(directResult) && directResult.length > 0) {
262-
return {
263-
content: [
264-
{
265-
type: "text",
266-
text: JSON.stringify({ result: directResult }),
267-
},
268-
],
269-
};
248+
return directResult;
270249
}
271250

272251
// If still empty and query was a search for a keyword, extract keyword and run semantic search
@@ -280,39 +259,22 @@ export const POST: APIRoute = async ({ request }) => {
280259
type: filterType,
281260
});
282261
if (Array.isArray(semanticResults) && semanticResults.length > 0) {
283-
return {
284-
content: [
285-
{
286-
type: "text",
287-
text: JSON.stringify({ result: semanticResults }),
288-
},
289-
],
290-
};
262+
return semanticResults.slice(0, 10).map((item: any) => ({
263+
title: item.title,
264+
type: item._type,
265+
slug: item.slug,
266+
excerpt: item.excerpt || "",
267+
}));
291268
}
292269
}
293270

294-
return {
295-
content: [
296-
{
297-
type: "text",
298-
text: JSON.stringify({ result: directResult || [] }),
299-
},
300-
],
301-
};
271+
return directResult || [];
302272
} catch (directErr) {
303273
return {
304-
content: [
305-
{
306-
type: "text",
307-
text: JSON.stringify({
308-
error:
309-
directErr instanceof Error
310-
? directErr.message
311-
: String(directErr),
312-
}),
313-
},
314-
],
315-
isError: true,
274+
error:
275+
directErr instanceof Error
276+
? directErr.message
277+
: String(directErr),
316278
};
317279
}
318280
},

0 commit comments

Comments
 (0)