Skip to content

Commit ceca36a

Browse files
committed
Continue the turn if only <think> tags were included in the response
1 parent ae056a7 commit ceca36a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,17 @@ export const runAgentStep = async (
396396
call.toolName === 'task_completed' || call.toolName === 'end_turn',
397397
)
398398

399+
// If the response is only <think>...</think> tags with no other non-whitespace content,
400+
// the model was just thinking and should continue rather than end its turn.
401+
const responseWithoutThinkTags = fullResponse
402+
.replace(/<think>[\s\S]*?<\/think>/g, '')
403+
.replace(/<think>[\s\S]*$/, '')
404+
.trim()
405+
const isThinkOnly =
406+
hasNoToolResults &&
407+
responseWithoutThinkTags.length === 0 &&
408+
fullResponse.trim().length > 0
409+
399410
// If the agent has the task_completed tool, it must be called to end its turn.
400411
const requiresExplicitCompletion =
401412
agentTemplate.toolNames.includes('task_completed')
@@ -408,7 +419,8 @@ export const runAgentStep = async (
408419
shouldEndTurn = hasTaskCompleted
409420
} else {
410421
// For other models, also end turn when there are no tool calls
411-
shouldEndTurn = hasTaskCompleted || hasNoToolResults
422+
// Exception: if the response is only <think> tags, continue the turn
423+
shouldEndTurn = hasTaskCompleted || (hasNoToolResults && !isThinkOnly)
412424
}
413425

414426
agentState = {

0 commit comments

Comments
 (0)