Import
import {
Conversation,
CONVERSATIONS_OPERATIONS,
conversationsProblemOf,
createConversationsApiTransport,
} from "veryfront/conversations";Examples
Create a conversation and add a message
import { Conversation, createConversationsApiTransport } from "veryfront/conversations";
const transport = createConversationsApiTransport({
baseUrl: "<API_ORIGIN>",
getToken: () => process.env.VERYFRONT_API_TOKEN!,
});
const conversation = await Conversation.create(
{ projectReference: "<PROJECT_ID>", title: "Release notes" },
{ transport },
);
const message = await conversation.createMessage({
parts: [{ type: "text", text: "Draft the release notes." }],
});
console.log(conversation.id, message.id);List, read, update and delete conversations
import { Conversation, createConversationsApiTransport } from "veryfront/conversations";
const transport = createConversationsApiTransport({
baseUrl: "<API_ORIGIN>",
getToken: () => process.env.VERYFRONT_API_TOKEN!,
});
const page = await Conversation.list({ projectReference: "<PROJECT_ID>", limit: 20 }, {
transport,
});
console.log(page.data.map((item) => item.details?.title), page.pageInfo.next);
const conversation = await Conversation.get("<CONVERSATION_ID>", { transport });
const archived = await conversation.update({ status: "archived" });
console.log(archived.details?.status); // "archived"
await archived.delete();Read messages and work with branches
import { Conversation, createConversationsApiTransport } from "veryfront/conversations";
const transport = createConversationsApiTransport({
baseUrl: "<API_ORIGIN>",
getToken: () => process.env.VERYFRONT_API_TOKEN!,
});
// attach() uses a known conversation ID without reading the conversation first.
const conversation = Conversation.attach("<CONVERSATION_ID>", { transport });
let cursor: string | undefined;
do {
const page = await conversation.listMessages({ cursor, limit: 50 });
for (const message of page.data) console.log(message.seq, message.role);
cursor = page.pageInfo.next ?? undefined;
} while (cursor);
const message = await conversation.getMessage("<MESSAGE_ID>");
await conversation.updateMessage(message.id, { parts: [{ type: "text", text: "Edited text." }] });
const branch = await conversation.createMessageBranch(message.id, {
parts: [{ type: "text", text: "Another version." }],
});
const branches = await conversation.listMessageBranches("<PARENT_MESSAGE_ID>");
console.log(branches.data.length);
await conversation.updateBranchSelection(branch.id);
await conversation.deleteMessage(branch.id);Manage participants and read state
import {
Conversation,
conversationsProblemOf,
createConversationsApiTransport,
} from "veryfront/conversations";
const transport = createConversationsApiTransport({
baseUrl: "<API_ORIGIN>",
getToken: () => process.env.VERYFRONT_API_TOKEN!,
});
const conversation = Conversation.attach("<CONVERSATION_ID>", { transport });
await conversation.createParticipant({ userId: "<USER_ID>", role: "viewer" });
const participants = await conversation.listParticipants();
console.log(participants.data.map((participant) => participant.role));
await conversation.deleteParticipant("<USER_ID>");
const readState = await conversation.getReadState();
console.log(readState.unreadCount);
try {
await conversation.updateReadState({ messageId: "<MESSAGE_ID>" });
} catch (error) {
// A failed request carries the API problem body, for example status 404.
console.error(conversationsProblemOf(error)?.status);
}API
Conversation.list(params, options)
List the conversations the caller can read.
Returns: <code>Promise<ConversationPage></code>
Conversation.get(conversationId, options)
Read one conversation.
Returns: <code>Promise<Conversation></code>
Conversation.create(input, options)
Start a conversation.
Returns: <code>Promise<Conversation></code>
Conversation.attach(conversationId, options)
Use an existing conversation by ID without reading it first. The client's details stay undefined.
Returns: Conversation
conversation.update(input, options)
Change the title, summary, status or project. Resolves with the updated conversation.
Returns: <code>Promise<Conversation></code>
conversation.delete(options)
Delete the conversation.
Returns: <code>Promise<void></code>
conversation.listMessages(params, options)
Read one page of messages. Pass pageInfo.next as cursor to read the following page.
Returns: <code>Promise<ConversationMessagePage></code>
conversation.getMessage(messageId, options)
Read one message.
Returns: <code>Promise<ConversationMessage></code>
conversation.createMessage(input, options)
Add a user message.
Returns: <code>Promise<ConversationMessage></code>
conversation.updateMessage(messageId, input, options)
Replace the parts of a message.
Returns: <code>Promise<ConversationMessage></code>
conversation.deleteMessage(messageId, options)
Delete one message.
Returns: <code>Promise<void></code>
conversation.listMessageBranches(messageId, params, options)
List the child messages (branches) under a message. The API returns every branch in one page, so params has no fields.
Returns: <code>Promise<ConversationMessagePage></code>
conversation.createMessageBranch(messageId, input, options)
Create an alternative version of a message and select it.
Returns: <code>Promise<ConversationMessage></code>
conversation.updateBranchSelection(messageId, options)
Select the active branch by the message to show.
Returns: <code>Promise<ConversationBranchSelection></code>
conversation.listParticipants(options)
List the participants. The API returns every participant in one page.
Returns: <code>Promise<ConversationParticipantPage></code>
conversation.createParticipant(input, options)
Add a user to the conversation.
Returns: <code>Promise<ConversationParticipant></code>
conversation.deleteParticipant(userId, options)
Remove a user from the conversation.
Returns: <code>Promise<void></code>
conversation.getReadState(options)
Read the caller's read cursor and unread count.
Returns: <code>Promise<ConversationReadState></code>
conversation.updateReadState(input, options)
Advance the caller's read cursor to a message.
Returns: <code>Promise<ConversationReadState></code>
Type Reference
ConversationClientOptions
Connection settings of Conversation.list, get, create and attach.
CreateConversationInput
Input of Conversation.create.
ConversationDetails
A conversation as the API returns it.
ConversationMessage
A message of a conversation.