A lightweight JavaScript client for the Cognipeer API. Works in both Node.js (CommonJS) and TypeScript environments.
npm install cognipeer-client
import CognipeerClient from 'cognipeer-client';
// Initialize the client
const client = new CognipeerClient({
baseUrl: 'https://api.cognipeer.com/v1/client', // Optional, defaults to this value
token: 'your-api-token' // Optional, but recommended for authentication
});
// List available peers
const peers = await client.peer.list();
console.log(peers);
// Get a specific peer by ID
const peerId = '90aa68af-d5c4-4b60-b63e-b732db08bb46';
const peerDetails = await client.peer.get(peerId);
// Create a conversation with a peer
const conversation = await client.conversation.create(peerId);
// Send a message to a conversation
const conversationId = conversation._id;
const message = await client.conversation.sendMessage(
conversationId,
'Hello, how can you help me?'
);
console.log(message.content);
// Get JSON response (structured data)
const jsonResponse = await client.conversation.sendMessage(
conversationId,
'Give me data in JSON format',
{ responseFormat: 'json' }
);
console.log(jsonResponse.output); // Access the parsed JSON output
// Find all conversations
const conversations = await client.conversation.list();
// Get a specific conversation
const specificConversation = await client.conversation.get(conversationId);
// Get messages from a conversation
const messages = await client.conversation.getMessages(conversationId);
// Send a message to a peer using competition mode (creates a new conversation)
const competitionResponse = await client.peer.competition(
peerId,
{
content: 'Tell me about your features',
responseFormat: 'json'
}
);
// Chat with a peer using message history (creates a new conversation)
const chatResponse = await client.peer.chat(
peerId,
{
messages: [
{ role: 'user', content: 'Hello, who are you?' },
{ role: 'ai', content: 'I am an AI assistant.' },
{ role: 'user', content: 'What can you help me with?' }
]
}
);
// Execute an app with inputs
const appId = 'your-app-id';
const result = await client.app.execute(appId, {
inputs: {
textInput: 'Some text value',
numberInput: 42,
// File inputs will be handled automatically when sent to the API
fileInput: fileData
}
});
console.log(result); // Output values mapped by output name
const CognipeerClient = require('cognipeer-client');
// Initialize the client
const client = new CognipeerClient({
baseUrl: 'https://api.cognipeer.com/v1/client', // Optional
token: 'your-api-token' // Optional, but recommended
});
// Using async/await
async function example() {
// List available peers
const peers = await client.peer.list();
// Create a conversation with the first peer
if (peers.length > 0) {
const conversation = await client.conversation.create(peers[0]._id);
// Send a message
const response = await client.conversation.sendMessage(conversation._id, 'Hello!');
console.log(response.content);
// Find all conversations for this peer
const peerConversations = await client.conversation.list({ peerId: peers[0]._id });
console.log(`Found ${peerConversations.length} conversations`);
// Chat with the peer directly (creates a new conversation)
const chatResponse = await client.peer.chat(
peers[0]._id,
{
messages: [
{ role: 'user', content: 'What can you do?' }
]
}
);
console.log(chatResponse.content);
}
}
// Using promises
client.peer.list()
.then(peers => {
if (peers.length > 0) {
return client.conversation.create(peers[0]._id);
}
})
.then(conversation => {
return client.conversation.sendMessage(conversation._id, 'Hello!');
})
.then(message => {
console.log(message.content);
})
.catch(error => {
console.error(error);
});
const client = new CognipeerClient(options);
options
(optional): Configuration objectbaseUrl
(optional): The base URL for the Cognipeer API. Defaults to 'https://api.cognipeer.com/v1/client'token
(optional): Authentication token for the API
Retrieves a list of all available peers.
const peers = await client.peer.list({ limit: 20 });
options
(optional):limit
(optional): Maximum number of peers to return (default: 10)
Returns: Promise<Peer[]>
Get details of a specific peer by ID.
const peer = await client.peer.get(peerId);
id
(required): The ID of the peer
Returns: Promise<Peer>
Send a message to a peer in competition mode (creates a new conversation).
const response = await client.peer.competition(
peerId,
{
content: 'Tell me about your products',
responseFormat: 'json',
responseSchema: { type: 'object', properties: {} }, // Optional
additionalContext: { customData: 'value' } // Optional
}
);
peerId
(required): The ID of the peeroptions
(required):content
(required): The message contentresponseFormat
(optional): Format of the response. Can be 'text' (default) or 'json'responseSchema
(optional): JSON schema for structured responsesadditionalContext
(optional): Additional context to provide to the peer
Returns: Promise<Message>
or Promise<JsonResponseMessage>
depending on the response format
Chat with a peer by sending a series of messages (creates a new conversation).
const response = await client.peer.chat(
peerId,
{
messages: [
{ role: 'user', content: 'Hello' },
{ role: 'ai', content: 'Hi there! How can I help you?' },
{ role: 'user', content: 'What services do you offer?' }
],
responseFormat: 'text'
}
);
peerId
(required): The ID of the peeroptions
(required):messages
(required): Array of message objects, each with role ('user' or 'ai') and contentresponseFormat
(optional): Format of the response. Can be 'text' (default) or 'json'responseSchema
(optional): JSON schema for structured responsesadditionalContext
(optional): Additional context to provide to the peer
Returns: Promise<Message>
or Promise<JsonResponseMessage>
depending on the response format
Creates a new conversation with the specified peer.
const conversation = await client.conversation.create(peerId);
peerId
(required): The ID of the peer to create a conversation with
Returns: Promise<Conversation>
Get a list of conversations with optional filtering.
const conversations = await client.conversation.list({ peerId: 'some-id' });
filter
(optional): Filter criteria for conversationspeerId
(optional): Filter by peer ID
Returns: Promise<Conversation[]>
Get a specific conversation by ID.
const conversation = await client.conversation.get(conversationId);
id
(required): The ID of the conversation to find
Returns: Promise<Conversation>
Sends a message to an existing conversation.
const message = await client.conversation.sendMessage(
conversationId,
content,
{ responseFormat: 'json' }
);
id
(required): The ID of the conversation to send a message tocontent
(required): The content of the message to sendoptions
(optional): Additional options for the messageresponseFormat
(optional): Format of the response. Can be 'text' (default) or 'json'
Returns: Promise<Message>
or Promise<JsonResponseMessage>
depending on the response format
Get messages from a conversation.
const messages = await client.conversation.getMessages(conversationId, 20);
id
(required): The ID of the conversation to find messages inmessagesCount
(optional): Number of messages to return (default: 10)
Returns: Promise<Message[]>
Execute an app with the specified inputs.
const result = await client.app.execute(appId, {
inputs: {
name: 'John Doe',
query: 'How do I reset my password?',
preferences: { language: 'en' }
// File inputs are supported and will be handled by the API
}
});
id
(required): The ID of the app to executeoptions
(required):inputs
(required): Object containing input values keyed by input name
Returns: Promise<Record<string, any>>
- The execution results with outputs mapped by name
The client includes built-in error handling that will log API errors to the console. You can also wrap your calls in try/catch blocks for custom error handling:
try {
const peers = await client.peer.list();
} catch (error) {
// Custom error handling
console.error('Failed to list peers:', error);
}
For on-premises deployments, just specify your local API endpoint when initializing the client:
const client = new CognipeerClient({
baseUrl: 'https://api.cognipeer.com/v1/client',
token: 'your-local-token'
});
MIT