Skip to content
SegmentStream/ Docs

Loading documentation…

↑ ↓ to navigate  ↵ to open  Esc to close
Website Console
Section menu
SDKsApp SDKTypeScript

@segmentstream/app/agent

50 public exports from @segmentstream/app/agent.

Public exports

AGENT_TOKENS_PATH

Where the signed-in member's browser gets an agent token from the ontology.

const AGENT_TOKENS_PATH = "/api/ontology/agent/tokens"
AgentChat

The chat with the agent: its messages and status, and the commands that drive it. A refusal of the agent's routes, such as `rate_limited` when the agent's limits for people who signed up stop a turn, is a {@link SegmentStreamApiError }, in `error` and from the commands, to report by code.

type AgentChat = UseEveAgentReturn & {
    readonly answer: (interaction: string, option: string) => Promise<unknown>;
    readonly visitorSignUp: VisitorHandoff | undefined;
    readonly visitorChat: string | undefined;
};
AgentDraft

The agent's draft reply in the chat, waiting for a member to approve, edit or reject it.

interface AgentDraft {
    readonly id: string;
    readonly draft: string;
    readonly heldAt: string;
    readonly revision: "redrafting" | "failed" | null;
    readonly revises: string | null;
    readonly to: ReadonlyArray<string>;
    readonly cc: ReadonlyArray<string>;
    readonly files: ReadonlyArray<AgentDraftFile>;
    readonly sessionId: string | null;
    readonly turnId: string | null;
}
AgentDraftFile

A record file a draft carries, by its record, file property and revision, as the inbox shows it.

interface AgentDraftFile {
    readonly entity: string;
    readonly id: string;
    readonly property: string;
    readonly revisionId: string;
    readonly filename: string;
    readonly contentType: string;
    readonly size: number;
}
AgentDraftRecipients

Whom a reply to mail goes to, in place of its own `to` and `cc`; either may be left out.

interface AgentDraftRecipients {
    readonly to?: ReadonlyArray<string>;
    readonly cc?: ReadonlyArray<string>;
}
AgentInteraction

Something the agent asks the member, such as a confirmation, with the options to answer.

type AgentInteraction = Interaction;
AgentInteractionOption
type AgentInteractionOption = InteractionOption;
AgentInteractionPart

A question of the agent's turn that no tool call carries.

type AgentInteractionPart = EveInteractionPart;
AgentMessage

One message of a chat with the agent: what the member sent, or what the agent's turn produced.

type AgentMessage = EveMessage;
AgentMessageMetadata
type AgentMessageMetadata = EveMessageMetadata;
AgentMessagePart

A renderable part of a message: text, reasoning, a file, a tool call or a question.

type AgentMessagePart = EveMessagePart;
AgentToolPart

A tool call of the agent's turn; `interaction` is the confirmation it waits on, if any.

type AgentToolPart = EveToolPart;
HeldReply
type HeldReply = AgentDraft;
HeldReplyRecipients
type HeldReplyRecipients = AgentDraftRecipients;
InboxAuthor

