Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions core/llm/openaiTypeConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,55 @@ describe("openaiTypeConverters", () => {
expect(functionCalls[0].id).toBe("fc_001");
});

it("should emit assistant message before function_call when reasoning and tool calls share a turn", () => {
const messages: ChatMessage[] = [
{
role: "thinking",
content: "",
reasoning_details: [
{ type: "reasoning_id", id: "rs_001" },
{
type: "encrypted_content",
encrypted_content: "encrypted_data_here",
},
],
metadata: { reasoningId: "rs_001" },
} as ChatMessage,
{
role: "assistant",
content: "I'll inspect the file first.",
toolCalls: [
{
id: "call_001",
type: "function",
function: { name: "read_file", arguments: '{"path":"a.txt"}' },
},
],
metadata: {
responsesOutputItemIds: ["msg_001", "fc_001"],
responsesOutputItemId: "fc_001",
},
} as ChatMessage,
];

const result = toResponsesInput(messages);

expect(result[0]).toMatchObject({
type: "reasoning",
id: "rs_001",
});
expect(result[1]).toMatchObject({
type: "message",
role: "assistant",
id: "msg_001",
});
expect(result[2]).toMatchObject({
type: "function_call",
id: "fc_001",
call_id: "call_001",
});
});

it("should strip fc_ id from function_calls after removed reasoning", () => {
const messages: ChatMessage[] = [
{
Expand Down
4 changes: 2 additions & 2 deletions core/llm/openaiTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,6 @@ export function toResponsesInput(messages: ChatMessage[]): ResponseInput {
(respId?.startsWith("msg_") ? respId : undefined);

if (Array.isArray(toolCalls) && toolCalls.length > 0) {
emitFunctionCallsFromToolCalls(toolCalls, fcIds, input);

if (text && text.trim()) {
if (msgId) {
const outputMessageItem: ResponseOutputMessage = {
Expand All @@ -1060,6 +1058,8 @@ export function toResponsesInput(messages: ChatMessage[]): ResponseInput {
pushMessage("assistant", text);
}
}

emitFunctionCallsFromToolCalls(toolCalls, fcIds, input);
} else if (msgId) {
const outputMessageItem: ResponseOutputMessage = {
id: msgId,
Expand Down
Loading