mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 07:23:11 +00:00
🚩 fix: Preserve Client Tool Call Finish Reason After Text (#16408)
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
3c72c3fb4a
commit
2278da115f
7 changed files with 95 additions and 16 deletions
|
|
@ -576,7 +576,7 @@ describe('OpenAIChatCompletionController', () => {
|
|||
);
|
||||
|
||||
it.each([true, false])(
|
||||
'publishes only the accepted client call after a model claim (stream=%s)',
|
||||
'publishes accepted client calls after text and a model claim (stream=%s)',
|
||||
async (streaming) => {
|
||||
const api = require('@librechat/api');
|
||||
const actual = jest.requireActual('@librechat/api');
|
||||
|
|
@ -608,6 +608,9 @@ describe('OpenAIChatCompletionController', () => {
|
|||
agentId: 'agent-123',
|
||||
messageId: 'claimed-message',
|
||||
});
|
||||
await handlers.on_message_delta.handle('on_message_delta', {
|
||||
delta: { content: [{ type: 'text', text: 'Checking the weather.' }] },
|
||||
});
|
||||
await handlers.on_model_response.handle('on_model_response', {
|
||||
type: 'model_response',
|
||||
id: 'accepted-client',
|
||||
|
|
@ -627,6 +630,9 @@ describe('OpenAIChatCompletionController', () => {
|
|||
.map(([frame]) => frame)
|
||||
.filter((frame) => frame !== 'data: [DONE]\n\n')
|
||||
.map((frame) => JSON.parse(frame.slice(6)));
|
||||
expect(
|
||||
frames.some((frame) => frame.choices[0].delta.content === 'Checking the weather.'),
|
||||
).toBe(true);
|
||||
const calls = frames.flatMap((frame) => frame.choices[0].delta.tool_calls ?? []);
|
||||
expect(calls).toEqual([
|
||||
{ index: 0, id: 'a', type: 'function', function: { name: 'get_time', arguments: '' } },
|
||||
|
|
@ -642,6 +648,7 @@ describe('OpenAIChatCompletionController', () => {
|
|||
expect(frames.at(-1).choices[0].finish_reason).toBe('tool_calls');
|
||||
} else {
|
||||
const choice = res.json.mock.calls[0][0].choices[0];
|
||||
expect(choice.message.content).toBe('Checking the weather.');
|
||||
expect(choice.message.tool_calls).toEqual([
|
||||
{
|
||||
id: 'a',
|
||||
|
|
@ -909,6 +916,7 @@ describe('OpenAIChatCompletionController', () => {
|
|||
expect.anything(),
|
||||
expect.anything(),
|
||||
mockCompletionUsage,
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -920,7 +928,12 @@ describe('OpenAIChatCompletionController', () => {
|
|||
|
||||
await OpenAIChatCompletionController(req, res);
|
||||
|
||||
expect(sendFinalChunk).toHaveBeenCalledWith(expect.anything(), 'stop', mockCompletionUsage);
|
||||
expect(sendFinalChunk).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
'stop',
|
||||
mockCompletionUsage,
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
describe('content filtering', () => {
|
||||
|
|
|
|||
|
|
@ -1180,7 +1180,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
|
|||
// Finalize response
|
||||
const duration = Date.now() - requestStartTime;
|
||||
if (isStreaming) {
|
||||
sendFinalChunk(handlerConfig, 'stop', usage);
|
||||
sendFinalChunk(handlerConfig, 'stop', usage, true);
|
||||
res.end();
|
||||
logger.debug(`[OpenAI API] Response ${responseId} completed in ${duration}ms (streaming)`);
|
||||
|
||||
|
|
@ -1216,6 +1216,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
|
|||
aggregator.getReasoning(),
|
||||
aggregator.toolCalls,
|
||||
usage,
|
||||
true,
|
||||
);
|
||||
res.json(response);
|
||||
logger.debug(
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ Missing names, missing arguments, malformed JSON or unattributable argument data
|
|||
|
||||
## Accepted-result terminal behavior
|
||||
|
||||
The SDK projector accepts only client-disposition calls from graph-accepted responses. A newer accepted text/internal result or a model-tool claim discards superseded calls for that agent. Calls are validated, given unique outward IDs and emitted at successful response completion; failed or aborted runs never publish buffered calls. Internal tools followed by a text answer finish with `stop` and no outward calls. The shared response builders use `tool_calls` as the finish reason when client calls exist without text.
|
||||
The SDK projector accepts only client-disposition calls from graph-accepted responses. A newer accepted text/internal result or a model-tool claim discards superseded calls for that agent. Calls are validated, given unique outward IDs and emitted at successful response completion; failed or aborted runs never publish buffered calls. Internal tools followed by a text answer finish with `stop` and no outward calls. Both in-repo hosts explicitly mark the SDK map as accepted-only when calling the shared serializers: if any accepted client calls remain, the final reason is `tool_calls`, even when text was already emitted. Compatibility consumers of the legacy run-step projector retain the previous `stop` finish reason for historical calls followed by text.
|
||||
|
||||
## Verification and limits
|
||||
|
||||
|
|
|
|||
|
|
@ -50,13 +50,18 @@ describe('OpenAI-compatible agent stream handlers', () => {
|
|||
invalidToolCalls: [],
|
||||
});
|
||||
expect(frames).toHaveLength(3);
|
||||
sendFinalChunk(config, 'stop', {
|
||||
prompt_tokens: 12,
|
||||
completion_tokens: 3,
|
||||
total_tokens: 15,
|
||||
primary: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },
|
||||
subagent: { prompt_tokens: 2, completion_tokens: 1, total_tokens: 3 },
|
||||
});
|
||||
sendFinalChunk(
|
||||
config,
|
||||
'stop',
|
||||
{
|
||||
prompt_tokens: 12,
|
||||
completion_tokens: 3,
|
||||
total_tokens: 15,
|
||||
primary: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },
|
||||
subagent: { prompt_tokens: 2, completion_tokens: 1, total_tokens: 3 },
|
||||
},
|
||||
true,
|
||||
);
|
||||
return frames;
|
||||
};
|
||||
|
||||
|
|
@ -75,7 +80,7 @@ describe('OpenAI-compatible agent stream handlers', () => {
|
|||
},
|
||||
{},
|
||||
]);
|
||||
expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('stop');
|
||||
expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('tool_calls');
|
||||
expect(chunks[chunks.length - 1].usage).toMatchObject({
|
||||
total_tokens: 15,
|
||||
subagent: { total_tokens: 3 },
|
||||
|
|
|
|||
|
|
@ -807,14 +807,16 @@ export function sendFinalChunk(
|
|||
config: OpenAIContentStreamConfig,
|
||||
finishReason: ChatCompletionChunkChoice['finish_reason'] = 'stop',
|
||||
usageOverride?: CompletionUsage,
|
||||
/** True when the map contains only accepted client-owned calls, not legacy run-step history. */
|
||||
acceptedToolCallsOnly = false,
|
||||
): void {
|
||||
const { context, tracker } = config;
|
||||
const writer = getStreamWriter(config);
|
||||
tracker.finishToolCalls?.();
|
||||
|
||||
// Determine finish reason based on content
|
||||
// Legacy run-step history followed by text stays 'stop'; accepted client calls must be executed.
|
||||
let reason = finishReason;
|
||||
if (tracker.toolCalls.size > 0 && !tracker.hasText) {
|
||||
if (tracker.toolCalls.size > 0 && (acceptedToolCallsOnly || !tracker.hasText)) {
|
||||
reason = 'tool_calls';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -161,6 +161,60 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
|
|||
},
|
||||
);
|
||||
|
||||
it.each([true, false])(
|
||||
'finishes with tool_calls when an accepted client call follows text (stream=%s)',
|
||||
async (stream) => {
|
||||
const req = createMockReq(
|
||||
{ id: 'user' },
|
||||
{ model: 'agent_test', messages: [{ role: 'user', content: 'hi' }], stream },
|
||||
);
|
||||
const res = createMockRes();
|
||||
processStream.mockImplementationOnce(async () => {
|
||||
const { customHandlers: handlers } = createRun.mock.calls[0][0] as Parameters<
|
||||
NonNullable<ChatCompletionDependencies['createRun']>
|
||||
>[0];
|
||||
await handlers.on_message_delta.handle('on_message_delta', {
|
||||
delta: { content: [{ type: 'text', text: 'Checking the weather.' }] },
|
||||
});
|
||||
await handlers.on_model_response.handle('on_model_response', {
|
||||
type: 'model_response',
|
||||
id: 'accepted-client',
|
||||
agentId: 'agent_test',
|
||||
toolCalls: [{ id: 'client', name: 'lookup', args: { city: 'Madrid' } }],
|
||||
toolCallDispositions: ['client'],
|
||||
invalidToolCalls: [],
|
||||
});
|
||||
});
|
||||
|
||||
await createAgentChatCompletion(req, res, deps);
|
||||
|
||||
if (stream) {
|
||||
const chunks: ChatCompletionChunk[] = (res.write as jest.Mock).mock.calls
|
||||
.map(([frame]: [string]) => frame)
|
||||
.filter((frame) => frame !== 'data: [DONE]\n\n')
|
||||
.map((frame) => JSON.parse(frame.slice(6)));
|
||||
expect(
|
||||
chunks.some((chunk) => chunk.choices[0].delta.content === 'Checking the weather.'),
|
||||
).toBe(true);
|
||||
expect(
|
||||
chunks
|
||||
.flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])
|
||||
.some((call) => call.id === 'client'),
|
||||
).toBe(true);
|
||||
expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('tool_calls');
|
||||
} else {
|
||||
const response = getResponseMock(res, 'json').mock.calls[0][0] as {
|
||||
choices: [
|
||||
{ message: { content: string; tool_calls: [{ id: string }] }; finish_reason: string },
|
||||
];
|
||||
};
|
||||
expect(response.choices[0].message.content).toBe('Checking the weather.');
|
||||
expect(response.choices[0].message.tool_calls[0].id).toBe('client');
|
||||
expect(response.choices[0].finish_reason).toBe('tool_calls');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.each([true, false])(
|
||||
'does not publish fallback arguments after provider failure (stream=%s)',
|
||||
async (stream) => {
|
||||
|
|
|
|||
|
|
@ -570,9 +570,12 @@ export function buildNonStreamingResponse(
|
|||
reasoning: string,
|
||||
toolCalls: Map<number, ToolCall>,
|
||||
usage: CompletionUsage,
|
||||
/** True when the map contains only accepted client-owned calls, not legacy run-step history. */
|
||||
acceptedToolCallsOnly = false,
|
||||
): ChatCompletionResponse {
|
||||
const toolCallsArray = Array.from(toolCalls.values());
|
||||
const finishReason = toolCallsArray.length > 0 && !text ? 'tool_calls' : 'stop';
|
||||
const finishReason =
|
||||
toolCallsArray.length > 0 && (acceptedToolCallsOnly || !text) ? 'tool_calls' : 'stop';
|
||||
|
||||
return {
|
||||
id: context.requestId,
|
||||
|
|
@ -920,7 +923,7 @@ export async function createAgentChatCompletion(
|
|||
|
||||
// Finalize response
|
||||
if (isStreaming && handlerConfig) {
|
||||
sendFinalChunk(handlerConfig);
|
||||
sendFinalChunk(handlerConfig, 'stop', undefined, true);
|
||||
res.end();
|
||||
} else if (aggregator) {
|
||||
aggregator.finishToolCalls?.();
|
||||
|
|
@ -939,6 +942,7 @@ export async function createAgentChatCompletion(
|
|||
aggregator.getReasoning(),
|
||||
aggregator.toolCalls,
|
||||
usage,
|
||||
true,
|
||||
);
|
||||
res.json(response);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue