Skip to content

Commit 8f65339

Browse files
committed
fix: 保留 Langfuse OpenAI 数组消息角色
1 parent 3acd375 commit 8f65339

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/services/langfuse/__tests__/langfuse.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,26 @@ describe('Langfuse integration', () => {
208208
},
209209
])
210210
})
211+
212+
test('preserves roles for OpenAI-style array content messages', async () => {
213+
const { convertMessagesToLangfuse } = await import('../convert.js')
214+
const result = convertMessagesToLangfuse([
215+
{
216+
role: 'system',
217+
content: [{ type: 'text', text: 'system reminder' }],
218+
},
219+
{
220+
role: 'tool',
221+
tool_call_id: 'call_1',
222+
content: [{ type: 'text', text: 'tool output' }],
223+
},
224+
])
225+
226+
expect(result).toEqual([
227+
{ role: 'system', content: 'system reminder' },
228+
{ role: 'tool', content: 'tool output', tool_call_id: 'call_1' },
229+
])
230+
})
211231
})
212232

213233
// ── client tests ────────────────────────────────────────────────────────────

src/services/langfuse/convert.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,16 @@ export function convertMessagesToLangfuse(
196196
.map(b => toContentPart(b))
197197
.filter((p): p is LangfuseContentPart => p !== null)
198198
if (parts.length > 0 || toolMessages.length === 0) {
199-
result.push({ role: 'user', content: collapseContent(parts) })
199+
result.push({
200+
role,
201+
content: collapseContent(parts),
202+
...('tool_call_id' in inner && typeof inner.tool_call_id === 'string'
203+
? { tool_call_id: inner.tool_call_id }
204+
: {}),
205+
...('tool_calls' in inner && Array.isArray(inner.tool_calls)
206+
? { tool_calls: inner.tool_calls as LangfuseToolCall[] }
207+
: {}),
208+
})
200209
}
201210
result.push(...toolMessages)
202211
}

0 commit comments

Comments
 (0)