Skip to content

think: expose additive TurnConfig stopWhen conditions#1561

Merged
whoiskatrin merged 1 commit into
mainfrom
feat/think-stop-when
May 19, 2026
Merged

think: expose additive TurnConfig stopWhen conditions#1561
whoiskatrin merged 1 commit into
mainfrom
feat/think-stop-when

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

Summary

Expose additive AI SDK stopWhen conditions through Think's TurnConfig.

Think already owns the streamText() call and maps maxSteps to:

stopWhen: stepCountIs(maxSteps)

That covers step-count limits, but it leaves no way for Think subclasses to express other AI SDK stop conditions such as:

hasToolCall("finalAnswer")

This PR adds:

beforeTurn() {
  return {
    stopWhen: hasToolCall("finalAnswer")
  };
}

Behavior

Custom stopWhen conditions are additive.

Think now sends both:

  • its existing stepCountIs(maxSteps) safety bound
  • any custom TurnConfig.stopWhen condition(s)

to the AI SDK, so the loop ends when either matches.

That means callers can stop early after a designated tool call without silently removing Think's existing runaway-loop guardrail.

API

interface TurnConfig {
  maxSteps?: number;
  stopWhen?: StopCondition | StopCondition[];
}

Example:

import { hasToolCall } from "ai";

beforeTurn() {
  return {
    stopWhen: hasToolCall("finalAnswer")
  };
}

StopCondition is also re-exported from @cloudflare/think alongside the other AI SDK lifecycle-facing types.

Notes on extensions

stopWhen is function-valued, so it is supported from a Think subclass's beforeTurn() hook, but not from sandboxed extension beforeTurn hooks over RPC.

The docs call this out explicitly.

Tests

Added regression coverage verifying that:

  • a Think turn can return stopWhen: hasToolCall("echo")
  • the tool executes
  • Think does not start the follow-up continuation model step after that matching tool call

Documentation

Updated:

  • packages/think/README.md
  • docs/think/lifecycle-hooks.md

to document:

  • TurnConfig.stopWhen
  • composition with maxSteps
  • hasToolCall(...) usage
  • subclass-only support for function-valued stop conditions

Validation

Passed:

npm run build --workspace @cloudflare/think
npm run build --workspace agents
npx vitest --run -c packages/think/src/tests/vitest.config.ts packages/think/src/tests/hooks.test.ts

Focused Think hook suite:

66 passed

Also ran:

npm run check

Formatting, exports, and lint passed. Repo-wide typecheck could not complete in this environment because tsgo is unavailable:

/bin/sh: tsgo: command not found

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 19, 2026

🦋 Changeset detected

Latest commit: fb5745b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/think Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 19, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1561

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1561

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1561

hono-agents

npm i https://pkg.pr.new/hono-agents@1561

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1561

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1561

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1561

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1561

commit: fb5745b

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@whoiskatrin whoiskatrin marked this pull request as ready for review May 19, 2026 21:14
@whoiskatrin whoiskatrin merged commit 831ba1d into main May 19, 2026
4 checks passed
@whoiskatrin whoiskatrin deleted the feat/think-stop-when branch May 19, 2026 21:19
@github-actions github-actions Bot mentioned this pull request May 19, 2026
cjol pushed a commit that referenced this pull request May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants