Skip to content

Commit f490490

Browse files
committed
fix: skip compiled children-blocks in chat UI widgets
Guard Messages, Message, Part, Interrupts, Interrupt, ChatInput, and ToolApproval so tagged @{ } children render instead of being invoked as render props.
1 parent bc1d07d commit f490490

6 files changed

Lines changed: 159 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@octanejs/tanstack-ai": patch
3+
---
4+
5+
Skip compiled `.tsrx` children-blocks when chat UI widgets decide between a render-prop child and ordinary template children.

‎packages/tanstack-ai/src/chat-ui/chat-input.tsrx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useState } from 'octane';
1+
import { isChildrenBlock, useRef, useState } from 'octane';
22
import { useChatContext } from './chat.tsrx';
33
import type { OctaneNode, RefObject } from 'octane';
44

@@ -90,7 +90,9 @@ export function ChatInput({
9090
// Render prop pattern
9191
if (children) {
9292
return <>
93-
{children(renderProps)}
93+
{typeof children === 'function' && !isChildrenBlock(children)
94+
? children(renderProps)
95+
: children}
9496
</>;
9597
}
9698

‎packages/tanstack-ai/src/chat-ui/create-ui.tsrx‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Bundlers replace this local declaration's guarded development check.
22
declare const process: { env: { NODE_ENV?: string } };
3-
import { createContext, memo, useContext } from 'octane';
3+
import { createContext, isChildrenBlock, memo, useContext } from 'octane';
44
import type { ComponentProps, ComponentType, Context, OctaneNode } from 'octane';
55
import {
66
automaticPartsForMessage,
@@ -469,9 +469,13 @@ function createChatUIImpl<
469469
const chat = useChatContext();
470470
const messages = readMessages(chat);
471471
const interrupts = readInterrupts(chat);
472-
if (children) return <>
473-
{children(messages)}
474-
</>;
472+
if (children) {
473+
return <>
474+
{typeof children === 'function' && !isChildrenBlock(children)
475+
? children(messages)
476+
: children}
477+
</>;
478+
}
475479
return <>
476480
{messages.map(
477481
(message) =>
@@ -523,9 +527,13 @@ function createChatUIImpl<
523527
interrupts,
524528
inlineToolNames: names,
525529
});
526-
if (children) return <>
527-
{children(selected.parts)}
528-
</>;
530+
if (children) {
531+
return <>
532+
{typeof children === 'function' && !isChildrenBlock(children)
533+
? children(selected.parts)
534+
: children}
535+
</>;
536+
}
529537
return <MessageRenderContext.Provider value={{ message, interrupts, inlineToolNames: names }}>
530538
<MessageComponent message={message} Parts={Parts} />
531539
</MessageRenderContext.Provider>;
@@ -629,7 +637,11 @@ function createChatUIImpl<
629637
}).parts[0];
630638
if (!selected) return null;
631639
return <PartContext.Provider value={selected}>
632-
{children ? (children(mixPart(selected))) : <SelectedPartInner selected={selected} />}
640+
{children
641+
? (typeof children === 'function' && !isChildrenBlock(children)
642+
? children(mixPart(selected))
643+
: children)
644+
: <SelectedPartInner selected={selected} />}
633645
</PartContext.Provider>;
634646
}
635647

@@ -646,9 +658,13 @@ function createChatUIImpl<
646658
interrupts: readInterrupts(chat),
647659
inlineToolNames,
648660
});
649-
if (children) return <>
650-
{children(selected.interrupts)}
651-
</>;
661+
if (children) {
662+
return <>
663+
{typeof children === 'function' && !isChildrenBlock(children)
664+
? children(selected.interrupts)
665+
: children}
666+
</>;
667+
}
652668
return <>
653669
{selected.interrupts.map(
654670
(interrupt) => <Interrupt key={interrupt.id} interrupt={interrupt} />,
@@ -686,7 +702,11 @@ function createChatUIImpl<
686702
children?: (mixed: ChatUIInterrupt & InterruptMixins<TOptions>) => OctaneNode;
687703
}) {
688704
return <InterruptContext.Provider value={interrupt}>
689-
{children ? (children(mixInterrupt(interrupt))) : <InterruptInner interrupt={interrupt} />}
705+
{children
706+
? (typeof children === 'function' && !isChildrenBlock(children)
707+
? children(mixInterrupt(interrupt))
708+
: children)
709+
: <InterruptInner interrupt={interrupt} />}
690710
</InterruptContext.Provider>;
691711
},
692712
);

‎packages/tanstack-ai/src/chat-ui/tool-approval.tsrx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { isChildrenBlock, type OctaneNode } from 'octane';
12
import { useChatContext } from './chat.tsrx';
2-
import type { OctaneNode } from 'octane';
33

