Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

fix: scroll and alignment for toolblock (#144)

Vinit13d206905852
diff --git a/packages/trueforge-ui-sdk/src/atoms/CodeEditor.tsx b/packages/trueforge-ui-sdk/src/atoms/CodeEditor.tsx
index a3de9fa4b..f4197e646 100644
--- a/packages/trueforge-ui-sdk/src/atoms/CodeEditor.tsx
+++ b/packages/trueforge-ui-sdk/src/atoms/CodeEditor.tsx
@@ -7,7 +7,7 @@ import { useSlot } from '../theme/SlotsProvider.js';
import { cn } from './lib/cn.js';
import type { MonacoEditorCoreProps } from './MonacoEditorCore.js';
-export type CodeEditorProps = Omit<MonacoEditorCoreProps, 'options'> & {
+export type CodeEditorProps = Omit<MonacoEditorCoreProps, 'onAutoHeightChange' | 'options'> & {
filename?: string;
showToolbar?: boolean;
};
diff --git a/packages/trueforge-ui-sdk/src/atoms/MonacoEditorCore.tsx b/packages/trueforge-ui-sdk/src/atoms/MonacoEditorCore.tsx
index edcd74592..748b4a8f6 100644
--- a/packages/trueforge-ui-sdk/src/atoms/MonacoEditorCore.tsx
+++ b/packages/trueforge-ui-sdk/src/atoms/MonacoEditorCore.tsx
@@ -14,6 +14,7 @@ export type MonacoEditorCoreProps = {
onChange?: (value: string) => void;
onMount?: (editor: unknown, monaco: unknown) => void;
beforeMount?: (monaco: unknown) => void;
+ onAutoHeightChange?: (height: number) => void; // Reports Monaco's measured, clamped content height while `autoHeight` is active.
className?: string;
/**
* Fixed height. Ignored when `autoHeight` is true (except as an initial
@@ -156,6 +157,7 @@ export function MonacoEditorCore({
onChange,
onMount,
beforeMount,
+ onAutoHeightChange,
className,
height = 400,
autoHeight = false,
@@ -171,9 +173,11 @@ export function MonacoEditorCore({
const onChangeRef = useRef(onChange);
const onMountRef = useRef(onMount);
const beforeMountRef = useRef(beforeMount);
+ const onAutoHeightChangeRef = useRef(onAutoHeightChange);
onChangeRef.current = onChange;
onMountRef.current = onMount;
beforeMountRef.current = beforeMount;
+ onAutoHeightChangeRef.current = onAutoHeightChange;
const minPx = toPx(minHeight, DEFAULT_LINE_HEIGHT + DEFAULT_VERTICAL_PADDING);
const maxPx = toPx(maxHeight, toPx(DEFAULT_MAX_HEIGHT, 168));
@@ -193,6 +197,7 @@ export function MonacoEditorCore({
setAutoPx(next);
wrapper.style.height = `${next}px`;
editor.layout();
+ onAutoHeightChangeRef.current?.(next);
};
useEffect(() => {
diff --git a/packages/trueforge-ui-sdk/src/atoms/ToolApprovalBar.tsx b/packages/trueforge-ui-sdk/src/atoms/ToolApprovalBar.tsx
index d4d07a00a..e7e2cc0c8 100644
--- a/packages/trueforge-ui-sdk/src/atoms/ToolApprovalBar.tsx
+++ b/packages/trueforge-ui-sdk/src/atoms/ToolApprovalBar.tsx
@@ -173,7 +173,7 @@ export function ToolApprovalBar({
))}
</div>
)}
- <div className="flex gap-2">
+ <div className="flex items-center gap-2">
<input
placeholder="Enter reason for denial"
aria-label="Reason for denial"
@@ -183,7 +183,7 @@ export function ToolApprovalBar({
disabled={interactionsLocked}
onChange={e => onDenialReasonChange?.(e.target.value)}
className={cn(
- 'h-6 flex-1 rounded border border-input bg-background px-2 text-xs text-foreground',
+ 'h-8 flex-1 rounded border border-input bg-background px-2 text-xs text-foreground',
'focus:outline-none focus:ring-1 focus:ring-ring',
)}
/>
diff --git a/packages/trueforge-ui-sdk/src/atoms/ToolCallContentBlock.tsx b/packages/trueforge-ui-sdk/src/atoms/ToolCallContentBlock.tsx
index 29292a503..d5bca40c5 100644
--- a/packages/trueforge-ui-sdk/src/atoms/ToolCallContentBlock.tsx
+++ b/packages/trueforge-ui-sdk/src/atoms/ToolCallContentBlock.tsx
@@ -19,6 +19,7 @@ export type ToolCallContentBlockProps = {
onFullscreenChange?: (fullscreen: boolean) => void;
contentHeightRem?: number;
contentRef?: (node: HTMLDivElement | null) => void;
+ onContentHeightChange?: (height: number) => void;
dataTestPrefix?: string;
className?: string;
};
@@ -28,11 +29,13 @@ function JsonEditor({
height,
autoHeight = false,
maxHeight,
+ onAutoHeightChange,
}: {
content: string;
height?: string | number;
autoHeight?: boolean;
maxHeight?: string | number;
+ onAutoHeightChange?: (height: number) => void;
}) {
const MonacoEditorCore = useSlot('MonacoEditorCore');
@@ -43,6 +46,7 @@ function JsonEditor({
height={height}
autoHeight={autoHeight}
maxHeight={maxHeight}
+ onAutoHeightChange={onAutoHeightChange}
options={{
readOnly: true,
minimap: { enabled: false },
@@ -52,7 +56,7 @@ function JsonEditor({
folding: true,
fontSize: 12,
}}
- className="!border-0"
+ className="border-0!"
/>
);
}
@@ -66,8 +70,9 @@ export function ToolCallContentBlock({
resizable = false,
fullscreen = false,
onFullscreenChange,
- contentHeightRem = 1.5,
+ contentHeightRem,
contentRef,
+ onContentHeightChange,
dataTestPrefix,
className,
}: ToolCallContentBlockProps) {
@@ -82,9 +87,10 @@ export function ToolCallContentBlock({
});
};
+ const hasMeasuredResizableHeight = resizable && contentHeightRem !== undefined;
const bodyStyle = resizable
? {
- height: `${Math.min(contentHeightRem, 10)}rem`,
+ ...(hasMeasuredResizableHeight ? { height: `${Math.min(contentHeightRem, 10)}rem` } : {}),
resize: 'vertical' as const,
padding: '0.25rem',
}
@@ -126,15 +132,24 @@ export function ToolCallContentBlock({
<div
ref={resizable ? contentRef : undefined}
className={cn(
- 'relative min-h-[1.875rem] rounded-b-lg border border-t-0 border-border bg-background',
- resizable && 'overflow-y-auto',
+ 'relative min-h-7.5 rounded-b-lg border border-t-0 border-border bg-background',
+ resizable && 'overflow-hidden',
)}
style={bodyStyle}
>
{isJson ? (
- <JsonEditor content={content} autoHeight maxHeight={maxHeight} />
+ <JsonEditor
+ content={content}
+ height={hasMeasuredResizableHeight ? '100%' : undefined}
+ autoHeight={!hasMeasuredResizableHeight}
+ maxHeight={maxHeight}
+ onAutoHeightChange={resizable ? onContentHeightChange : undefined}
+ />
) : (
- <div className="overflow-y-auto" style={maxHeight ? { maxHeight } : undefined}>
+ <div
+ className={cn('overflow-y-auto', hasMeasuredResizableHeight && 'h-full')}
+ style={!hasMeasuredResizableHeight && maxHeight ? { maxHeight } : undefined}
+ >
<Markdown content={content} className="text-xs text-muted-foreground" />
</div>
)}
diff --git a/packages/trueforge-ui-sdk/src/atoms/agent-chat/AgentStepRow.tsx b/packages/trueforge-ui-sdk/src/atoms/agent-chat/AgentStepRow.tsx
index fa87d4da0..66d8bd26a 100644
--- a/packages/trueforge-ui-sdk/src/atoms/agent-chat/AgentStepRow.tsx
+++ b/packages/trueforge-ui-sdk/src/atoms/agent-chat/AgentStepRow.tsx
@@ -187,7 +187,7 @@ export function AgentStepRow({
{hasPersistentChildren && (
<div
- className="flex min-w-0 gap-3"
+ className={cn('flex min-w-0', showPersistentContentConnector ? 'gap-3' : 'gap-2')}
data-testid={dataTestPrefix ? `${dataTestPrefix}-persistent-content` : undefined}
>
{showChevronColumn && <div className="w-5 shrink-0" />}
diff --git a/packages/trueforge-ui-sdk/src/containers/ToolCallContentBlockContainer.tsx b/packages/trueforge-ui-sdk/src/containers/ToolCallContentBlockContainer.tsx
index 168fe349c..8a4b04b19 100644
--- a/packages/trueforge-ui-sdk/src/containers/ToolCallContentBlockContainer.tsx
+++ b/packages/trueforge-ui-sdk/src/containers/ToolCallContentBlockContainer.tsx
@@ -7,22 +7,48 @@ import { useSlot } from '../theme/SlotsProvider.js';
export function ToolCallContentBlockContainer(props: ToolCallContentBlockProps) {
const ToolCallContentBlock = useSlot('ToolCallContentBlock');
+ const { isJson, onContentHeightChange: notifyContentHeightChange, resizable } = props;
const [fullscreen, setFullscreen] = useState(false);
- const [contentHeightRem, setContentHeightRem] = useState(1.5);
+ const [contentHeightRem, setContentHeightRem] = useState<number>();
const observerRef = useRef<ResizeObserver | null>(null);
+ const contentNodeRef = useRef<HTMLDivElement | null>(null);
+ const hasMeasuredRef = useRef(false);
const contentRef = useCallback(
(node: HTMLDivElement | null) => {
observerRef.current?.disconnect();
observerRef.current = null;
- if (!props.resizable || node == null) return;
+ contentNodeRef.current = node;
+ hasMeasuredRef.current = false;
+ if (node == null) return;
- const updateHeight = () => setContentHeightRem(node.scrollHeight / 16);
+ setContentHeightRem(undefined);
+ if (!resizable) return;
+
+ const updateHeight = () => {
+ if (!hasMeasuredRef.current && isJson !== false) return;
+ const renderedHeight = node.getBoundingClientRect().height;
+ const height = hasMeasuredRef.current && renderedHeight > 0 ? renderedHeight : node.scrollHeight;
+ hasMeasuredRef.current = true;
+ setContentHeightRem(height / 16);
+ };
updateHeight();
observerRef.current = new ResizeObserver(updateHeight);
observerRef.current.observe(node);
},
- [props.resizable],
+ [isJson, resizable],
+ );
+
+ const onContentHeightChange = useCallback(
+ (height: number) => {
+ const node = contentNodeRef.current;
+ if (node == null) return;
+ const renderedHeight = node.getBoundingClientRect().height;
+ hasMeasuredRef.current = true;
+ setContentHeightRem((renderedHeight > 0 ? renderedHeight : Math.max(height, node.scrollHeight)) / 16);
+ notifyContentHeightChange?.(height);
+ },
+ [notifyContentHeightChange],
);
useEffect(
@@ -39,6 +65,7 @@ export function ToolCallContentBlockContainer(props: ToolCallContentBlockProps)
onFullscreenChange={setFullscreen}
contentHeightRem={contentHeightRem}
contentRef={contentRef}
+ onContentHeightChange={onContentHeightChange}
/>
);
}
diff --git a/packages/trueforge-ui-sdk/test/atoms/MonacoEditorCore.test.tsx b/packages/trueforge-ui-sdk/test/atoms/MonacoEditorCore.test.tsx
index 5bb9a4bc3..7a6b5e844 100644
--- a/packages/trueforge-ui-sdk/test/atoms/MonacoEditorCore.test.tsx
+++ b/packages/trueforge-ui-sdk/test/atoms/MonacoEditorCore.test.tsx
@@ -111,18 +111,27 @@ describe('MonacoEditorCore', () => {
it('clamps auto-height and responds to Monaco content-size changes', async () => {
monacoMocks.editor.getContentHeight.mockReturnValue(500);
+ const onAutoHeightChange = vi.fn();
const { container, unmount } = render(
- <MonacoEditorCore value={'one\ntwo'} autoHeight minHeight={30} maxHeight={100} />,
+ <MonacoEditorCore
+ value={'one\ntwo'}
+ autoHeight
+ minHeight={30}
+ maxHeight={100}
+ onAutoHeightChange={onAutoHeightChange}
+ />,
);
await waitFor(() => expect(monacoMocks.editorApi.create).toHaveBeenCalledOnce());
const editorContainer = container.querySelector('.aui-monaco');
expect(editorContainer).toHaveStyle({ height: '100px' });
expect(monacoMocks.editor.layout).toHaveBeenCalled();
+ expect(onAutoHeightChange).toHaveBeenLastCalledWith(100);
monacoMocks.editor.getContentHeight.mockReturnValue(10);
act(() => monacoMocks.invokeSizeChange());
expect(editorContainer).toHaveStyle({ height: '30px' });
+ expect(onAutoHeightChange).toHaveBeenLastCalledWith(30);
unmount();
expect(monacoMocks.sizeDisposable.dispose).toHaveBeenCalledOnce();
diff --git a/packages/trueforge-ui-sdk/test/atoms/ToolApprovalBar.test.tsx b/packages/trueforge-ui-sdk/test/atoms/ToolApprovalBar.test.tsx
index 1884c5c38..0eabdbd05 100644
--- a/packages/trueforge-ui-sdk/test/atoms/ToolApprovalBar.test.tsx
+++ b/packages/trueforge-ui-sdk/test/atoms/ToolApprovalBar.test.tsx
@@ -68,6 +68,8 @@ describe('ToolApprovalBar', () => {
expect(screen.getByRole('alert')).toHaveTextContent('Reason is required');
const reasonInput = screen.getByRole('textbox', { name: 'Reason for denial' });
expect(reasonInput).toHaveAttribute('aria-invalid', 'true');
+ expect(reasonInput).toHaveClass('h-8');
+ expect(screen.getByRole('button', { name: 'Submit' })).toHaveClass('h-8');
fireEvent.change(reasonInput, { target: { value: 'new reason' } });
expect(onDenialReasonChange).toHaveBeenCalledWith('new reason');
diff --git a/packages/trueforge-ui-sdk/test/atoms/ToolCallContentBlock.test.tsx b/packages/trueforge-ui-sdk/test/atoms/ToolCallContentBlock.test.tsx
index 7e8c7972a..96ebcc3c7 100644
--- a/packages/trueforge-ui-sdk/test/atoms/ToolCallContentBlock.test.tsx
+++ b/packages/trueforge-ui-sdk/test/atoms/ToolCallContentBlock.test.tsx
@@ -122,9 +122,24 @@ describe('ToolCallContentBlock', () => {
expect(screen.queryByTestId('json-editor')).not.toBeInTheDocument();
const body = screen.getByTestId('markdown-content').parentElement?.parentElement;
expect(body).toHaveStyle({ height: '10rem', resize: 'vertical' });
+ expect(body).toHaveClass('overflow-hidden');
expect(contentRef).toHaveBeenCalledWith(body);
});
+ it('lets Monaco own scrolling after the resizable body is measured', () => {
+ renderBlock({
+ title: 'Response',
+ content: '{"items":[1,2,3]}',
+ resizable: true,
+ contentHeightRem: 8,
+ });
+
+ const editor = screen.getByTestId('json-editor');
+ expect(editor).toHaveAttribute('data-height', '100%');
+ expect(editor).toHaveAttribute('data-auto-height', 'false');
+ expect(editor.parentElement).toHaveClass('overflow-hidden');
+ });
+
it('requests fullscreen and closes the controlled fullscreen dialog', () => {
const onFullscreenChange = vi.fn();
const { rerender } = renderBlock({
diff --git a/packages/trueforge-ui-sdk/test/atoms/agent-chat/AgentStepRow.test.tsx b/packages/trueforge-ui-sdk/test/atoms/agent-chat/AgentStepRow.test.tsx
index fbe0c7218..2b46d31fd 100644
--- a/packages/trueforge-ui-sdk/test/atoms/agent-chat/AgentStepRow.test.tsx
+++ b/packages/trueforge-ui-sdk/test/atoms/agent-chat/AgentStepRow.test.tsx
@@ -76,6 +76,7 @@ describe('AgentStepRow', () => {
title="Process files"
expanded={false}
dataTestPrefix="process"
+ showPersistentContentConnector={false}
persistentChildren={<div>Always-visible progress</div>}
>
<div>Expanded details</div>
@@ -85,12 +86,14 @@ describe('AgentStepRow', () => {
expect(screen.queryByTestId('process-expanded-content')).not.toBeInTheDocument();
expect(screen.queryByText('Expanded details')).not.toBeInTheDocument();
expect(screen.getByTestId('process-persistent-content')).toHaveTextContent('Always-visible progress');
+ expect(screen.getByTestId('process-persistent-content')).toHaveClass('gap-2');
rerender(
<TestSubject
title="Process files"
expanded
dataTestPrefix="process"
+ showPersistentContentConnector={false}
persistentChildren={<div>Always-visible progress</div>}
>
<div>Expanded details</div>
diff --git a/packages/trueforge-ui-sdk/test/containers/ToolCallContentBlockContainer.test.tsx b/packages/trueforge-ui-sdk/test/containers/ToolCallContentBlockContainer.test.tsx
index 6dd1a712f..c09c6cdb4 100644
--- a/packages/trueforge-ui-sdk/test/containers/ToolCallContentBlockContainer.test.tsx
+++ b/packages/trueforge-ui-sdk/test/containers/ToolCallContentBlockContainer.test.tsx
@@ -9,6 +9,8 @@ import { SlotsProvider } from '@/theme/SlotsProvider.js';
let observedNodes: Element[] = [];
let disconnectCount = 0;
+let reportContentHeight = true;
+let renderedContentHeight = 0;
class ResizeObserverProbe implements ResizeObserver {
readonly callback: ResizeObserverCallback;
@@ -40,15 +42,23 @@ function ToolCallContentBlockProbe({
onFullscreenChange,
contentHeightRem,
contentRef,
+ onContentHeightChange,
}: ToolCallContentBlockProps) {
const measuredContentRef = useCallback(
(node: HTMLDivElement | null) => {
if (node !== null) {
Object.defineProperty(node, 'scrollHeight', { configurable: true, value: 64 });
+ Object.defineProperty(node, 'getBoundingClientRect', {
+ configurable: true,
+ value: () => new DOMRect(0, 0, 100, renderedContentHeight),
+ });
+ }
+ if (resizable) {
+ contentRef?.(node);
+ if (node !== null && reportContentHeight) onContentHeightChange?.(64);
}
- if (resizable) contentRef?.(node);
},
- [contentRef, resizable],
+ [contentRef, onContentHeightChange, resizable],
);
return (
@@ -93,6 +103,8 @@ describe('ToolCallContentBlockContainer', () => {
beforeEach(() => {
observedNodes = [];
disconnectCount = 0;
+ reportContentHeight = true;
+ renderedContentHeight = 0;
vi.stubGlobal('ResizeObserver', ResizeObserverProbe);
});
@@ -130,4 +142,20 @@ describe('ToolCallContentBlockContainer', () => {
unmount();
expect(disconnectCount).toBeGreaterThan(disconnectsBeforeUnmount);
});
+
+ it('does not lock JSON height before Monaco reports its measured content height', () => {
+ reportContentHeight = false;
+ render(<TestSubject resizable />);
+
+ expect(screen.getByTestId('content-block-probe')).toHaveAttribute('data-content-height', 'undefined');
+ });
+
+ it('preserves the rendered box height when Monaco finishes measuring', async () => {
+ renderedContentHeight = 72;
+ render(<TestSubject resizable />);
+
+ await waitFor(() => {
+ expect(screen.getByTestId('content-block-probe')).toHaveAttribute('data-content-height', '4.5');
+ });
+ });
});