feat(think): tool-call lifecycle hook follow-ups (#1343)#1823
Merged
Conversation
Address follow-ups 1, 2, 4, and 5 from #1343: - Preserve preliminary streaming through `beforeToolCall` for async generator `execute` tools, while keeping scalar tools free of synthetic `preliminary` chunks. Extract the shared decision logic into `_resolveToolCallDecision`. - Type `ToolCallResultContext.output` (and fix `ToolCallContext.input`) per tool when an explicit `TOOLS` generic is passed: narrowing on `ctx.toolName` now narrows input/output. Default `ToolSet` behavior is unchanged. Add `tool-call-types.test-d.ts`. - Add a raw `needsApproval` tool + `beforeToolCall` dual-gate ordering test. - Add an `execute` invocation counter to the test tool and assert block/substitute never run it. Co-authored-by: Cursor <cursoragent@cursor.com>
🦋 Changeset detectedLatest commit: c2cfe43 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
…of streaming tools Document and annotate that blocking or substituting an `async function*` tool surfaces one extra `preliminary: true` tool-result chunk to observation hooks (e.g. onChunk). This is inherent: the wrapper must commit to an AsyncIterable shape synchronously to preserve streaming on the execute path, and the AI SDK turns every yielded value into a preliminary + final. The model-visible final output is unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses follow-ups 1, 2, 4, and 5 from #1343.
Summary
(1) Preserve preliminary streaming through
beforeToolCall. Tools whoseexecuteis an async generator (async function* execute(...)— the canonical AI SDK streaming form) now stream their preliminary tool-results to the model even though Think wrapsexecuteto consultbeforeToolCallfirst. The streaming wrapper is itself anasync function*that awaits the decision thenyield*s every chunk through. Non-streaming tools keep a scalar wrapper, so they never emit a syntheticpreliminarychunk (the downside the issue flagged for the "always return AsyncIterable" approach). The non-canonicalasync () => makeIterator()form (aPromise<AsyncIterable>, which does not stream even in the raw AI SDK) still collapses to its last yielded value. Shared decision logic was extracted into_resolveToolCallDecision.(2) Per-tool typing for
output(and a latentinputfix). When an explicitTOOLSgeneric is passed, narrowing onctx.toolNamenow narrowsctx.inputonbeforeToolCalland — new —ctx.outputonafterToolCall's success branch to that tool's inferred output type. The AI SDK'sDynamicToolCallarm (input: unknown) was previously collapsing the union tounknown, so eveninputdidn't narrow despite the docs' claim; the contexts now distribute overkeyof TOOLS(dropping the dynamic arm) when keys are literal. DefaultToolSetbehavior is unchanged. Addedtool-call-types.test-d.ts.(4)
needsApproval×beforeToolCallordering test. Added a raw AI SDKneedsApprovaltool (not a Think Action) plus tests proving the dual-gate ordering: the approval gate prompts first; after approval,beforeToolCallis still the outer gate (block →executenever runs; allow → runs once).(5) Counter-based "block prevents execute" test. Added an
executeinvocation counter to the test tool and assertions thatblock/substitutenever run it (direct proof rather than the prior indirect output check).Follow-up #2's
ToolCallDecision.inputhalf and #3 (skip-wrapping optimization) are intentionally not included — see the remaining scope in #1343.Test plan
tsgotypecheck (main + tests tsconfigs)pnpm run build(declaration generation with the new conditional types)Made with Cursor