@cometchat/vercel-adapter
v0.0.1-beta
Published
A TypeScript library for CometChat integration with Vercel
Readme
CometChat Vercel AI SDK Adapter
A TypeScript adapter library that enables seamless integration between CometChat and Vercel's AI SDK. This library provides conversion utilities to bridge the gap between CometChat's messaging format and Vercel AI SDK's expected data structures.
Installation
Install the package using npm:
npm install @cometchat/vercel-adapterOr using yarn:
yarn add @cometchat/vercel-adapterWhat is this library?
This adapter library facilitates the integration of CometChat's AI capabilities with Vercel's AI SDK by providing:
- Message Format Conversion: Converts CometChat messages to Vercel AI SDK compatible format
- Tool Integration: Adapts CometChat tools to work with Vercel AI SDK's tool system
- Stream Event Mapping: Maps Vercel AI SDK streaming events to CometChat event format
- Type Safety: Full TypeScript support with proper type definitions
Functions Exposed
convertCometChatMessagesToVercelMessages(messages: Message[]): ModelMessage[]
Converts an array of CometChat messages to Vercel AI SDK compatible ModelMessage format.
Parameters:
messages: Array of CometChatMessageobjects
Returns:
- Array of
ModelMessageobjects compatible with Vercel AI SDK
Example:
import { convertCometChatMessagesToVercelMessages } from '@cometchat/vercel-adapter';
const cometChatMessages = [
{ role: 'user', content: 'Hello, how can you help me?' },
{ role: 'assistant', content: 'I can help you with various tasks!', toolCalls: [] }
];
const vercelMessages = convertCometChatMessagesToVercelMessages(cometChatMessages);convertCometChatToolsToVercelAISDKTools(tools: RunAgentInput["tools"]): any
Converts CometChat tools to Vercel AI SDK compatible tool format with Zod schema validation.
Parameters:
tools: Array of CometChat tool definitions
Returns:
- Object containing Vercel AI SDK compatible tools
Example:
import { convertCometChatToolsToVercelAISDKTools } from '@cometchat/vercel-adapter';
const cometChatTools = [
{
name: 'search',
description: 'Search for information',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query' }
},
required: ['query']
}
}
];
const vercelTools = convertCometChatToolsToVercelAISDKTools(cometChatTools);mapVercelStreamChunkToCometChatEvent(chunk: TextStreamPart<any>): CometChatStreamEvent[]
Maps Vercel AI SDK streaming chunks to CometChat event format for real-time communication.
Parameters:
chunk: ATextStreamPartfrom Vercel AI SDK stream
Returns:
- Array of
CometChatStreamEventobjects
Supported Stream Events:
text-start,text-delta,text-end- Text message eventstool-call,tool-result,tool-error- Tool execution eventsreasoning-start,reasoning-delta,reasoning-end- AI reasoning eventserror,finish,start- Stream lifecycle events
Example:
import { mapVercelStreamChunkToCometChatEvent } from '@cometchat/vercel-adapter';
// During streaming from Vercel AI SDK
streamResult.onTextChunk((chunk) => {
const cometChatEvents = mapVercelStreamChunkToCometChatEvent(chunk);
// Process CometChat events
cometChatEvents.forEach(event => {
console.log('CometChat event:', event);
});
});Usage Example
Here's a complete example showing how to use the adapter in a typical integration:
import {
convertCometChatMessagesToVercelMessages,
convertCometChatToolsToVercelAISDKTools,
mapVercelStreamChunkToCometChatEvent
} from '@cometchat/vercel-adapter';
import { streamText } from 'ai';
// Convert CometChat messages and tools
const vercelMessages = convertCometChatMessagesToVercelMessages(cometChatMessages);
const vercelTools = convertCometChatToolsToVercelAISDKTools(cometChatTools);
// Use with Vercel AI SDK
const result = await streamText({
model: yourModel,
messages: vercelMessages,
tools: vercelTools,
});
// Handle streaming responses
for await (const chunk of result.textStream) {
const cometChatEvents = mapVercelStreamChunkToCometChatEvent(chunk);
// Process events in your CometChat integration
}Development
Building the Library
npm run buildRunning Tests
npm run testDevelopment Mode
npm run dev