From dff678924e48b9555db026bedc012e6c8b7cfb6e Mon Sep 17 00:00:00 2001 From: claude-code-best Date: Sun, 26 Apr 2026 09:28:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20convertMessagesToLangfu?= =?UTF-8?q?se=20=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B=E4=BB=8E=20unknown=20?= =?UTF-8?q?=E6=94=B6=E7=AA=84=E4=B8=BA=E8=81=94=E5=90=88=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 readonly unknown[] 改为 readonly LangfuseInputMessage[], 其中 LangfuseInputMessage = UserMessage | AssistantMessage | ChatCompletionMessageParam, 让调用方获得编译期类型检查。 --- src/services/langfuse/__tests__/langfuse.test.ts | 3 ++- src/services/langfuse/convert.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/services/langfuse/__tests__/langfuse.test.ts b/src/services/langfuse/__tests__/langfuse.test.ts index dab33fd60..6c5a87947 100644 --- a/src/services/langfuse/__tests__/langfuse.test.ts +++ b/src/services/langfuse/__tests__/langfuse.test.ts @@ -231,6 +231,7 @@ describe('Langfuse integration', () => { test('merges assistant tool calls from OpenAI-style array content', async () => { const { convertMessagesToLangfuse } = await import('../convert.js') + // Content part with embedded tool_calls is non-standard; cast for defensive test const result = convertMessagesToLangfuse([ { role: 'assistant', @@ -255,7 +256,7 @@ describe('Langfuse integration', () => { }, ], }, - ]) + ] as any) expect(result).toEqual([ { diff --git a/src/services/langfuse/convert.ts b/src/services/langfuse/convert.ts index ad324c2a0..411692bce 100644 --- a/src/services/langfuse/convert.ts +++ b/src/services/langfuse/convert.ts @@ -10,7 +10,8 @@ * - tool_result blocks → separate { role: 'tool' } messages */ -import type { AssistantMessage } from 'src/types/message.js' +import type { AssistantMessage, UserMessage } from 'src/types/message.js' +import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions/completions.mjs' type LangfuseContentPart = | { type: 'text'; text: string } @@ -79,6 +80,12 @@ function mergeToolCalls( return [...merged.values()] } +/** Union of all message formats accepted by Langfuse converters. */ +type LangfuseInputMessage = + | UserMessage + | AssistantMessage + | ChatCompletionMessageParam + /** Normalize a content block into a LangfuseContentPart (non-tool_use, non-tool_result) */ function toContentPart(block: Record): LangfuseContentPart | null { const type = block.type as string | undefined @@ -178,7 +185,7 @@ function toRoleFromWrappedMessage(msg: Record): 'user' | 'assis /** Convert internal or OpenAI-style messages → Langfuse input format */ export function convertMessagesToLangfuse( - messages: readonly unknown[], + messages: readonly LangfuseInputMessage[], systemPrompt?: readonly string[], ): LangfuseChatMessage[] { const result: LangfuseChatMessage[] = []