mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 22:35:51 +00:00
refactor: 将 convertMessagesToLangfuse 参数类型从 unknown 收窄为联合类型
将 readonly unknown[] 改为 readonly LangfuseInputMessage[], 其中 LangfuseInputMessage = UserMessage | AssistantMessage | ChatCompletionMessageParam, 让调用方获得编译期类型检查。
This commit is contained in:
@@ -231,6 +231,7 @@ describe('Langfuse integration', () => {
|
|||||||
|
|
||||||
test('merges assistant tool calls from OpenAI-style array content', async () => {
|
test('merges assistant tool calls from OpenAI-style array content', async () => {
|
||||||
const { convertMessagesToLangfuse } = await import('../convert.js')
|
const { convertMessagesToLangfuse } = await import('../convert.js')
|
||||||
|
// Content part with embedded tool_calls is non-standard; cast for defensive test
|
||||||
const result = convertMessagesToLangfuse([
|
const result = convertMessagesToLangfuse([
|
||||||
{
|
{
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
@@ -255,7 +256,7 @@ describe('Langfuse integration', () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
])
|
] as any)
|
||||||
|
|
||||||
expect(result).toEqual([
|
expect(result).toEqual([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
* - tool_result blocks → separate { role: 'tool' } messages
|
* - 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 LangfuseContentPart =
|
||||||
| { type: 'text'; text: string }
|
| { type: 'text'; text: string }
|
||||||
@@ -79,6 +80,12 @@ function mergeToolCalls(
|
|||||||
return [...merged.values()]
|
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) */
|
/** Normalize a content block into a LangfuseContentPart (non-tool_use, non-tool_result) */
|
||||||
function toContentPart(block: Record<string, unknown>): LangfuseContentPart | null {
|
function toContentPart(block: Record<string, unknown>): LangfuseContentPart | null {
|
||||||
const type = block.type as string | undefined
|
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 */
|
/** Convert internal or OpenAI-style messages → Langfuse input format */
|
||||||
export function convertMessagesToLangfuse(
|
export function convertMessagesToLangfuse(
|
||||||
messages: readonly unknown[],
|
messages: readonly LangfuseInputMessage[],
|
||||||
systemPrompt?: readonly string[],
|
systemPrompt?: readonly string[],
|
||||||
): LangfuseChatMessage[] {
|
): LangfuseChatMessage[] {
|
||||||
const result: LangfuseChatMessage[] = []
|
const result: LangfuseChatMessage[] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user