mapChatMessagesToModelMessages (ai-sdk/messages.ts)

langfuse · packages/shared/src/server/llm/ai-sdk/messages.ts · typescript · MIT
ai, tag:embed-classified

Module exposing: mapChatMessagesToModelMessages

// Source: https://github.com/langfuse/langfuse | License: MIT | Copyright 2023-2026 ClickHouse, Inc
// Provided via justclaw.it.com; keep this notice with the code.
import type { AssistantContent, ModelMessage } from "ai";

import { LLMValidationError } from "../errors";
import {
  ChatMessage,
  ChatMessageRole,
  ChatMessageType,
  LLMAdapter,
  LLMToolCall,
  PROVIDERS_WITH_REQUIRED_USER_MESSAGE,
} from "../types";

// Helper function to safely stringify content
const safeStringify = (content: unknown): string => {
  try {
    return JSON.stringify(content);
  } catch {
    return "[Unserializable content]";
  }
};

const toSafeContent = (content: unknown): string =>
  typeof content === "string" ? content : safeStringify(content);

/**
 * Maps persisted/playground Langfuse `ChatMessag

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key