fix(web): stop consultation thinking from pinching the answer

Disable provider thinking so Flash CoT cannot fill max_tokens, raise the
spoken budget to 16384, emit a server-owned step tree, and continue once
when the body ends on length.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jesse_Chen
2026-08-22 21:19:00 +08:00
co-authored by Cursor
parent d2a5f0f5bf
commit 0ca7da997f
26 changed files with 1013 additions and 88 deletions
+39 -3
View File
@@ -36,6 +36,7 @@ import { createServerSupabaseClient } from "@/lib/supabase/server";
import { streamTextResponse } from "@/lib/stream-text-response";
import { streamAgentResponse } from "@/lib/stream-agent-response";
import type { AgentExecutionReceipt, WorkflowReceipt } from "@/lib/consultation-agent-events";
import { consultationContinuePrompt, type PublicThinkingSection } from "@/lib/consultation-thinking-plan";
import {
AGENT_MAX_STEPS,
AGENT_TIMEOUT_MS,
@@ -501,6 +502,7 @@ export async function POST(request: Request) {
workflowReceipt: WorkflowReceipt,
agentExecutionReceipt?: AgentExecutionReceipt,
thinkingText?: string,
thinkingSections?: PublicThinkingSection[],
): Promise<AgentSettlementResult> {
try {
const reply = parseAgentReply(
@@ -513,6 +515,7 @@ export async function POST(request: Request) {
role: "assistant" as const,
text: reply.text,
...(persistedThinking ? { thinkingText: persistedThinking } : {}),
...(thinkingSections?.length ? { thinkingSections } : {}),
techniqueTruth,
workflowReceipt,
...(agentExecutionReceipt ? { agentExecutionReceipt } : {}),
@@ -724,6 +727,15 @@ export async function POST(request: Request) {
usages.push(retried.totalUsage);
return retried.fullStream;
};
const continueAfterLength = async (output: string) => {
const continued = await agent.stream([
...baseMessages,
{ role: "assistant" as const, content: output },
{ role: "user" as const, content: consultationContinuePrompt(output) },
], streamOptions);
usages.push(continued.totalUsage);
return continued.fullStream;
};
const executionReceipt = (): AgentExecutionReceipt => ({
runId: requestId,
runtime: "mastra-agentic",
@@ -748,6 +760,7 @@ export async function POST(request: Request) {
stream: result.fullStream,
requireTool: false,
retryForAnswer,
continueAfterLength,
continueAfterDisconnect: true,
transformText: createBirthTimeModeOutputGuard(
generalDailyContext ? "general_no_birth_time" : consultationMode,
@@ -758,13 +771,14 @@ export async function POST(request: Request) {
headers: { "x-jyotish-birth-time-mode": consultationMode },
onFirstActivity: markFirstActivity,
onFirstOutput: markFirstText,
onComplete: (output, agentExecutionReceipt, thinkingText) => settleRun(() => completeResponse(
onComplete: (output, agentExecutionReceipt, thinkingText, thinkingSections) => settleRun(() => completeResponse(
output,
mergeUsage(usages),
generalDailyContext ? "public-panchanga-only" : "not-applicable",
workflowReceipt,
agentExecutionReceipt,
thinkingText,
thinkingSections,
), undefined),
onError: (error) => settleRun(
cancel,
@@ -812,6 +826,15 @@ export async function POST(request: Request) {
usages.push(retried.totalUsage);
return retried.fullStream;
};
const continueAfterLength = async (output: string) => {
const continued = await agent.stream([
...baseMessages,
{ role: "assistant" as const, content: output },
{ role: "user" as const, content: consultationContinuePrompt(output) },
], streamOptions);
usages.push(continued.totalUsage);
return continued.fullStream;
};
const executionReceipt = (): AgentExecutionReceipt => ({
runId: requestId,
runtime: "mastra-agentic",
@@ -837,6 +860,7 @@ export async function POST(request: Request) {
requireTool: true,
retry,
retryForAnswer,
continueAfterLength,
continueAfterDisconnect: true,
transformText: createBirthTimeModeOutputGuard(consultationMode, false),
toolStatus: () => workflowStatus(state.workflowReceipt?.status),
@@ -844,13 +868,14 @@ export async function POST(request: Request) {
headers: { "x-jyotish-birth-time-mode": consultationMode },
onFirstActivity: markFirstActivity,
onFirstOutput: markFirstText,
onComplete: (output, agentExecutionReceipt, thinkingText) => settleRun(() => completeResponse(
onComplete: (output, agentExecutionReceipt, thinkingText, thinkingSections) => settleRun(() => completeResponse(
output,
mergeUsage(usages),
state.techniqueTruth ?? "declared-window",
state.workflowReceipt ?? workflowReceipt,
agentExecutionReceipt,
thinkingText,
thinkingSections,
), undefined),
onError: (error) => settleRun(
cancel,
@@ -900,6 +925,15 @@ export async function POST(request: Request) {
usages.push(retried.totalUsage);
return retried.fullStream;
};
const continueAfterLength = async (output: string) => {
const continued = await agent.stream([
...baseMessages,
{ role: "assistant" as const, content: output },
{ role: "user" as const, content: consultationContinuePrompt(output) },
], streamOptions);
usages.push(continued.totalUsage);
return continued.fullStream;
};
const executionReceipt = (): AgentExecutionReceipt => ({
runId: requestId,
runtime: "mastra-agentic",
@@ -925,6 +959,7 @@ export async function POST(request: Request) {
requireTool: true,
retry,
retryForAnswer,
continueAfterLength,
continueAfterDisconnect: true,
transformText: (text) => createBirthTimeModeOutputGuard(
consultationMode,
@@ -935,13 +970,14 @@ export async function POST(request: Request) {
headers: { "x-jyotish-birth-time-mode": consultationMode },
onFirstActivity: markFirstActivity,
onFirstOutput: markFirstText,
onComplete: (output, agentExecutionReceipt, thinkingText) => settleRun(() => completeResponse(
onComplete: (output, agentExecutionReceipt, thinkingText, thinkingSections) => settleRun(() => completeResponse(
output,
mergeUsage(usages),
state.techniqueTruth ?? "unknown",
state.workflowReceipt ?? workflowReceipt,
agentExecutionReceipt,
thinkingText,
thinkingSections,
), undefined),
onError: (error) => settleRun(
cancel,