Skip to content

Commit 4fecf64

Browse files
committed
fix(chat): aggregate text across all steps and instruct model to synthesize tool findings
1 parent 3217f0f commit 4fecf64

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export const POST: APIRoute = async ({ request }) => {
153153
const result = streamText({
154154
model,
155155
system:
156-
"You are CodingCat.dev's intelligent content assistant. Answer user queries by searching and exploring the Sanity Content Lake dataset using your tools (initial_context, groq_query, schema_explorer, array_field_reader). Always ground your answers in the retrieved content (articles, podcasts, authors, transcripts) and cite relevant URLs or titles.",
156+
"You are CodingCat.dev's intelligent content assistant. Answer user queries by searching and exploring the Sanity Content Lake dataset using your tools (initial_context, groq_query, schema_explorer, array_field_reader). In each step, you can call tools to query the dataset. Once you obtain the results from your tool calls, you MUST synthesize your findings and generate a helpful, complete text answer grounded in the retrieved content (articles, podcasts, authors, transcripts) citing relevant titles or URLs.",
157157
messages: coreMessages,
158158
tools,
159159
stopWhen: isStepCount(10),
@@ -177,14 +177,21 @@ export const POST: APIRoute = async ({ request }) => {
177177
}
178178
}
179179
if (!hasOutput) {
180-
// Fallback: check full text from result in case textStream did not emit intermediate text
181-
const fullText = await result.text;
182-
if (fullText) {
183-
await writer.write(encoder.encode(fullText));
180+
// Check all steps to see if text was produced across steps
181+
const steps = await result.steps;
182+
const allText = steps.map((s) => s.text).filter(Boolean).join("\n\n");
183+
if (allText) {
184+
await writer.write(encoder.encode(allText));
184185
} else {
186+
const debugInfo = steps
187+
.map(
188+
(s, idx) =>
189+
`Step ${idx + 1}: finish=${s.finishReason}, tools=${s.toolCalls?.map((t) => t.toolName).join(",") || "none"}`,
190+
)
191+
.join("; ");
185192
await writer.write(
186193
encoder.encode(
187-
"I searched the CodingCat.dev dataset for your query, but did not find any matching results.",
194+
`I queried the CodingCat.dev Sanity content lake (${steps.length} steps: ${debugInfo}), but no final text was emitted.`,
188195
),
189196
);
190197
}

0 commit comments

Comments
 (0)