veryfront/conversations

Import

ts
import {
  Conversation,
  CONVERSATIONS_OPERATIONS,
  conversationsProblemOf,
  createConversationsApiTransport,
} from "veryfront/conversations";

Examples

Create a conversation and add a message

ts
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

ts
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

ts
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

ts
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.

PropertyTypeDescriptionSource
signal?AbortSignalCancels the request.source
transport<code>VeryfrontApiTransport<unknown></code>Canonical Veryfront API transport, including origin, credentials and retries, for example from createConversationsApiTransport.source

CreateConversationInput

Input of Conversation.create.

PropertyTypeDescriptionSource
projectReference?stringProject ID or slug.source
type?ConversationTypeDefaults to chat.source
title?stringsource
metadata?ConversationMetadataJSON object under 64 KB.source
idempotencyKey?string1 to 255 characters. A retry with the same input returns the same conversation.source
initialMessage?CreateConversationInitialMessageInputFirst user message, stored together with the conversation.source

ConversationDetails

A conversation as the API returns it.

PropertyTypeDescriptionSource
idstringsource
projectIdstring | nullProject the conversation belongs to; null for a conversation without a project.source
respondingAgentIdstring | nullsource
typeConversationTypesource
titlestring | nullsource
summarystring | nullsource
statusConversationStatussource
currentNodestring | nullMessage selected as the active branch.source
messageCountnumbersource
lastMessageAtstring | nullRFC 3339 timestamp of the latest message.source
metadataConversationMetadata | nullsource
createdBystring | nullsource
archivedAtstring | nullsource
createdAtstringsource
updatedAtstringsource
participantsConversationParticipantSummary[]source
initialMessageId?string | nullMessage created from initialMessage; null when the conversation started empty. Only Conversation.create reports it.source

ConversationMessage

A message of a conversation.

PropertyTypeDescriptionSource
idstringsource
conversationIdstringsource
parentIdstring | nullsource
seqnumberPosition of the message in the conversation.source
roleConversationMessageRolesource
createdBystring | nullsource
senderTypeConversationMessageSenderTypesource
senderIdstring | nullsource
senderDisplayNamestring | nullsource
partsConversationMessagePart[]source
modelstring | nullsource
runUsage<code>Record<string, unknown> | null</code>Run usage as the REST API reports it, with REST field names.source
tokenUsage<code>Record<string, unknown> | null</code>Token usage as the REST API reports it, with REST field names.source
finishReasonstring | nullsource
costCreditsstring | nullsource
statusConversationMessageStatussource
editedAtstring | nullsource
idempotencyKeystring | nullsource
metadataConversationMetadata | nullsource
createdAtstringsource
updatedAtstring | nullsource

Exports

Components

NameDescriptionSource
CONVERSATIONS_OPERATIONSMethod and path of each conversations operation, keyed by the REST operation ID.source

Functions

NameDescriptionSource
conversationsProblemOfThe problem body of an error thrown by the conversations SDK, or undefined for any other error.source
createConversationsApiTransportCreate the canonical Veryfront API transport for the conversations SDK. It owns the origin, credentials, retries, response body limits and telemetry of every SDK request.source

Classes

NameDescriptionSource
ConversationA conversation: the durable thread of messages, branches, participants and read state between people and agents.source

Types

NameDescriptionSource
ConversationBranchSelectionThe message selected as the active branch of a conversation.source
ConversationCallOptionsOptions of one conversations SDK call.source
ConversationClientOptionsConnection settings of Conversation.list, get, create and attach.source
ConversationDetailsA conversation as the API returns it.source
ConversationMessageA message of a conversation.source
ConversationMessagePageOne page of messages.source
ConversationMessagePartOne part of a message, for example { type: "text", text: "Hello" }. The SDK sends and returns parts unchanged, with the field names the REST API uses.source
ConversationMessageRoleRole of a message.source
ConversationMessageSenderTypeKind of sender that wrote a message.source
ConversationMessageStatusDelivery status of a message.source
ConversationMetadataCaller-defined JSON object, sent and returned unchanged.source
ConversationPageOne page of conversations.source
ConversationPageInfoCursors of one page. Pass next as cursor to read the following page.source
ConversationPageOfOne page of a list.source
ConversationParticipantA participant of a conversation.source
ConversationParticipantPageOne page of participants.source
ConversationParticipantRoleRole of a participant in a conversation.source
ConversationParticipantSummaryParticipant as a conversation embeds it.source
ConversationParticipantTypeKind of participant.source
ConversationReadStateThe caller's read cursor and unread count in a conversation.source
ConversationsApiTransportOptionsOptions for createConversationsApiTransport.source
ConversationsOperationRouteHTTP route of one conversations operation.source
ConversationSortByField that orders a conversation list.source
ConversationSortOrderDirection of a conversation list.source
ConversationsProblemProblem body of a failed conversations API request.source
ConversationStatusLifecycle status of a conversation.source
ConversationTypeKind of conversation.source
CreateConversationInitialMessageInputMessage fields shared by conversation.createMessage and initialMessage.source
CreateConversationInputInput of Conversation.create.source
CreateConversationMessageBranchInputInput of conversation.createMessageBranch.source
CreateConversationMessageInputInput of conversation.createMessage. The SDK sends the message with the user role.source
CreateConversationParticipantInputInput of conversation.createParticipant.source
ListConversationMessageBranchesParamsParameters of conversation.listMessageBranches. The API returns every branch in one page and accepts no parameters.source
ListConversationMessagesParamsPaging of conversation.listMessages.source
ListConversationsParamsFilters and paging of Conversation.list.source
TransportRetryConfigRetries after a failed attempt, with exponential backoff between initialDelay and maxDelay ms.source
UpdateConversationInputInput of conversation.update.source
UpdateConversationMessageInputInput of conversation.updateMessage.source
UpdateConversationReadStateInputInput of conversation.updateReadState.source
VeryfrontApiTransportSends requests to the Veryfront API with the transport's origin, credentials and retries.source