From 2fa6acb0587789944825915ba87d2674affa94ce Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 13 Dec 2024 20:32:41 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20improve=20mess?= =?UTF-8?q?age=20conversion=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/anthropic.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/core/src/anthropic.ts b/packages/core/src/anthropic.ts index 98bec02ca2..020219ee4d 100644 --- a/packages/core/src/anthropic.ts +++ b/packages/core/src/anthropic.ts @@ -40,6 +40,7 @@ import { HttpsProxyAgent } from "https-proxy-agent" import { MarkdownTrace } from "./trace" import { createFetch, FetchType } from "./fetch" import { JSONLLMTryParse } from "./json5" +import { groupBy } from "es-toolkit" const convertFinishReason = ( stopReason: Anthropic.Message["stop_reason"] @@ -91,13 +92,30 @@ const adjustUsage = ( const convertMessages = ( messages: ChatCompletionMessageParam[] -): Array => { - return messages.map(convertSingleMessage) +): Anthropic.Beta.PromptCaching.PromptCachingBetaMessageParam[] => { + const res: Anthropic.Beta.PromptCaching.PromptCachingBetaMessageParam[] = [] + for (const msg of messages.map(convertSingleMessage)) { + const last = res.at(-1) + if (last?.role !== msg.role) res.push(msg) + else { + if (typeof last.content === "string") + last.content = [ + { + type: "text", + text: last.content, + }, + ] + if (typeof msg.content === "string") + last.content.push({ type: "text", text: msg.content }) + else last.content.push(...msg.content) + } + } + return res } const convertSingleMessage = ( msg: ChatCompletionMessageParam -): Anthropic.Messages.MessageParam => { +): Anthropic.Beta.PromptCaching.PromptCachingBetaMessageParam => { const { role } = msg if (!role || role === "aici") { // Handle AICIRequest or other custom types