44
export interface ToolApprovalProps {
55
/** Tool call ID */
@@ -91,7 +91,9 @@ export function ToolApproval({
9191
// Render prop pattern
9292
if (children) {
9393
return <>
94-
{children(renderProps)}
94+
{typeof children === 'function' && !isChildrenBlock(children)
95+
? children(renderProps)
96+
: children}
9597
</>;
9698
}
9799

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// @ts-nocheck
2+
import { Chat, ChatInput, ToolApproval, createChatUI } from '@octanejs/tanstack-ai/ui';
3+
4+
const options = { connection: { connect: async function* () {} } };
5+
function Message({ Parts }) @{ <article><Parts /></article> }
6+
function Layout({ Messages, Interrupts }) @{
7+
<>
8+
<Messages />
9+
<Interrupts />
10+
</>
11+
}
12+
function Text({ part }) @{ <span>{part.content as string}</span> }
13+
const UI = createChatUI(options, {
14+
components: { layout: Layout, message: Message },
15+
partsComponents: { text: Text, fallback: () => null },
16+
interruptsComponents: { generic: { fallback: ({ interrupt }) => <i>{interrupt.reason as string}</i> } },
17+
});
18+
19+
const message = { id: 'm1', role: 'assistant', parts: [{ type: 'text', content: 'Hello' }] };
20+
const interrupt = { kind: 'generic', id: 'i1', reason: 'choose' };
21+
const chat = {
22+
messages: [message],
23+
interrupts: [interrupt],
24+
queue: [],
25+
cancelQueued() {},
26+
};
27+
28+
export function ChatUIChildren() @{
29+
<main>
30+
<UI.Provider chat={chat}>
31+
<UI.Messages>
32+
<span id="messages-template">messages-template</span>
33+
</UI.Messages>
34+
<UI.Messages>
35+
{(messages) => <output id="messages-callback">{String(messages.length)}</output>}
36+
</UI.Messages>
37+
<UI.Message message={message}>
38+
<span id="message-template">message-template</span>
39+
</UI.Message>
40+
<UI.Message message={message}>
41+
{(parts) => <output id="message-callback">{String(parts.length)}</output>}
42+
</UI.Message>
43+
<UI.Part part={message.parts[0]}>
44+
<span id="part-template">part-template</span>
45+
</UI.Part>
46+
<UI.Part part={message.parts[0]}>
47+
{(part) => <output id="part-callback">{part.key as string}</output>}
48+
</UI.Part>
49+
<UI.Interrupts>
50+
<span id="interrupts-template">interrupts-template</span>
51+
</UI.Interrupts>
52+
<UI.Interrupts>
53+
{(items) => <output id="interrupts-callback">{String(items.length)}</output>}
54+
</UI.Interrupts>
55+
<UI.Interrupt interrupt={interrupt}>
56+
<span id="interrupt-template">interrupt-template</span>
57+
</UI.Interrupt>
58+
<UI.Interrupt interrupt={interrupt}>
59+
{(item) => <output id="interrupt-callback">{item.reason as string}</output>}
60+
</UI.Interrupt>
61+
</UI.Provider>
62+
<Chat connection={options.connection}>
63+
<ChatInput>
64+
<span id="input-template">input-template</span>
65+
</ChatInput>
66+
<ChatInput>
67+
{(props) => <output id="input-callback">{props.disabled ? 'busy' : 'ready'}</output>}
68+
</ChatInput>
69+
<ToolApproval
70+
toolCallId="c1"
71+
toolName="buy"
72+
input={{ n: 1 }}
73+
approval={{ id: 'a1', needsApproval: true }}
74+
>
75+
<span id="approval-template">approval-template</span>
76+
</ToolApproval>
77+
<ToolApproval
78+
toolCallId="c2"
79+
toolName="sell"
80+
input={{ n: 2 }}
81+
approval={{ id: 'a2', needsApproval: true }}
82+
>
83+
{(props) => <output id="approval-callback">{props.toolName as string}</output>}
84+
</ToolApproval>
85+
</Chat>
86+
</main>
87+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { afterEach, expect, it } from 'vitest';
2+
import { mount } from '../../../octane/tests/_helpers';
3+
import { ChatUIChildren } from '../_fixtures/chat-ui-children.tsrx';
4+
5+
afterEach(() => document.body.replaceChildren());
6+
7+
it('chat UI widgets render template children and call render props', () => {
8+
const result = mount(ChatUIChildren);
9+
try {
10+
expect(result.find('#messages-template').textContent).toBe('messages-template');
11+
expect(result.find('#messages-callback').textContent).toBe('1');
12+
expect(result.find('#message-template').textContent).toBe('message-template');
13+
expect(result.find('#message-callback').textContent).toBe('1');
14+
expect(result.find('#part-template').textContent).toBe('part-template');
15+
expect(result.find('#part-callback').textContent).toBe('text');
16+
expect(result.find('#interrupts-template').textContent).toBe('interrupts-template');
17+
expect(result.find('#interrupts-callback').textContent).toBe('1');
18+
expect(result.find('#interrupt-template').textContent).toBe('interrupt-template');
19+
expect(result.find('#interrupt-callback').textContent).toBe('choose');
20+
expect(result.find('#input-template').textContent).toBe('input-template');
21+
expect(result.find('#input-callback').textContent).toBe('ready');
22+
expect(result.find('#approval-template').textContent).toBe('approval-template');
23+
expect(result.find('#approval-callback').textContent).toBe('sell');
24+
} finally {
25+
result.unmount();
26+
}
27+
});

0 commit comments

Comments
 (0)