refactor: 将 convertMessagesToLangfuse 参数类型从 unknown 收窄为联合类型

将 readonly unknown[] 改为 readonly LangfuseInputMessage[],
其中 LangfuseInputMessage = UserMessage | AssistantMessage | ChatCompletionMessageParam,
让调用方获得编译期类型检查。
This commit is contained in:
claude-code-best
2026-04-26 09:28:13 +08:00
parent 4591432a1d
commit dff678924e
2 changed files with 11 additions and 3 deletions

View File

@@ -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([
{

View File

@@ -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<string, unknown>): LangfuseContentPart | null {
const type = block.type as string | undefined
@@ -178,7 +185,7 @@ function toRoleFromWrappedMessage(msg: Record<string, unknown>): '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[] = []