Who wrote a message: the agent, a person (the chat's person, or staff), or another program.

type InboxAuthor = "agent" | "human" | "bot";
InboxChat

A person's private chat with the agent, as the inbox lists it.

interface InboxChat {
    readonly id: string;
    readonly title: string;
    readonly channel: string;
    readonly lastMessageAt: string | null;
    readonly waiting: number;
    readonly handledBy: string | null;
    readonly handoffAt: string | null;
    readonly handoffReason: string | null;
}
InboxChatOptions
interface InboxChatOptions {
    readonly refreshMs?: number;
}
InboxChatState
interface InboxChatState {
    readonly status: "loading" | "ready" | "error";
    readonly messages: ReadonlyArray<InboxMessage>;
    readonly notes: ReadonlyArray<InboxNote>;
    readonly drafts: ReadonlyArray<AgentDraft>;
    readonly held: ReadonlyArray<AgentDraft>;
    readonly hasOlder: boolean;
    readonly loadOlder: () => Promise<void>;
    readonly send: (text: string, files?: ReadonlyArray<InboxRecordFile>, recipients?: InboxRecipients) => Promise<InboxDelivery>;
    readonly recipients: () => Promise<{
        readonly to: ReadonlyArray<string>;
        readonly cc: ReadonlyArray<string>;
    }>;
    readonly approve: (reply: string, text?: string, recipients?: AgentDraftRecipients, files?: ReadonlyArray<Pick<AgentDraftFile, "entity" | "id" | "property" | "revisionId">>) => Promise<InboxDelivery>;
    readonly reject: (reply: string, reason?: string) => Promise<void>;
    readonly revise: (reply: string, note: string) => Promise<void>;
    readonly takeOver: () => Promise<void>;
    readonly handBack: () => Promise<void>;
    readonly error: SegmentStreamApiError | undefined;
    readonly refresh: () => Promise<void>;
}
InboxDelivery

What the provider did with a reply: `uncertain` may have been delivered, and is never resent.

type InboxDelivery = "sent" | "uncertain" | "failed";
InboxFile

A file a message carries, by its stored attachment.

interface InboxFile {
    readonly id: string;
    readonly name: string | null;
    readonly type: string | null;
    readonly size: number | null;
}
InboxMessage

A message of a chat, oldest first in `useInboxChat`'s `messages`.

interface InboxMessage {
    readonly id: string;
    readonly messageId: string | null;
    readonly at: string;
    readonly direction: "inbound" | "outbound";
    readonly by: InboxAuthor;
    readonly sender: string | null;
    readonly text: string;
    readonly files: ReadonlyArray<InboxFile>;
    readonly parts: ReadonlyArray<InboxPart>;
    readonly replyTo: string | null;
    readonly reactions: ReadonlyArray<InboxReaction>;
    readonly edited: boolean;
    readonly deleted: boolean;
    readonly delivery: InboxMessageDelivery | null;
    readonly disposition: "claimed" | "turn" | "skipped" | null;
    readonly reason: string | null;
}
InboxMessageDelivery

What became of a message the workspace's side sent, as the channel last reported it: `sent`, `delivered` and `read`, which implies delivered; `failed`; or `uncertain`, which may have been delivered. `null` for a person's message.

type InboxMessageDelivery = "sent" | "delivered" | "read" | "failed" | "uncertain";
InboxNote

What the agent kept in the chat for staff, never sent to its person: its `note` on a turn's reply, or a question it could not answer (`unanswered`), with the session and turn that wrote it.

interface InboxNote {
    readonly id: string;
    readonly at: string;
    readonly text: string;
    readonly sessionId: string | null;
    readonly turnId: string | null;
    readonly kind: "note" | "unanswered";
}
InboxOptions
interface InboxOptions {
    readonly where?: Where;
    readonly pageSize?: number;
    readonly refreshMs?: number;
}
InboxPart

What a message carries beside its text: a place, or a person's contact card.

type InboxPart = {
    readonly type: "location";
    readonly latitude: number;
    readonly longitude: number;
    readonly name: string | null;
    readonly address: string | null;
} | {
    readonly type: "contact";
    readonly name: string;
    readonly phones: ReadonlyArray<string>;
    readonly emails: ReadonlyArray<string>;
    readonly organization: string | null;
};
InboxReaction

One person's reaction to a message, by their account on the channel.

interface InboxReaction {
    readonly sender: string;
    readonly emoji: string;
}
InboxRecordFile

A stored file a member's reply carries: a record and its file property, content by default.

interface InboxRecordFile {
    readonly entity: string;
    readonly id: string;
    readonly property?: string;
}
InboxState
interface InboxState {
    readonly status: "loading" | "ready" | "error";
    readonly chats: ReadonlyArray<InboxChat>;
    readonly hasMore: boolean;
    readonly loadMore: () => Promise<void>;
    readonly error: SegmentStreamApiError | undefined;
    readonly refresh: () => Promise<void>;
}
SavedChatSession

A session to continue, as `useAgent`'s `initialSession` takes it: replayed from its start.

interface SavedChatSession {
    readonly sessionId: string;
    readonly streamIndex: 0;
    readonly inbox?: string;
}
SignedUpVisitor

The user a website's guest became.

interface SignedUpVisitor {
    readonly userId: string;
    readonly chat: string;
    readonly created: boolean;
}
UseAgentOptions

How the chat is set up. In the workspace app it talks to the app's agent as the signed-in member. On another website, `app` names the workspace app whose agent its visitors talk to.

type UseAgentOptions = Omit<UseEveAgentOptions, "agent" | "auth" | "baseUrl"> & {
    readonly app?: string;
    readonly botCheck?: VisitorOptions["botCheck"];
};
VISITORS_PATH

Where a website's browser gets its visitor an agent token for a workspace app's agent.

const VISITORS_PATH = "/api/ontology/agent/visitors"
VisitorEmailOptions

Where a link returns and how the guest is admitted, for {@link sendVisitorEmailCode}.

interface VisitorEmailOptions extends VisitorOptions {
    readonly returnTo?: string;
}
VisitorHandoff

What an emailed link the visitor opened came back with: the user this browser's guest became, or the refusal's code, such as `handoff_other_browser` when the link was opened in a browser other than the one that asked for it (the person is signed up, and continues where they asked), `member_address`, `link_invalid` or `handoff_invalid`.

type VisitorHandoff = ({
    readonly status: "signed_up";
} & SignedUpVisitor) | {
    readonly status: "refused";
    readonly code: string;
};
VisitorOptions

How a website's visitor gets their agent token, as `useAgent({ app, ... })` passes it on.

interface VisitorOptions {
    readonly botCheck?: () => string | undefined | Promise<string | undefined>;
}
agentChat

The signed-in member's own chat with the agent, `member:<id>`, as the ontology named it with their agent token, which this gets first when none is held. Throws as {@link agentToken} does.

function agentChat(): Promise<string>;
agentToken

The signed-in member's agent token, which the agent accepts as the bearer of its HTTP API: the ontology issues it for the app's session, and the browser keeps it until shortly before it expires. Throws `SegmentStreamApiError` when nobody is signed in or the ontology refuses.

function agentToken(): Promise<string>;
completeVisitorHandoff

Completes the sign-up an emailed link handed back to this page, once per page load: it reads `#segmentstream_guest` (or `#segmentstream_guest_error`) from the address, removes it, and gives the app's ontology the handoff with this browser's guest token. Only the guest who asked for the link takes it; the browser then keeps the user's tokens, so the chat goes on as the user's. `useAgent({ app })` and {@link visitorToken} call it themselves. `undefined` when the page came from no link.

function completeVisitorHandoff(app: string, options?: VisitorOptions): Promise<VisitorHandoff | undefined>;
forgetVisitorTokens

Forgets the visitor's tokens and chat for every app, those this browser kept from earlier visits too, and the agent sessions their chats kept, so that the next message starts again as a new visitor, in a new chat.

function forgetVisitorTokens(): void;
notesOf

What the agent kept for staff on the turn that wrote `draft`, oldest first: the notes, of `notes`, of its session and turn. None for a draft that names no turn.

function notesOf(draft: Pick<AgentDraft, "sessionId" | "turnId">, notes: ReadonlyArray<InboxNote>): ReadonlyArray<InboxNote>;
openInteractions

The questions and confirmations still open in a chat's messages, oldest first.

function openInteractions(messages: ReadonlyArray<AgentMessage>): AgentInteraction[];
saveChatSession

Keeps the chat's session, or forgets it with `undefined`.

function saveChatSession(chat: string, session: {
    readonly sessionId: string;
    readonly inbox?: string;
} | undefined): void;
savedChatSession

The session the chat last used, or `undefined`.

function savedChatSession(chat: string): SavedChatSession | undefined;
sendVisitorEmailCode

Has the app's own sign-in email `email`, for the website's guest to become the user it belongs to. The email holds either a six-digit code, which the guest gives {@link signUpVisitor}, or, for an address that never signed in to the app, a sign-in link: opened, it returns to `returnTo` and this browser completes the sign-up by itself (see {@link completeVisitorHandoff}). The answer is the same either way, so the page's words cover both. Throws `SegmentStreamApiError`, to report by its code: `email_sign_up_closed` when the app's ontology does not let people sign up by email, `rate_limited`, `email_unavailable`, or `invalid_request`.

function sendVisitorEmailCode(app: string, email: string, options?: VisitorEmailOptions): Promise<void>;
signUpVisitor

Makes the website's guest the user the code proves `email` belongs to: the user the app's ontology signed up with that address, or a new one with the roles its `signUp` gives `email`. The browser keeps the user's agent token and refresh token in place of the guest's, so the open chat goes on as the user's, and later visits too. Throws `SegmentStreamApiError`, to report by its code: `code_invalid`, `member_address` for an address of a member, who signs in in the app, `email_sign_up_closed`, `rate_limited`, `email_unavailable`, or `forbidden` once the guest is gone.

function signUpVisitor(app: string, input: {
    readonly email: string;
    readonly code: string;
}, options?: VisitorOptions): Promise<SignedUpVisitor>;
useAgent

The signed-in member's chat with the workspace's agent, as themselves: the agent acts with their roles, and confirmations are answered with the buttons the page renders for them. The hook gets and renews the member's agent token from the ontology; the page supplies every word. With `app`, on another website, the chat is a visitor's: the first message gets them a guest's agent token, and they stay that guest, in the same chat, while the browser keeps their refresh token. ```tsx const chat = useAgent(); chat.messages; // what the member and the agent said, part by part await chat.send("Which leads came in today?"); await chat.answer(interaction.interactionId, option.id); ```

function useAgent(options?: UseAgentOptions): AgentChat;
useInbox

The inbox's chats, the one with the most recent message first, a page at a time, each with the number of replies the agent holds for review in it; `loadMore` reads the next page. The pages read so far are read again every `refreshMs` while the page is visible, and after every reply, approval and rejection `useInboxChat` makes, each chat listed once, as last read. Another `where` or `pageSize` starts again from the first page.

function useInbox(options?: InboxOptions): InboxState;
useInboxChat

One chat of the inbox, by its `agent_chat` record: its messages, oldest first, with older ones on request, what the agent kept in it for staff, the agent's drafts waiting in it, and the member's replies, approvals and rejections. The chat is read again every `refreshMs` while the page is visible: in a chat the workspace's chat store keeps, only the messages and notes that changed since the last read, new ones and edits, deletions, reactions and delivery statuses of earlier ones; otherwise its newest messages.

function useInboxChat(chat: string | undefined, options?: InboxChatOptions): InboxChatState;
visitorChat

The key of the chat this browser's visitor has with the agent of the workspace app at `app`, the `key` of the app's `agent_chat` record: `web:guest:<id>` for a guest, and `web:member:<id>` once they became a user. The website tags its own records with it, such as its analytics events or its CRM's, so that the app's ontology relates them to the chat. `undefined` before the ontology admitted the visitor, and for a visitor this browser kept from before the app SDK remembered their chat, until the ontology admits them anew.

function visitorChat(app: string): string | undefined;
visitorToken

The agent token of a website's visitor for the workspace app at `app`, which its agent accepts as the bearer of its HTTP API. Where the app's ontology names this website's origin in `guest`, the visitor is a guest: a session, not a user, which the ontology creates no user for. The browser keeps the refresh token and renews the token with it, so the visitor stays the same guest, in the same chat, on their next visit; a month without a visit ends it. A website the ontology still names in `signUp.origins` signs its visitor up as a user instead. Throws `SegmentStreamApiError` when the ontology refuses, such as `origin_not_allowed`, `rate_limited`, `bot_check_required` or `bot_check_failed`.

function visitorToken(app: string, options?: VisitorOptions): Promise<string>;