🧰 refactor: Pure Step Reducer for Tool Call Events (#16290)

* refactor: lift tool call step transitions into pure reducers

Add pure reducers for tool_calls run steps, argument deltas, completions
and run-step closures, and move the skill-authoring detection with them.
useStepHandler keeps the step-to-tool-call-id map and records the id each
reducer returns.

* test: cover tool call rendering order with an e2e scenario

* test: anchor the tool call scenario on the code execution card

* test: state the tool reducer guarantees the tests actually cover

Drop the claim that a replayed run step is idempotent after completion and
that a completion may arrive before its run step; cover re-announcing an
open step instead. Match the code card by its name prefix, since a slow
run appends its duration.

(cherry picked from commit 9c7555ab0d)

Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/16290
This commit is contained in:
Marco Beretta 2026-09-24 18:50:04 +02:00 • committed by Lia
parent ccae933e6d
commit 0173047549
5 changed files with 482 additions and 185 deletions

View file

@ -1,4 +1,4 @@
import { StepTypes, ContentTypes } from 'librechat-data-provider';
import { StepTypes, ContentTypes, ToolCallTypes } from 'librechat-data-provider';
import type {
Agents,
TMessage,
@ -7,9 +7,13 @@ import type {
TMessageContentParts,
} from 'librechat-data-provider';
import {
applyToolCallCompleted,
calculateContentIndex,
applyReasoningDelta,
applySummarizeDelta,
applyToolCallDelta,
applyRunStepClosed,
applyToolCallsStep,
finalizeSummaries,
applyMessageDelta,
applySummaryStep,
@ -43,6 +47,20 @@ const messageStep = (
...overrides,
});
const toolStep = (
id: string,
index: number,
toolCalls: Agents.ToolCall[],
overrides: Partial<Agents.RunStep> = {},
): Agents.RunStep => ({
id,
index,
runId: RUN_ID,
type: StepTypes.TOOL_CALLS,
stepDetails: { type: StepTypes.TOOL_CALLS, tool_calls: toolCalls as Agents.AgentToolCall[] },
...overrides,
});
const textDelta = (id: string, text: string): Agents.MessageDeltaEvent => ({
id,
delta: { content: [{ type: ContentTypes.TEXT, text }] },
@ -53,8 +71,22 @@ const thinkDelta = (id: string, think: string): Agents.ReasoningDeltaEvent => ({
delta: { content: [{ type: ContentTypes.THINK, think }] },
});
const argsDelta = (id: string, args: string): Agents.RunStepDeltaEvent => ({
id,
delta: { type: StepTypes.TOOL_CALLS, tool_calls: [{ args } as Agents.ToolCallChunk] },
});
const toolEnd = (id: string, toolCall: Partial<Agents.ToolCall>): Agents.ToolEndEvent => ({
id,
index: 0,
tool_call: { type: ToolCallTypes.TOOL_CALL, ...toolCall } as Agents.ToolCall,
});
const typesOf = (message: TMessage) => (message.content ?? []).map((part) => part?.type);
const toolCallAt = (message: TMessage, index: number) =>
(message.content?.[index] as Agents.ToolCallContent | undefined)?.tool_call;
/**
* The invariant every reducer keeps: each step's part sits at `step.index + offset`, whatever
* order the events arrived in.
@ -331,6 +363,195 @@ describe('steps', () => {
});
});
describe('tool calls', () => {
const search = toolStep('step-tool', 1, [{ id: 'call-1', name: 'search', args: '' }]);
const runToolCall = (response: TMessage) => {
const opened = applyToolCallsStep(response, search, 0);
let message = opened.message;
for (const chunk of ['{"q":', '"cats"}']) {
message = applyToolCallDelta(
message,
search,
argsDelta(search.id, chunk),
opened.toolCallId ?? '',
0,
) as TMessage;
}
return {
toolCallId: opened.toolCallId,
message: applyToolCallCompleted(
message,
search,
toolEnd(search.id, { id: 'call-1', name: 'search', args: '{"q":"cats"}', output: '3' }),
0,
),
};
};
it('opens, streams args into, and completes the step part', () => {
const text = messageStep('step-text', 0);
const { message, toolCallId } = runToolCall(streamText(createResponse(), text, ['Hi']));
expect(toolCallId).toBe('call-1');
expect(toolCallAt(message, 1)).toMatchObject({
id: 'call-1',
name: 'search',
args: '{"q":"cats"}',
output: '3',
progress: 1,
stepId: search.id,
});
expectStepPositions(message, [
[text, ContentTypes.TEXT],
[search, ContentTypes.TOOL_CALL],
]);
});
it('is idempotent when the completion is delivered twice', () => {
const { message } = runToolCall(createResponse());
const completedTwice = applyToolCallCompleted(
message,
search,
toolEnd(search.id, { id: 'call-1', name: 'search', args: '{"q":"cats"}', output: '3' }),
0,
);
expect(completedTwice.content).toEqual(message.content);
});
it('keeps streamed args when an open step is announced again', () => {
const opened = applyToolCallsStep(createResponse(), search, 0).message;
const streamed = applyToolCallDelta(
opened,
search,
argsDelta(search.id, '{"q":'),
'call-1',
0,
) as TMessage;
const reannounced = applyToolCallsStep(streamed, search, 0).message;
expect(reannounced.content).toEqual(streamed.content);
});
it('keeps streamed args when the completion omits them', () => {
const opened = applyToolCallsStep(createResponse(), search, 0).message;
const streamed = applyToolCallDelta(opened, search, argsDelta(search.id, '{}'), 'call-1', 0);
const completed = applyToolCallCompleted(
streamed as TMessage,
search,
toolEnd(search.id, { id: 'call-1', name: 'search', output: 'ok' }),
0,
);
expect(toolCallAt(completed, 1)).toMatchObject({ args: '{}', output: 'ok' });
});
it('writes a completion into its step slot when the opening part is missing', () => {
const completed = applyToolCallCompleted(
createResponse(),
search,
toolEnd(search.id, { id: 'call-1', name: 'search', args: { q: 'cats' }, output: '3' }),
0,
);
expect(completed.content?.[0]).toBeUndefined();
expect(toolCallAt(completed, 1)).toMatchObject({ id: 'call-1', progress: 1 });
});
it('attaches OAuth prompts carried on an args delta', () => {
const opened = applyToolCallsStep(createResponse(), search, 0).message;
const result = applyToolCallDelta(
opened,
search,
{
id: search.id,
delta: {
type: StepTypes.TOOL_CALLS,
tool_calls: [{ args: '' } as Agents.ToolCallChunk],
auth: 'https://auth.example.com',
expires_at: 123,
},
},
'call-1',
0,
);
expect(toolCallAt(result as TMessage, 1)).toMatchObject({
auth: 'https://auth.example.com',
expires_at: 123,
});
});
it('ignores deltas that are not tool-call deltas', () => {
expect(
applyToolCallDelta(
createResponse(),
search,
{ id: search.id, delta: { type: StepTypes.MESSAGE_CREATION } },
'call-1',
0,
),
).toBeUndefined();
});
it('displaces a pending OAuth prompt when real content lands in its slot', () => {
const oauth = createResponse([
{
type: ContentTypes.TOOL_CALL,
tool_call: { id: 'oauth', name: 'oauth_mcp_github', args: '', type: 'tool_call' },
} as TMessageContentParts,
]);
const result = streamText(oauth, messageStep('step-text', 0), ['resumed']);
expect(result.content).toEqual([{ type: ContentTypes.TEXT, text: 'resumed' }]);
});
it('stamps the terminal status on close, and only on tool calls', () => {
const { message } = runToolCall(createResponse());
const closed: Agents.RunStepClosedEvent = {
id: search.id,
index: 1,
type: StepTypes.TOOL_CALLS,
status: 'cancelled',
created_at: 1_000,
closed_at: 1_250,
};
const result = applyRunStepClosed(message, search, closed, 0);
const closedTwice = applyRunStepClosed(result as TMessage, search, closed, 0);
expect(toolCallAt(result as TMessage, 1)).toMatchObject({
runStepStatus: 'cancelled',
runStepDurationMs: 250,
});
expect(closedTwice?.content).toEqual(result?.content);
expect(applyRunStepClosed(createResponse(), search, closed, 0)).toBeUndefined();
expect(
applyRunStepClosed(
streamText(createResponse(), messageStep('step-text', 1), ['t']),
search,
closed,
0,
),
).toBeUndefined();
});
it('records the last non-empty tool call id announced by a step', () => {
const parallel = toolStep('step-many', 0, [
{ id: 'call-a', name: 'a', args: '' },
{ id: '', name: 'b', args: '' },
]);
expect(applyToolCallsStep(createResponse(), parallel, 0).toolCallId).toBe('call-a');
expect(
applyToolCallsStep(createResponse(), messageStep('step-text', 0), 0).toolCallId,
).toBeUndefined();
});
});
describe('image parts', () => {
it('holds an image slot without letting text overwrite it', () => {
const warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined);

View file

@ -1,2 +1,3 @@
export * from './content';
export * from './text';
export * from './tools';

View file

@ -0,0 +1,160 @@
import { StepTypes, ContentTypes, getRunStepDurationMs } from 'librechat-data-provider';
import type { Agents, TMessage } from 'librechat-data-provider';
import { getStepMetadata, updateContent } from './content';
/** Mirrors `SKILL_FILE_PREFIX` in `@librechat/api` file-authoring handlers. */
const SKILL_FILE_PREFIX = 'skills/';
const FILE_AUTHORING_TOOLS = new Set(['create_file', 'edit_file']);
/**
* True when a completed tool call authored a skill file (`create_file` /
* `edit_file` targeting a `skills/...` path). Skills created or edited
* mid-chat must invalidate the cached skill queries, or the Skills panel
* and builder keep showing the pre-authoring catalog.
*/
export function isSkillAuthoringToolCall(toolCall?: Agents.ToolCall): boolean {
if (!toolCall?.name || !FILE_AUTHORING_TOOLS.has(toolCall.name)) {
return false;
}
const { args } = toolCall;
let filePath: unknown;
if (typeof args === 'object' && args !== null) {
filePath = (args as { file_path?: unknown }).file_path;
} else if (typeof args === 'string') {
try {
filePath = (JSON.parse(args) as { file_path?: unknown }).file_path;
} catch {
return false;
}
}
return typeof filePath === 'string' && filePath.startsWith(SKILL_FILE_PREFIX);
}
/**
* Opens the tool-call parts a `tool_calls` run step announces, all at the step's slot.
* AI SDK: `tool-input-start`.
*
* Returns the next message and the tool-call id to record for the step, so later argument deltas,
* which carry only the step id, can be attributed. The last non-empty id wins, matching a step
* that announces a single call.
*/
export function applyToolCallsStep(
message: TMessage,
runStep: Agents.RunStep,
editPrefixOffset: number,
): { message: TMessage; toolCallId?: string } {
if (runStep.stepDetails.type !== StepTypes.TOOL_CALLS) {
return { message };
}
const index = runStep.index + editPrefixOffset;
const metadata = getStepMetadata(runStep);
let next: TMessage = { ...message };
let toolCallId: string | undefined;
for (const toolCall of (runStep.stepDetails.tool_calls ?? []) as Agents.ToolCall[]) {
const id = toolCall.id ?? '';
if ('id' in toolCall && id) {
toolCallId = id;
}
next = updateContent(
next,
index,
{
type: ContentTypes.TOOL_CALL,
tool_call: { name: toolCall.name ?? '', args: toolCall.args, id, stepId: runStep.id },
},
false,
metadata,
);
}
return { message: next, toolCallId };
}
/**
* Appends streamed tool-call arguments (and any OAuth prompt) to the step's tool-call part.
* AI SDK: `tool-input-delta`. Returns `undefined` for a delta that is not a tool-call delta.
*/
export function applyToolCallDelta(
message: TMessage,
runStep: Agents.RunStep,
delta: Agents.RunStepDeltaEvent,
toolCallId: string,
editPrefixOffset: number,
): TMessage | undefined {
if (delta.delta.type !== StepTypes.TOOL_CALLS || !delta.delta.tool_calls) {
return undefined;
}
const index = runStep.index + editPrefixOffset;
const metadata = getStepMetadata(runStep);
let next: TMessage = { ...message };
for (const toolCallDelta of delta.delta.tool_calls) {
const contentPart: Agents.MessageContentComplex = {
type: ContentTypes.TOOL_CALL,
tool_call: {
name: toolCallDelta.name ?? '',
args: toolCallDelta.args ?? '',
id: toolCallId,
stepId: delta.id,
},
};
if (delta.delta.auth != null) {
contentPart.tool_call.auth = delta.delta.auth;
contentPart.tool_call.expires_at = delta.delta.expires_at;
}
next = updateContent(next, index, contentPart, false, metadata);
}
return next;
}
/**
* Settles the step's tool-call part with its output, marking it complete.
* AI SDK: `tool-output-available`.
*/
export function applyToolCallCompleted(
message: TMessage,
runStep: Agents.RunStep,
result: Agents.ToolEndEvent,
editPrefixOffset: number,
): TMessage {
return updateContent(
{ ...message },
runStep.index + editPrefixOffset,
{ type: ContentTypes.TOOL_CALL, tool_call: { ...result.tool_call, stepId: result.id } },
true,
getStepMetadata(runStep),
);
}
/**
* Stamps a closed step's terminal status and duration on its tool-call part. Only tool calls
* render a running state, so any other part type (or an empty slot, as on a reconnect that
* missed the opening) returns `undefined` and is left alone.
*/
export function applyRunStepClosed(
message: TMessage,
runStep: Agents.RunStep,
closed: Agents.RunStepClosedEvent,
editPrefixOffset: number,
): TMessage | undefined {
const index = runStep.index + editPrefixOffset;
const existing = message.content?.[index];
if (!existing || existing.type !== ContentTypes.TOOL_CALL) {
return undefined;
}
const existingToolCall = existing[ContentTypes.TOOL_CALL];
if (!existingToolCall) {
return undefined;
}
/** Spread conditionally so an unknowable duration leaves any value the server already stamped
* in place, rather than overwriting it with `undefined`. */
const durationMs = getRunStepDurationMs(closed);
const content = [...(message.content ?? [])];
content[index] = {
...existing,
[ContentTypes.TOOL_CALL]: {
...existingToolCall,
runStepStatus: closed.status,
...(durationMs != null && { runStepDurationMs: durationMs }),
},
};
return { ...message, content };
}

View file

@ -1,15 +1,7 @@
import { useCallback, useRef } from 'react';
import { useStore } from 'jotai';
import { useRecoilCallback } from 'recoil';
import {
Constants,
StepTypes,
StepEvents,
ContentTypes,
getRunStepDurationMs,
getRunStepCloseMetadata,
getToolTimingDurations,
} from 'librechat-data-provider';
import { Constants, StepTypes, StepEvents, ContentTypes } from 'librechat-data-provider';
import type {
Agents,
TMessage,
@ -34,14 +26,17 @@ import {
} from '~/components/Chat/Subagents/state';
import {
applyReasoningDelta,
applyToolCallCompleted,
isSkillAuthoringToolCall,
applyToolCallDelta,
applySummarizeDelta,
applyRunStepClosed,
applyToolCallsStep,
applyMessageDelta,
finalizeSummaries,
applyAgentUpdate,
applySummaryStep,
getStepMetadata,
getEditPrefix,
updateContent,
} from './steps';
import {
sandboxStartingByToolCallId,
@ -86,34 +81,6 @@ type TStepEvent =
| { event: StepEvents.ON_SANDBOX_STARTING; data: SandboxStartingEvent }
| { event: StepEvents.ON_PTC_TOOL_CALL; data: PtcToolCallEvent };
/** Mirrors `SKILL_FILE_PREFIX` in `@librechat/api` file-authoring handlers. */
const SKILL_FILE_PREFIX = 'skills/';
const FILE_AUTHORING_TOOLS = new Set(['create_file', 'edit_file']);
/**
* True when a completed tool call authored a skill file (`create_file` /
* `edit_file` targeting a `skills/...` path). Skills created or edited
* mid-chat must invalidate the cached skill queries, or the Skills panel
* and builder keep showing the pre-authoring catalog.
*/
function isSkillAuthoringToolCall(toolCall?: Agents.ToolCall): boolean {
if (!toolCall?.name || !FILE_AUTHORING_TOOLS.has(toolCall.name)) {
return false;
}
const { args } = toolCall;
let filePath: unknown;
if (typeof args === 'object' && args !== null) {
filePath = (args as { file_path?: unknown }).file_path;
} else if (typeof args === 'string') {
try {
filePath = (JSON.parse(args) as { file_path?: unknown }).file_path;
} catch {
return false;
}
}
return typeof filePath === 'string' && filePath.startsWith(SKILL_FILE_PREFIX);
}
export default function useStepHandler({
setMessages,
getMessages,
@ -644,42 +611,14 @@ export default function useStepHandler({
}
if (runStep.stepDetails.type === StepTypes.TOOL_CALLS) {
const streamedCallCount = runStep.stepDetails.tool_calls?.length ?? 0;
let updatedResponse = { ...response };
const contentIndex = runStep.index + editPrefixOffset;
((runStep.stepDetails.tool_calls ?? []) as Agents.ToolCall[]).forEach((toolCall) => {
const toolCallId = toolCall.id ?? '';
if ('id' in toolCall && toolCallId) {
toolCallIdMap.current.set(runStep.id, toolCallId);
}
const contentPart: Agents.MessageContentComplex = {
type: ContentTypes.TOOL_CALL,
tool_call: {
name: toolCall.name ?? '',
args: toolCall.args,
id: toolCallId,
stepId: runStep.id,
toolPreparationStartedAt:
firstFragmentByCall.current.get(toolTimingKey(runStep.id, toolCallId)) ??
(streamedCallCount <= 1
? firstFragmentByStep.current.get(runStep.id)
: undefined),
toolDispatchedAt: dispatchedByCall.current.get(
toolTimingKey(runStep.id, toolCallId),
),
},
};
updatedResponse = updateContent(
updatedResponse,
contentIndex,
contentPart,
false,
getStepMetadata(runStep),
);
});
const { message: updatedResponse, toolCallId } = applyToolCallsStep(
response,
runStep,
editPrefixOffset,
);
if (toolCallId) {
toolCallIdMap.current.set(runStep.id, toolCallId);
}
messageMap.current.set(responseMessageId, updatedResponse);
setMessages(
mergeResponseMessage(messages, updatedResponse, responseMessageId, {
@ -886,50 +825,16 @@ export default function useStepHandler({
}
const response = messageMap.current.get(responseMessageId);
if (
const updatedResponse =
response &&
runStepDelta.delta.type === StepTypes.TOOL_CALLS &&
runStepDelta.delta.tool_calls
) {
let updatedResponse = { ...response };
runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {
const toolCallId = toolCallDelta.id || toolCallIdMap.current.get(runStepDelta.id) || '';
const contentPart: Agents.MessageContentComplex = {
type: ContentTypes.TOOL_CALL,
tool_call: {
name: toolCallDelta.name ?? '',
args: toolCallDelta.args ?? '',
id: toolCallId,
stepId: runStepDelta.id,
toolPreparationStartedAt:
firstFragmentByCall.current.get(toolTimingKey(runStepDelta.id, toolCallId)) ??
(toolCallDelta.index === 0
? firstFragmentByStep.current.get(runStepDelta.id)
: undefined),
toolDispatchedAt: dispatchedByCall.current.get(
toolTimingKey(runStepDelta.id, toolCallId),
),
},
};
if (runStepDelta.delta.auth != null) {
contentPart.tool_call.auth = runStepDelta.delta.auth;
contentPart.tool_call.expires_at = runStepDelta.delta.expires_at;
}
// Use server's index, offset by the retained edit prefix
const currentIndex = runStep.index + editPrefixOffset;
updatedResponse = updateContent(
updatedResponse,
currentIndex,
contentPart,
false,
getStepMetadata(runStep),
);
});
applyToolCallDelta(
response,
runStep,
runStepDelta,
toolCallIdMap.current.get(runStepDelta.id) ?? '',
editPrefixOffset,
);
if (updatedResponse) {
messageMap.current.set(responseMessageId, updatedResponse);
setMessages(
mergeResponseMessage(messages, updatedResponse, responseMessageId, {
@ -1005,23 +910,12 @@ export default function useStepHandler({
const response = messageMap.current.get(responseMessageId);
if (response) {
let updatedResponse = { ...response };
const contentPart: Agents.MessageContentComplex = {
type: ContentTypes.TOOL_CALL,
tool_call: { ...result.tool_call, stepId },
};
// Use server's index, offset by the retained edit prefix
const currentIndex = runStep.index + editPrefixOffset;
updatedResponse = updateContent(
updatedResponse,
currentIndex,
contentPart,
true,
getStepMetadata(runStep),
const updatedResponse = applyToolCallCompleted(
response,
runStep,
result,
editPrefixOffset,
);
messageMap.current.set(responseMessageId, updatedResponse);
setMessages(
mergeResponseMessage(messages, updatedResponse, responseMessageId, {
@ -1052,58 +946,10 @@ export default function useStepHandler({
return;
}
const currentIndex = runStep.index + editPrefixOffset;
const existing = response.content?.[currentIndex];
/**
* Only tool calls render a running state, so only they need the
* terminal status. Leaving other part types untouched keeps this from
* disturbing text or reasoning content.
*/
if (!existing || existing.type !== ContentTypes.TOOL_CALL) {
const updatedResponse = applyRunStepClosed(response, runStep, closed, editPrefixOffset);
if (!updatedResponse) {
return;
}
const existingToolCall = existing[ContentTypes.TOOL_CALL];
if (!existingToolCall) {
return;
}
/** Spread conditionally so an unknowable duration leaves any value the
* server already stamped in place, rather than overwriting it with
* `undefined`. */
const durationMs = getRunStepDurationMs(closed);
const callId = existingToolCall.id ?? '';
const key = toolTimingKey(closed.id, callId);
const singleCallStep =
runStep.stepDetails.type === StepTypes.TOOL_CALLS &&
(runStep.stepDetails.tool_calls?.length ?? 0) <= 1;
const observedAt = Math.min(
firstFragmentByCall.current.get(key) ?? Infinity,
existingToolCall.toolPreparationStartedAt ?? Infinity,
singleCallStep ? (firstFragmentByStep.current.get(closed.id) ?? Infinity) : Infinity,
);
const timing = getToolTimingDurations({
observedAt: Number.isFinite(observedAt) ? observedAt : undefined,
dispatchedAt: existingToolCall.toolDispatchedAt ?? dispatchedByCall.current.get(key),
completedAt: completedByCall.current.get(key),
});
firstFragmentByCall.current.delete(key);
firstFragmentByStep.current.delete(closed.id);
dispatchedByCall.current.delete(key);
completedByCall.current.delete(key);
const updatedContent = [...(response.content ?? [])];
updatedContent[currentIndex] = {
...existing,
[ContentTypes.TOOL_CALL]: {
...existingToolCall,
runStepStatus: closed.status,
...getRunStepCloseMetadata(closed),
...(durationMs != null && { runStepDurationMs: durationMs }),
...timing,
},
};
const updatedResponse = { ...response, content: updatedContent };
messageMap.current.set(responseMessageId, updatedResponse);
setMessages(
mergeResponseMessage(messages, updatedResponse, responseMessageId, {

View file

@ -0,0 +1,69 @@
import { expect, test } from '@playwright/test';
import type { Locator, Page } from '@playwright/test';
import {
sendMessageAndWaitForCompletion,
enableCodeInterpreter,
selectMockEndpoint,
MOCK_ENDPOINTS,
NEW_CHAT_PATH,
messagesView,
} from '../helpers';
/**
* A tool call is opened by its run step, grows through argument deltas and is
* settled by its completion, all folded into one part at the step's index by
* the tool reducers in `client/src/hooks/SSE/steps/tools.ts`.
*/
const FINAL_TEXT = 'E2E execute_code complete';
const TOOL_OUTPUT = 'stdout: E2E code exec ok';
const uniqueLabel = (prefix: string) =>
`${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
/** The code-execution card's status toggle; a slow run appends its duration to the name. */
const toolCard = (page: Page) =>
messagesView(page).getByRole('button', { name: /^Finished running/ });
const toolOutput = (page: Page) => messagesView(page).getByText(TOOL_OUTPUT, { exact: true });
/** True when `first` sits before `second` in document order. */
async function precedes(first: Locator, second: Locator): Promise<boolean> {
const handle = await second.elementHandle();
return first.evaluate(
(node, other) =>
other != null && (node.compareDocumentPosition(other) & Node.DOCUMENT_POSITION_FOLLOWING) > 0,
handle,
);
}
async function expectOneToolCardBeforeReply(page: Page, label: string) {
const reply = messagesView(page).getByText(`${FINAL_TEXT}: ${label}`, { exact: true });
await expect(reply).toBeVisible({ timeout: 30_000 });
await expect(reply).toHaveCount(1);
await expect(toolCard(page)).toHaveCount(1, { timeout: 30_000 });
await expect(toolOutput(page)).toHaveCount(1);
expect(await precedes(toolCard(page), reply)).toBe(true);
expect(await precedes(toolOutput(page), reply)).toBe(true);
}
test.describe('tool call steps', () => {
test('a tool call renders once, ahead of the reply that follows it, live and after reload @scenario:tool-call-renders-once-before-its-reply', async ({
page,
}) => {
test.setTimeout(120_000);
const label = uniqueLabel('tool');
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);
await enableCodeInterpreter(page);
const response = await sendMessageAndWaitForCompletion(page, `E2E_EXECUTE_CODE:${label}`);
expect(response.ok()).toBeTruthy();
await expect(page.getByRole('button', { name: 'Stop generating' })).toBeHidden({
timeout: 30_000,
});
await expectOneToolCardBeforeReply(page, label);
await page.reload({ timeout: 10_000 });
await expectOneToolCardBeforeReply(page, label);
});
});