Guides
Message-to-LLM Converter
`toAiMessages()` converts FluxyChat messages into the message format expected by AI SDKs (OpenAI, Anthropic, Vercel AI SDK, etc.).
Message-to-LLM Converter
toAiMessages() converts FluxyChat messages into the message format expected by AI SDKs (OpenAI, Anthropic, Vercel AI SDK, etc.).
When to Use
- Feeding chat history to an AI agent
- Converting a conversation thread into context for
generateTextorstreamText - Building AI-powered chat features on top of FluxyChat
Usage
import { toAiMessages } from "@fluxy-chat/sdk";
const messages = await client.getMessages("room-1", { limit: 50 });
const history = await toAiMessages(messages, {
botUserId: "agent-bot-123",
});
// Use with any AI SDK
const response = await openai.chat.completions.create({
model: "gpt-4",
messages: history,
});Options
botUserId
Identifies which user is the bot/agent. Messages from this user are mapped to role: "assistant", all others to role: "user".
toAiMessages(messages, { botUserId: "my-bot-id" });includeNames
When true, prefixes user messages with [userId]: for multi-user context:
toAiMessages(messages, { includeNames: true });
// User message → "[alice]: hello there"transformMessage
Hook to modify or skip individual messages:
toAiMessages(messages, {
transformMessage: (aiMessage, sourceMsg) => {
// Add prefix to all messages
return { ...aiMessage, content: `[${sourceMsg.roomId}] ${aiMessage.content}` };
// Or return null to skip
},
});onUnsupportedAttachment
Callback for attachment types that can't be converted (audio, video):
toAiMessages(messages, {
onUnsupportedAttachment: (att, msg) => {
console.warn(`Can't include ${att.kind} from message ${msg.id}`);
},
});How It Works
- Messages are sorted chronologically by
createdAt - Empty/whitespace-only content is filtered out
- Bot messages become
assistant, user messages becomeuser - Link previews are appended as text
- Image and text files are converted to
FilePartusing their URL - Unsupported attachments trigger the callback
Output Format
type AiMessage =
| { role: "user"; content: string | AiMessagePart[] }
| { role: "assistant"; content: string };This format is compatible with the Vercel AI SDK's ModelMessage type and most LLM provider APIs.
See Also
- Provider Registry Guide — Managing AI providers
- LLM Middleware Guide — Transforming AI SDK calls