| 1 | /** |
| 2 | * Chat: channels, direct messages, threads and messages, kept by the chat |
| 3 | * service (`services/chat`). People and agents are members alike. |
| 4 | * |
| 5 | * Wire shapes are snake_case end to end, so the site, the public API and |
| 6 | * the live socket all carry the same objects. |
| 7 | */ |
| 8 | import type { ServiceBinding } from "./clients"; |
| 9 | import type { User } from "./identity"; |
| 10 | import type { Result } from "./result"; |
| 11 | import type { AskerAccess } from "./workspace-agents"; |
| 12 | |
| 13 | /** Who is speaking: a person (by user id) or a workspace agent (by agent id). */ |
| 14 | export type Principal = { kind: "user" | "agent"; id: string }; |
| 15 | |
| 16 | export function principalKey(principal: Principal): string { |
| 17 | return `${principal.kind}:${principal.id}`; |
| 18 | } |
| 19 | |
| 20 | export function parsePrincipalKey(key: string): Principal | null { |
| 21 | const at = key.indexOf(":"); |
| 22 | if (at < 0) return null; |
| 23 | const kind = key.slice(0, at); |
| 24 | const id = key.slice(at + 1); |
| 25 | if ((kind !== "user" && kind !== "agent") || !id) return null; |
| 26 | return { kind, id }; |
| 27 | } |
| 28 | |
| 29 | /** How a member shows: resolved by the chat service when it answers. */ |
| 30 | export type MemberProfile = Principal & { |
| 31 | /** `username` for a person (lowercased: what `@` mentions), `handle` for an agent. */ |
| 32 | name: string; |
| 33 | /** |
| 34 | * A person's username as they wrote it (`Ana`), when that differs from |
| 35 | * `name`; absent for an agent. Shown in their card and in autocomplete. |
| 36 | */ |
| 37 | display_username?: string | null; |
| 38 | /** |
| 39 | * What chat shows them as: a person's display name, else their username |
| 40 | * in its chosen case; an agent's display name. Read it with `memberName`. |
| 41 | */ |
| 42 | display_name: string; |
| 43 | /** Uploaded avatar hash for a person, or null for the letter avatar. */ |
| 44 | avatar: string | null; |
| 45 | /** An agent's one-line role ("Release manager for g1t"). */ |
| 46 | role: string | null; |
| 47 | /** |
| 48 | * An agent's title ("QA Engineer"); null for a person. Chat always sets |
| 49 | * it; optional so profiles a page makes for itself need not. |
| 50 | */ |
| 51 | title?: string | null; |
| 52 | /** What an agent's pixel creature is drawn from; null for a person. Always set by chat. */ |
| 53 | avatar_seed?: string | null; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * A member's handle as it shows (`@Ana`, without the `@`): a person's |
| 58 | * username in its chosen case, an agent's handle. |
| 59 | */ |
| 60 | export function memberHandle(member: { name: string; display_username?: string | null }): string { |
| 61 | const display = member.display_username; |
| 62 | return display && display.toLowerCase() === member.name.toLowerCase() ? display : member.name; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * How a member's name is shown everywhere in chat (messages, the |
| 67 | * sidebar, direct-message titles, typing, notifications and pushes): their |
| 68 | * display name, else their handle in its chosen case. One rule, so every |
| 69 | * surface agrees. |
| 70 | */ |
| 71 | export function memberName(member: { name: string; display_name?: string | null; display_username?: string | null }): string { |
| 72 | return member.display_name?.trim() || memberHandle(member); |
| 73 | } |
| 74 | |
| 75 | export type ChannelKind = "channel" | "dm"; |
| 76 | |
| 77 | export type Channel = { |
| 78 | id: string; |
| 79 | workspace_id: string; |
| 80 | kind: ChannelKind; |
| 81 | /** Lowercase, no `#`. Null for a direct message. */ |
| 82 | name: string | null; |
| 83 | topic: string | null; |
| 84 | private: boolean; |
| 85 | created_by: Principal; |
| 86 | created_at: string; |
| 87 | archived_at: string | null; |
| 88 | last_message_at: string | null; |
| 89 | }; |
| 90 | |
| 91 | export type ChannelMember = { |
| 92 | channel_id: string; |
| 93 | member: MemberProfile; |
| 94 | role: "owner" | "member"; |
| 95 | starred: boolean; |
| 96 | muted: boolean; |
| 97 | last_read_id: string | null; |
| 98 | joined_at: string; |
| 99 | }; |
| 100 | |
| 101 | /** A card g1t or an agent posts: an event, a task, an approval. */ |
| 102 | export type MessageCard = { |
| 103 | /** What it is about, e.g. `pull`, `issue`, `task`, `deploy`, `approval`, `session`, `draft_issue`. */ |
| 104 | kind: string; |
| 105 | title: string; |
| 106 | /** A short line under the title: "3/3 checks · +214 −87". */ |
| 107 | detail: string | null; |
| 108 | /** A status shown on the right: "Needs approval", "Merged". */ |
| 109 | state: string | null; |
| 110 | /** Where clicking the card goes, relative to the site. */ |
| 111 | href: string | null; |
| 112 | /** A preview in Markdown under the title: a draft issue's body, a report's first lines. */ |
| 113 | body?: string | null; |
| 114 | /** Labelled facts shown in two columns: "Repository · acme/web", "Cap · $2.00". */ |
| 115 | fields?: CardField[]; |
| 116 | /** |
| 117 | * What people can do right here. Pressing one goes to the service that |
| 118 | * owns the card (`owner`), which checks the person may, does it, and |
| 119 | * updates the card in place. A card without `owner` has no actions. |
| 120 | */ |
| 121 | actions?: CardAction[]; |
| 122 | /** The service whose card this is and that answers its actions: `agents`. */ |
| 123 | owner?: "agents" | null; |
| 124 | /** What the card is about, for its owner: a session id, a draft id. */ |
| 125 | ref?: string | null; |
| 126 | }; |
| 127 | |
| 128 | export type CardField = { label: string; value: string }; |
| 129 | |
| 130 | /** A button on a card. */ |
| 131 | export type CardAction = { |
| 132 | /** Unique on the card: `stop`, `approve`, `file`. */ |
| 133 | id: string; |
| 134 | label: string; |
| 135 | style?: "primary" | "danger" | "default"; |
| 136 | /** Asked before it runs: "Stop this session and everything under it?". */ |
| 137 | confirm?: string | null; |
| 138 | /** A value it needs first, asked inline: an amount in dollars, or a line of text. */ |
| 139 | input?: { kind: "money" | "text"; label: string; placeholder?: string | null; initial?: string | null } | null; |
| 140 | /** A link instead of an action: opens this place in the site. */ |
| 141 | href?: string | null; |
| 142 | }; |
| 143 | |
| 144 | /** What pressing a card's action did, as the person who pressed it is told. */ |
| 145 | export type CardActionResult = { ok: boolean; message: string | null }; |
| 146 | |
| 147 | export type ChatMessage = { |
| 148 | /** Time-sortable (ULID-like), so ordering by id is ordering by time. */ |
| 149 | id: string; |
| 150 | channel_id: string; |
| 151 | author: MemberProfile; |
| 152 | kind: "text" | "card"; |
| 153 | body: string; |
| 154 | card: MessageCard | null; |
| 155 | /** The message this replies under, or null for a top-level message. */ |
| 156 | thread_root: string | null; |
| 157 | reply_count: number; |
| 158 | last_reply_at: string | null; |
| 159 | created_at: string; |
| 160 | edited_at: string | null; |
| 161 | deleted_at: string | null; |
| 162 | /** |
| 163 | * Its reactions, one per emoji in the order each was first used. Chat |
| 164 | * always sets it; optional so messages a page makes for itself need not. |
| 165 | */ |
| 166 | reactions?: ChatReaction[]; |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * One emoji's reactions on a message. `emoji` is a Unicode emoji or |
| 171 | * `:name:` for one of the workspace's own (`CustomEmoji`). |
| 172 | */ |
| 173 | export type ChatReaction = { |
| 174 | emoji: string; |
| 175 | /** Everyone who reacted with it, people and agents alike. */ |
| 176 | count: number; |
| 177 | /** |
| 178 | * Whether the viewer did. In live `message.*` events, which everyone in |
| 179 | * the room gets, it is always false: keep your own from what you know. |
| 180 | */ |
| 181 | me: boolean; |
| 182 | /** The first ten who reacted with it, for the hover list. */ |
| 183 | by: MemberProfile[]; |
| 184 | }; |
| 185 | |
| 186 | /** Who may add a workspace's emoji: every member (the default), or only its owners. */ |
| 187 | export type EmojiUpload = "members" | "admins"; |
| 188 | |
| 189 | /** |
| 190 | * One of a workspace's own emoji, used as `:name:`. Its image is served |
| 191 | * from the usercontent origin at `/emoji/<file>` (never from the site). |
| 192 | */ |
| 193 | export type CustomEmoji = { |
| 194 | name: string; |
| 195 | /** For an alias, the emoji it is another name for; it shows that one's image. */ |
| 196 | alias_of: string | null; |
| 197 | /** The SHA-256 of the image's bytes. */ |
| 198 | file: string; |
| 199 | content_type: "image/png" | "image/gif" | "image/webp"; |
| 200 | bytes: number; |
| 201 | created_by: MemberProfile; |
| 202 | created_at: string; |
| 203 | }; |
| 204 | |
| 205 | export type EmojiList = { |
| 206 | emoji: CustomEmoji[]; |
| 207 | emoji_upload: EmojiUpload; |
| 208 | /** Whether the viewer may add emoji and aliases. */ |
| 209 | can_upload: boolean; |
| 210 | /** Whether the viewer is an owner: may remove any emoji and change `emoji_upload`. */ |
| 211 | can_manage: boolean; |
| 212 | }; |
| 213 | |
| 214 | /** Who may do something in a workspace's chat: every member, or only its owners. */ |
| 215 | export type ChatAllowed = "members" | "owners"; |
| 216 | |
| 217 | /** |
| 218 | * Who may rename and archive a channel: its owners (whoever made it) and |
| 219 | * the workspace's owners (the default), or the workspace's owners only. |
| 220 | */ |
| 221 | export type ChannelManagers = "channel_owners" | "owners"; |
| 222 | |
| 223 | /** |
| 224 | * A workspace's chat settings, which its owners choose (Workspace, |
| 225 | * Settings, Chat). The chat service enforces each one; the page only |
| 226 | * hides what the viewer may not do. |
| 227 | */ |
| 228 | export type ChatSettings = { |
| 229 | /** Who may create public channels. */ |
| 230 | public_channels: ChatAllowed; |
| 231 | /** Who may create private channels. */ |
| 232 | private_channels: ChatAllowed; |
| 233 | /** Who may rename, archive and unarchive channels. */ |
| 234 | manage_channels: ChannelManagers; |
| 235 | /** Who may add custom emoji (`admins` is the workspace's owners). */ |
| 236 | emoji_upload: EmojiUpload; |
| 237 | /** |
| 238 | * The public channels someone new is put in the first time they open |
| 239 | * Chat, by id. #general unless the owners chose otherwise. |
| 240 | */ |
| 241 | default_channels: string[]; |
| 242 | }; |
| 243 | |
| 244 | /** What the viewer may do in a workspace's chat, from its settings and their role. */ |
| 245 | export type ChatPermissions = { |
| 246 | create_public_channels: boolean; |
| 247 | create_private_channels: boolean; |
| 248 | add_emoji: boolean; |
| 249 | /** Whether they may change the workspace's chat settings: owners only. */ |
| 250 | manage_settings: boolean; |
| 251 | }; |
| 252 | |
| 253 | /** The chat settings page: the settings, what the viewer may do, and the public channels to choose defaults from. */ |
| 254 | export type ChatSettingsView = { |
| 255 | settings: ChatSettings; |
| 256 | can: ChatPermissions; |
| 257 | channels: Channel[]; |
| 258 | }; |
| 259 | |
| 260 | /** A change to a channel: its name, its topic, or whether it is archived. Anything left out stays. */ |
| 261 | export type ChannelChange = { name?: string; topic?: string | null; archived?: boolean }; |
| 262 | |
| 263 | /** An image for a new emoji, as base64. Its type is read from its bytes, never taken from here. */ |
| 264 | export type EmojiFile = { data: string }; |
| 265 | |
| 266 | /** The largest custom emoji image, in bytes, and the widest or tallest, in pixels. */ |
| 267 | export const MAX_EMOJI_BYTES = 256 * 1024; |
| 268 | export const MAX_EMOJI_SIDE = 512; |
| 269 | |
| 270 | /** One row of the Chat sidebar. */ |
| 271 | export type ChatSidebarEntry = { |
| 272 | channel: Channel; |
| 273 | /** "g1t-core", or the other members' names for a direct message. */ |
| 274 | title: string; |
| 275 | /** For a direct message: who else is in it (up to four). */ |
| 276 | others: MemberProfile[]; |
| 277 | starred: boolean; |
| 278 | muted: boolean; |
| 279 | unread: number; |
| 280 | mentions: number; |
| 281 | }; |
| 282 | |
| 283 | export type ChatSidebar = { |
| 284 | entries: ChatSidebarEntry[]; |
| 285 | /** Public channels in the workspace the viewer has not joined. */ |
| 286 | browsable: number; |
| 287 | /** What the viewer may do, from the workspace's chat settings: the page hides what they may not. */ |
| 288 | can: ChatPermissions; |
| 289 | }; |
| 290 | |
| 291 | export type MessagePage = { |
| 292 | messages: ChatMessage[]; |
| 293 | /** Pass as `before` to read further back; null at the beginning. */ |
| 294 | older: string | null; |
| 295 | /** |
| 296 | * For a thread's first page: the message the thread is under, however |
| 297 | * many replies it has, so a thread panel always shows it (a session's |
| 298 | * live card, say) at its top. |
| 299 | */ |
| 300 | root?: ChatMessage | null; |
| 301 | /** |
| 302 | * Set when the page was read with `after`: pass it as `after` again for |
| 303 | * the next messages, or null when this page reached the newest. |
| 304 | */ |
| 305 | newer?: string | null; |
| 306 | }; |
| 307 | |
| 308 | export type NewChannel = { name: string; topic?: string | null; private?: boolean }; |
| 309 | |
| 310 | export type PostMessage = { body: string; thread_root?: string | null }; |
| 311 | |
| 312 | /** |
| 313 | * Who will read what is said in a conversation: a direct message's or |
| 314 | * private channel's people, or, for a public channel, the whole workspace. |
| 315 | * An agent answers there only with what every one of them may see. |
| 316 | */ |
| 317 | export type ChatAudience = { |
| 318 | kind: "dm" | "private" | "public"; |
| 319 | /** The people in it (agents left out). For a public channel, its current members, for reference only. */ |
| 320 | member_user_ids: string[]; |
| 321 | member_count: number; |
| 322 | }; |
| 323 | |
| 324 | /** A message an agent found by searching, with the conversation it is in. */ |
| 325 | export type AgentFoundMessage = { |
| 326 | channel_id: string; |
| 327 | /** The channel's name, or null for a direct message. */ |
| 328 | channel: string | null; |
| 329 | message: ChatMessage; |
| 330 | }; |
| 331 | |
| 332 | /** The most agent-to-agent hops one person's request may start. */ |
| 333 | export const CHAT_MAX_HOPS = 6; |
| 334 | |
| 335 | /** |
| 336 | * What an agent posts. An agent's message never wakes another agent, even |
| 337 | * when it @mentions one: only a hand-off does (`handOffAsAgent`). Its |
| 338 | * mentions of anyone who is not a member of the conversation lose their |
| 339 | * `@`, so they show as plain names and notify nobody. When it answers a |
| 340 | * delivery, it passes that delivery's `hops`, `asked_by`, `asker` and |
| 341 | * `chain` back, which a card that waits on the asker uses. Left out: asked |
| 342 | * by the person who created the agent. |
| 343 | */ |
| 344 | export type AgentPostMessage = PostMessage & { |
| 345 | card?: MessageCard | null; |
| 346 | hops?: number; |
| 347 | asked_by?: string | null; |
| 348 | /** The delivery's `asker`, handed on to agents this message wakes. Absent: none (they treat the asker as unable to change code). */ |
| 349 | asker?: AskerAccess | null; |
| 350 | /** |
| 351 | * The delivery's `chain`: the agents that handled this request before |
| 352 | * the one posting, oldest first. Chat adds the poster, and never hands |
| 353 | * the message to the agent that sent the work to it (no ping-pong). |
| 354 | */ |
| 355 | chain?: string[]; |
| 356 | }; |
| 357 | |
| 358 | /** |
| 359 | * A conversation as an agent is told about it before every turn: what it |
| 360 | * is and who is in it, so it knows who reads what it says and who doesn't. |
| 361 | * Every agent member is listed; people up to `CONVERSATION_PEOPLE_SHOWN` |
| 362 | * (the person who asked always among them), with the totals beside. |
| 363 | */ |
| 364 | export type ConversationForAgent = { |
| 365 | channel: Channel; |
| 366 | /** Agents first, then people. */ |
| 367 | members: MemberProfile[]; |
| 368 | /** How many people and agents are in it, listed or not. */ |
| 369 | people: number; |
| 370 | agents: number; |
| 371 | }; |
| 372 | |
| 373 | /** The most people `conversationForAgent` lists; the rest are a count. */ |
| 374 | export const CONVERSATION_PEOPLE_SHOWN = 20; |
| 375 | |
| 376 | /** |
| 377 | * An agent handing work to a colleague agent for the person who asked |
| 378 | * (docs.g1t.sh/guides/agents/, "Hand off"). The fields after `brief` |
| 379 | * are the delivery the agent is answering, passed back as for a post. |
| 380 | */ |
| 381 | export type AgentHandOff = { |
| 382 | /** The colleague, by agent id. */ |
| 383 | colleague_id: string; |
| 384 | /** What they are asked to do, addressed to them. */ |
| 385 | brief: string; |
| 386 | thread_root?: string | null; |
| 387 | hops?: number; |
| 388 | asked_by: string; |
| 389 | asker?: AskerAccess | null; |
| 390 | chain?: string[]; |
| 391 | }; |
| 392 | |
| 393 | /** |
| 394 | * Where a hand-off went. `here`: the colleague is in this channel or group |
| 395 | * direct message, and the brief was posted here. `group_dm`: the brief was |
| 396 | * posted in the direct message of the person who asked, the agent and the |
| 397 | * colleague (`opened` when it was new), and a card linking to it was |
| 398 | * posted here. |
| 399 | */ |
| 400 | export type HandOffResult = { where: "here" | "group_dm"; channel_id: string; message_id: string; opened: boolean }; |
| 401 | |
| 402 | /** |
| 403 | * What the live socket sends. The site opens |
| 404 | * `wss://<site>/<workspace>/chat/live?channel=<id>`; the site checks the |
| 405 | * session and forwards the upgrade to the chat service with the viewer. |
| 406 | */ |
| 407 | export type ChatLiveEvent = |
| 408 | | { type: "message.created"; message: ChatMessage } |
| 409 | | { type: "message.updated"; message: ChatMessage } |
| 410 | | { type: "message.deleted"; channel_id: string; id: string } |
| 411 | | { type: "typing"; channel_id: string; member: MemberProfile; until: string } |
| 412 | | { type: "read"; channel_id: string; principal: Principal; last_read_id: string } |
| 413 | | { type: "channel.updated"; channel: Channel } |
| 414 | | { type: "reaction.added" | "reaction.removed"; channel_id: string; message_id: string; emoji: string; member: MemberProfile }; |
| 415 | |
| 416 | /** |
| 417 | * Header the site sets on a forwarded live socket: the viewer, as JSON. |
| 418 | * The site forwards the upgrade to the chat service's |
| 419 | * `GET /live?workspace=<slug>&channel=<id>` (`workspace` may be left out, |
| 420 | * at the cost of looking up each of the viewer's workspaces). |
| 421 | */ |
| 422 | export const CHAT_VIEWER_HEADER = "x-g1t-chat-viewer"; |
| 423 | |
| 424 | /** A channel with its members, and whether the viewer may rename or archive it. */ |
| 425 | export type ChannelDetail = { channel: Channel; members: ChannelMember[]; can_manage: boolean }; |
| 426 | |
| 427 | export type ChatApi = { |
| 428 | sidebar(workspace: string, viewer: User): Promise<Result<ChatSidebar>>; |
| 429 | channel( |
| 430 | workspace: string, |
| 431 | channelId: string, |
| 432 | viewer: User, |
| 433 | ): Promise<Result<ChannelDetail>>; |
| 434 | /** The same, found by its name in the workspace (`#general` or `general`), as the site's URLs name channels. */ |
| 435 | channelByName( |
| 436 | workspace: string, |
| 437 | name: string, |
| 438 | viewer: User, |
| 439 | ): Promise<Result<ChannelDetail>>; |
| 440 | /** |
| 441 | * Browse channels: every public channel, and the private ones the viewer |
| 442 | * is in. With `archived`, the archived ones instead. |
| 443 | */ |
| 444 | browse(workspace: string, viewer: User, options?: { archived?: boolean }): Promise<Result<Channel[]>>; |
| 445 | /** Makes a channel. Who may make a public or a private one is the workspace's setting. */ |
| 446 | createChannel(workspace: string, viewer: User, input: NewChannel): Promise<Result<Channel>>; |
| 447 | /** |
| 448 | * Renames, archives or unarchives a channel (the workspace's |
| 449 | * `manage_channels` setting says who may), or changes its topic (any |
| 450 | * member). #general is never renamed or archived. Everyone looking at it |
| 451 | * gets `channel.updated`. |
| 452 | */ |
| 453 | updateChannel(workspace: string, channelId: string, viewer: User, change: ChannelChange): Promise<Result<Channel>>; |
| 454 | /** The workspace's chat settings, for any member; only owners may change them. */ |
| 455 | chatSettings(workspace: string, viewer: User): Promise<Result<ChatSettingsView>>; |
| 456 | /** Owners only: changes some of the workspace's chat settings, and answers all of them. */ |
| 457 | setChatSettings(workspace: string, viewer: User, change: Partial<ChatSettings>): Promise<Result<ChatSettings>>; |
| 458 | /** |
| 459 | * The direct message between the viewer and these members, created on |
| 460 | * first use. The same set of members always gets the same channel. |
| 461 | */ |
| 462 | openDm(workspace: string, viewer: User, members: Principal[]): Promise<Result<Channel>>; |
| 463 | join(workspace: string, channelId: string, viewer: User): Promise<Result<null>>; |
| 464 | leave(workspace: string, channelId: string, viewer: User): Promise<Result<null>>; |
| 465 | /** Adds a person or an agent. An invite grants read, never write. */ |
| 466 | invite(workspace: string, channelId: string, viewer: User, member: Principal): Promise<Result<null>>; |
| 467 | /** |
| 468 | * Newest first. Without `thread_root`, the channel's top-level messages; |
| 469 | * with it, that thread's replies, plus the message they reply to as the |
| 470 | * oldest once the page reaches the start of the thread (`older` null). |
| 471 | * With `after` (catching up after a reconnect): the messages after that |
| 472 | * id instead, oldest first, deleted ones included so the client can |
| 473 | * drop them; `newer` says whether there are more. |
| 474 | * Limit 50 by default, 200 at most. |
| 475 | */ |
| 476 | messages( |
| 477 | workspace: string, |
| 478 | channelId: string, |
| 479 | viewer: User, |
| 480 | page?: { before?: string | null; after?: string | null; limit?: number; thread_root?: string | null }, |
| 481 | ): Promise<Result<MessagePage>>; |
| 482 | post(workspace: string, channelId: string, viewer: User, message: PostMessage): Promise<Result<ChatMessage>>; |
| 483 | edit(workspace: string, channelId: string, viewer: User, id: string, body: string): Promise<Result<ChatMessage>>; |
| 484 | remove(workspace: string, channelId: string, viewer: User, id: string): Promise<Result<null>>; |
| 485 | markRead(workspace: string, channelId: string, viewer: User, id: string): Promise<Result<null>>; |
| 486 | setPreferences( |
| 487 | workspace: string, |
| 488 | channelId: string, |
| 489 | viewer: User, |
| 490 | prefs: { starred?: boolean; muted?: boolean }, |
| 491 | ): Promise<Result<null>>; |
| 492 | /** |
| 493 | * Posts as an agent. Only the agents service calls this, for replies and |
| 494 | * cards; the agent must be a member of the channel. |
| 495 | */ |
| 496 | postAsAgent( |
| 497 | workspace: string, |
| 498 | channelId: string, |
| 499 | agentId: string, |
| 500 | message: AgentPostMessage, |
| 501 | ): Promise<Result<ChatMessage>>; |
| 502 | /** |
| 503 | * Changes a message an agent posted: its body, its card, or both. Only |
| 504 | * the agents service calls this, to keep a session's live card current. |
| 505 | * Changing a message wakes nobody. |
| 506 | */ |
| 507 | updateAsAgent( |
| 508 | workspace: string, |
| 509 | channelId: string, |
| 510 | agentId: string, |
| 511 | id: string, |
| 512 | change: { body?: string; card?: MessageCard | null }, |
| 513 | ): Promise<Result<ChatMessage>>; |
| 514 | /** |
| 515 | * A person presses an action on a card in a conversation they can read. |
| 516 | * Chat hands it to the card's owner with the person, and the owner |
| 517 | * decides, acts and updates the card. |
| 518 | */ |
| 519 | cardAction( |
| 520 | workspace: string, |
| 521 | channelId: string, |
| 522 | viewer: User, |
| 523 | messageId: string, |
| 524 | actionId: string, |
| 525 | input?: string | null, |
| 526 | ): Promise<Result<CardActionResult>>; |
| 527 | /** Shows "is typing" for an agent while it works on a reply. */ |
| 528 | agentTyping(workspace: string, channelId: string, agentId: string): Promise<Result<null>>; |
| 529 | /** |
| 530 | * What an agent reads before it replies, oldest first: with |
| 531 | * `thread_root`, that thread (its root, then its replies); without, the |
| 532 | * channel's or direct message's latest top-level messages. Only the |
| 533 | * agents service calls this; the agent must be a member of the channel, |
| 534 | * so it reads only what was said where it was invited. `limit` defaults |
| 535 | * to 30, at most 100. |
| 536 | */ |
| 537 | historyForAgent( |
| 538 | workspace: string, |
| 539 | channelId: string, |
| 540 | agentId: string, |
| 541 | page?: { thread_root?: string | null; limit?: number }, |
| 542 | ): Promise<Result<ChatMessage[]>>; |
| 543 | /** Internal: who reads a conversation. */ |
| 544 | audience(workspace: string, channelId: string): Promise<Result<ChatAudience>>; |
| 545 | /** |
| 546 | * Internal, for an agent about to answer in `channelId`, which it must be |
| 547 | * a member of: the conversation and its members (`ConversationForAgent`). |
| 548 | * `askedBy` (a user id) is always among the people listed. |
| 549 | */ |
| 550 | conversationForAgent(workspace: string, channelId: string, agentId: string, askedBy?: string | null): Promise<Result<ConversationForAgent>>; |
| 551 | /** |
| 552 | * Internal: `agentId`, answering in `channelId`, hands work to a |
| 553 | * colleague agent for the person who asked. If the colleague is in this |
| 554 | * channel or group direct message, the brief is posted here; otherwise in |
| 555 | * the group direct message of the person, the agent and the colleague |
| 556 | * (opened on first use), with a card here linking to it. Either way the |
| 557 | * brief wakes the colleague, one hop further along the same chain, and |
| 558 | * nobody else. Refused for an agent that is not of the workspace, the |
| 559 | * agent itself, @g1t, one already in the chain, past the hop limit, or |
| 560 | * when the person who asked is not in this conversation. |
| 561 | */ |
| 562 | handOffAsAgent(workspace: string, channelId: string, agentId: string, handOff: AgentHandOff): Promise<Result<HandOffResult>>; |
| 563 | /** |
| 564 | * Internal, for an agent replying in `channelId`: messages matching |
| 565 | * `query` (newest first, at most 20) from conversations every person in |
| 566 | * that conversation's audience is in, and from public channels. Never |
| 567 | * another direct message unless it has exactly the same people. The |
| 568 | * audience is worked out here from `channelId`, never taken from the |
| 569 | * caller. |
| 570 | */ |
| 571 | searchForAgent(workspace: string, channelId: string, query: string, limit?: number): Promise<Result<AgentFoundMessage[]>>; |
| 572 | /** |
| 573 | * Internal, for an agent replying in `channelId`: a thread (`id`, its |
| 574 | * root or any reply) in `targetChannelId`, oldest first, under the same |
| 575 | * rule. A conversation the audience may not read is not found, exactly |
| 576 | * as one that does not exist. |
| 577 | */ |
| 578 | threadForAgent(workspace: string, channelId: string, targetChannelId: string, id: string): Promise<Result<AgentFoundMessage[]>>; |
| 579 | /** |
| 580 | * Reacts to a message with `emoji` (one Unicode emoji, or `:name:` for |
| 581 | * one of the workspace's own). Once per member per emoji; at most 50 |
| 582 | * different emoji on a message. Answers the message's reactions now. |
| 583 | */ |
| 584 | react(workspace: string, channelId: string, viewer: User, messageId: string, emoji: string): Promise<Result<ChatReaction[]>>; |
| 585 | unreact(workspace: string, channelId: string, viewer: User, messageId: string, emoji: string): Promise<Result<ChatReaction[]>>; |
| 586 | /** Internal, for the agents service: an agent reacts (or, with `remove`, takes it back). It must be in the channel. */ |
| 587 | reactAsAgent( |
| 588 | workspace: string, |
| 589 | channelId: string, |
| 590 | agentId: string, |
| 591 | messageId: string, |
| 592 | emoji: string, |
| 593 | remove?: boolean, |
| 594 | ): Promise<Result<ChatReaction[]>>; |
| 595 | /** The workspace's own emoji, by name, and who may add them. */ |
| 596 | listEmoji(workspace: string, viewer: User): Promise<Result<EmojiList>>; |
| 597 | /** Adds an emoji: a PNG, GIF or WebP of at most 256 KB and 512×512. */ |
| 598 | addEmoji(workspace: string, viewer: User, name: string, file: EmojiFile): Promise<Result<CustomEmoji>>; |
| 599 | /** Gives an existing emoji (`target`) another name. */ |
| 600 | aliasEmoji(workspace: string, viewer: User, name: string, target: string): Promise<Result<CustomEmoji>>; |
| 601 | /** Removes an emoji, and its aliases with it. Its creator or an owner may. */ |
| 602 | removeEmoji(workspace: string, viewer: User, name: string): Promise<Result<null>>; |
| 603 | /** Owners only: who may add emoji. */ |
| 604 | setEmojiUpload(workspace: string, viewer: User, value: EmojiUpload): Promise<Result<EmojiUpload>>; |
| 605 | }; |
| 606 | |
| 607 | async function rpc<T>(service: ServiceBinding, method: string, args: object): Promise<T> { |
| 608 | const response = await service.fetch(`https://service/rpc/${method}`, { |
| 609 | method: "POST", |
| 610 | headers: { "content-type": "application/json" }, |
| 611 | body: JSON.stringify(args), |
| 612 | }); |
| 613 | if (!response.ok) { |
| 614 | throw new Error(`${method} failed with status ${response.status}`); |
| 615 | } |
| 616 | return (await response.json()) as T; |
| 617 | } |
| 618 | |
| 619 | export function chatClient(service: ServiceBinding): ChatApi { |
| 620 | const call = <T>(method: string, args: object) => rpc<T>(service, method, args); |
| 621 | return { |
| 622 | sidebar: (workspace, viewer) => call("sidebar", { workspace, viewer }), |
| 623 | channel: (workspace, channelId, viewer) => call("channel", { workspace, channel_id: channelId, viewer }), |
| 624 | channelByName: (workspace, name, viewer) => call("channel_by_name", { workspace, name, viewer }), |
| 625 | browse: (workspace, viewer, options) => call("browse", { workspace, viewer, archived: options?.archived ?? false }), |
| 626 | createChannel: (workspace, viewer, input) => call("create_channel", { workspace, viewer, input }), |
| 627 | updateChannel: (workspace, channelId, viewer, change) => call("update_channel", { workspace, channel_id: channelId, viewer, change }), |
| 628 | chatSettings: (workspace, viewer) => call("chat_settings", { workspace, viewer }), |
| 629 | setChatSettings: (workspace, viewer, change) => call("set_chat_settings", { workspace, viewer, change }), |
| 630 | openDm: (workspace, viewer, members) => call("open_dm", { workspace, viewer, members }), |
| 631 | join: (workspace, channelId, viewer) => call("join", { workspace, channel_id: channelId, viewer }), |
| 632 | leave: (workspace, channelId, viewer) => call("leave", { workspace, channel_id: channelId, viewer }), |
| 633 | invite: (workspace, channelId, viewer, member) => |
| 634 | call("invite", { workspace, channel_id: channelId, viewer, member }), |
| 635 | messages: (workspace, channelId, viewer, page) => |
| 636 | call("messages", { |
| 637 | workspace, |
| 638 | channel_id: channelId, |
| 639 | viewer, |
| 640 | before: page?.before ?? null, |
| 641 | after: page?.after ?? null, |
| 642 | limit: page?.limit ?? null, |
| 643 | thread_root: page?.thread_root ?? null, |
| 644 | }), |
| 645 | post: (workspace, channelId, viewer, message) => call("post", { workspace, channel_id: channelId, viewer, message }), |
| 646 | edit: (workspace, channelId, viewer, id, body) => call("edit", { workspace, channel_id: channelId, viewer, id, body }), |
| 647 | remove: (workspace, channelId, viewer, id) => call("remove", { workspace, channel_id: channelId, viewer, id }), |
| 648 | markRead: (workspace, channelId, viewer, id) => call("mark_read", { workspace, channel_id: channelId, viewer, id }), |
| 649 | setPreferences: (workspace, channelId, viewer, prefs) => |
| 650 | call("set_preferences", { workspace, channel_id: channelId, viewer, prefs }), |
| 651 | postAsAgent: (workspace, channelId, agentId, message) => |
| 652 | call("post_as_agent", { workspace, channel_id: channelId, agent_id: agentId, message }), |
| 653 | cardAction: (workspace, channelId, viewer, messageId, actionId, input) => |
| 654 | call("card_action", { workspace, channel_id: channelId, viewer, message_id: messageId, action_id: actionId, input: input ?? null }), |
| 655 | updateAsAgent: (workspace, channelId, agentId, id, change) => |
| 656 | call("update_as_agent", { workspace, channel_id: channelId, agent_id: agentId, id, change }), |
| 657 | agentTyping: (workspace, channelId, agentId) => |
| 658 | call("agent_typing", { workspace, channel_id: channelId, agent_id: agentId }), |
| 659 | audience: (workspace, channelId) => call("audience", { workspace, channel_id: channelId }), |
| 660 | conversationForAgent: (workspace, channelId, agentId, askedBy) => |
| 661 | call("conversation_for_agent", { workspace, channel_id: channelId, agent_id: agentId, asked_by: askedBy ?? null }), |
| 662 | handOffAsAgent: (workspace, channelId, agentId, handOff) => |
| 663 | call("hand_off_as_agent", { workspace, channel_id: channelId, agent_id: agentId, hand_off: handOff }), |
| 664 | searchForAgent: (workspace, channelId, query, limit) => |
| 665 | call("search_for_agent", { workspace, channel_id: channelId, query, limit: limit ?? null }), |
| 666 | threadForAgent: (workspace, channelId, targetChannelId, id) => |
| 667 | call("thread_for_agent", { workspace, channel_id: channelId, target_channel_id: targetChannelId, id }), |
| 668 | react: (workspace, channelId, viewer, messageId, emoji) => |
| 669 | call("react", { workspace, channel_id: channelId, viewer, message_id: messageId, emoji }), |
| 670 | unreact: (workspace, channelId, viewer, messageId, emoji) => |
| 671 | call("unreact", { workspace, channel_id: channelId, viewer, message_id: messageId, emoji }), |
| 672 | reactAsAgent: (workspace, channelId, agentId, messageId, emoji, remove) => |
| 673 | call("react_as_agent", { workspace, channel_id: channelId, agent_id: agentId, message_id: messageId, emoji, remove: remove ?? false }), |
| 674 | listEmoji: (workspace, viewer) => call("list_emoji", { workspace, viewer }), |
| 675 | addEmoji: (workspace, viewer, name, file) => call("add_emoji", { workspace, viewer, name, file }), |
| 676 | aliasEmoji: (workspace, viewer, name, target) => call("alias_emoji", { workspace, viewer, name, target }), |
| 677 | removeEmoji: (workspace, viewer, name) => call("remove_emoji", { workspace, viewer, name }), |
| 678 | setEmojiUpload: (workspace, viewer, value) => call("set_emoji_upload", { workspace, viewer, value }), |
| 679 | historyForAgent: (workspace, channelId, agentId, page) => |
| 680 | call("history_for_agent", { |
| 681 | workspace, |
| 682 | channel_id: channelId, |
| 683 | agent_id: agentId, |
| 684 | thread_root: page?.thread_root ?? null, |
| 685 | limit: page?.limit ?? null, |
| 686 | }), |
| 687 | }; |
| 688 | } |