Repository navigation
fix(react-core): let a custom catch-all renderer return null - #6942
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthrough
ChangesNullable custom renderer support
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to Custom catch-all tool renderers can now return null to suppress selected output, with matching runtime, type-level, and documentation coverage. The change is ready to merge. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
@copilotkit/a2ui-renderer
@copilotkit/agentcore-runner
@copilotkit/angular
@copilotkit/channels
@copilotkit/channels-core
@copilotkit/channels-discord
@copilotkit/channels-intelligence
@copilotkit/channels-slack
@copilotkit/channels-teams
@copilotkit/channels-telegram
@copilotkit/channels-ui
@copilotkit/channels-whatsapp
@copilotkit/core
@copilotkit/react-core
@copilotkit/react-native
@copilotkit/react-textarea
@copilotkit/react-ui
@copilotkit/runtime
@copilotkit/runtime-client-gql
@copilotkit/sdk-js
@copilotkit/shared
@copilotkit/sqlite-runner
@copilotkit/voice
@copilotkit/vue
@copilotkit/web-components
@copilotkit/web-inspector
commit: |
|
Size Change: 0 B Total Size: 1.65 MB ℹ️ View Unchanged
|
`useDefaultRenderTool`'s `render` was typed to return `React.ReactElement`, so a caller who wanted to render only some tool calls could not return `null` to suppress the built-in default for the rest. The value already flowed through correctly at runtime; only the type rejected it. Widen the public `render` return type, and the wrapper local that carries the user's value, to `React.ReactElement | null`. The `as unknown as` cast into `useRenderTool` stays, because `useRenderTool` still requires a `ReactElement` return on main; PR #6533 widens that hook, after which the cast can be tightened. Guarded by a `.test-d.ts` assertion rather than a runtime test: types are erased, so a null-returning render forwards identically before and after the widening and a runtime test would assert nothing. Extracted from #5509, which is otherwise stale. Co-authored-by: Atai Barkai <[email protected]>
2058dbc to
82cd7c0
Compare
Production Digest-Pinning AuditAll 42 services digest-pinned. Run 2026-09-08 14:39:58 PDT — 0 finding(s). |
Summary
useDefaultRenderTool'srenderwas typed to returnReact.ReactElement. A caller who wants to render only some tool calls therefore could not returnnullto suppress the built-in default for the rest — the value flowed through correctly at runtime, but the type rejected it.This widens the public
renderreturn type, and the wrapper local that carries the user's value, toReact.ReactElement | null.The reference page hand-writes the same signature, so it is updated to match, with one behavior bullet describing what
nulldoes.Scope, and its relationship to #6533
#6533 already widens the same return type to
React.ReactElement | nullindefineToolCallRenderer.tsanduse-render-tool.tsx. It does not touchuse-default-render-tool.tsx, which is the remaining gap and the whole of this PR. There is no file overlap, so the two can land in either order.A structural sweep of
react-core/src/v2for renders still typed=> React.ReactElementwith no| nullconfirms this leaves nothing behind on this surface:The
as unknown ascast intouseRenderToolis deliberately left in place:useRenderToolstill requires aReactElementreturn onmain(verified again after the rebase —use-render-tool.tsx:41). Once #6533 lands, that cast can be tightened. The bridge comment is updated to say so.DefaultToolCallRenderer's own return type staysReact.ReactElement— the built-in default always renders an element.The Vue counterpart needs no equivalent change: its
renderalready returnsVNodeChild, which admitsnull, andreference/vue/hooks/useDefaultRenderTool.mdxalready matches.Why the guard is a type test, not a runtime test
TypeScript types are erased, so a
null-returning render forwards identically before and after the widening. The runtime test passes against un-widened source, which makes it worthless as a guard for this change. So the real guard isuse-default-render-tool-types.test-d.ts, using theexpectTypeOf+toEqualTypeOfconvention already documented inv2/__tests__/headless-type-exports.test-d.ts.toEqualTypeOfis required rather than assignability: a function returningReactElementis assignable to one returningReactElement | null, so an assignability check would pass against the un-widened type and assert nothing.The
.test-d.tsbasename is outside vitest'sincludeglobs, so nothing there executes;tsc --noEmit(check-types) is what reads it. Confirmed on this base:The runtime test is kept as well, since it still covers prop adaptation and forwarding.
Testing
All numbers below were re-measured after the rebase onto
main(42494df).Mutation check of the type guard — revert the widening in the source, confirm the guard goes red:
The guard fails when the widening is reverted, and the two new errors are exactly the guard plus the runtime test's own use of it. Nothing else moves.
Typecheck (
tsc -p packages/react-core --noEmit) — error set byte-identical to pristineorigin/mainin the same worktree, none in the touched files:The 63 are pre-existing worktree noise:
react-coreresolves@copilotkit/coreand@copilotkit/sharedfrom a sibling checkout'sdist, so unrelated exports read as missing. They are present on pristineorigin/mainin the same worktree, which is what the diff above shows.Target test file:
Broader
src/v2/hooks+src/v2/types— failure counts identical to pristineorigin/mainin the same worktree, plus exactly the one new passing test:Lint:
oxlint packages/react-core/src/v2/hooks/—Found 62 warnings and 0 errors.(all pre-existing exhaustive-deps warnings, none in the touched files).Formatting:
oxfmt --checkon the three source/test files —All matched files use the correct format.oxfmtdoes not process.mdx, so the reference page is out of its scope.Public-API manifest: no regeneration needed —
scripts/release/public-api/manifest.v1.jsonrecords no type signatures and does not mentionuseDefaultRenderTool(grep -c ReactElement→0).Provenance
Extracted from #5509 (@ataibarkai), which is 3915 commits behind
mainand being closed. That PR also changeddefineToolCallRenderer's schema default fromdef.name === "*" && !def.args ? z.any() : def.argstodef.args ?? z.any(). That change is deliberately not carried here — it alters runtime behavior for named renderers declared withoutargs(fromargs: undefinedtoargs: z.any()) and deserves its own PR and its own verification.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
nullto suppress output when no UI should be displayed.Documentation
useDefaultRenderToolguidance to describe null-return behavior and selectively rendering tool calls.Tests