---
title: Team chat
summary: "`host.chat`: the team's own messaging (channels, DMs, threads, reactions, pins), kept and checked by the core, acting as the signed-in person."
audience: both
stability: experimental
since: protocol 1
---

# Team chat

`host.chat` is the team's own messaging, Slack style: public and private channels, direct messages, threads,
reactions, pins, read state and typing. The core keeps every conversation and message, decides who may see and do
what, stamps authors from the session, and notifies people about DMs, mentions and thread replies.

```ts
interface SpacemixHost {
  chat?: ChatApi; // present with `chat`
}

interface ChatApi {
  people(): Promise<ChatPerson[]>;
  conversations(): Promise<ChatConversation[]>;
  history(conversation: string, options?: { before?: string; limit?: number }): Promise<{ messages: ChatMessage[]; more: boolean }>;
  thread(conversation: string, root: string): Promise<{ root: ChatMessage; replies: ChatMessage[] }>;
  search(query: string, options?: { conversation?: string; from?: string; limit?: number }): Promise<ChatMessage[]>;
  threads(options?: { limit?: number }): Promise<ChatMessage[]>;
  mentions(options?: { limit?: number }): Promise<ChatMessage[]>;
  pins(conversation: string): Promise<ChatMessage[]>;
  send(conversation: string, text: string, options?: { thread?: string; broadcast?: boolean; via?: ChatVia }): Promise<ChatMessage>;
  edit(conversation: string, id: string, text: string): Promise<ChatMessage>;
  delete(conversation: string, id: string): Promise<void>;
  react(conversation: string, id: string, emoji: string, on: boolean): Promise<ChatMessage>;
  pin(conversation: string, id: string, pinned: boolean): Promise<ChatMessage>;
  markRead(conversation: string, at: number, options?: { force?: boolean }): Promise<void>;
  createChannel(input: { name: string; private?: boolean; topic?: string; purpose?: string; members?: string[] }): Promise<ChatConversation>;
  openDm(users: string[]): Promise<ChatConversation>;
  updateChannel(conversation: string, patch: { name?: string; topic?: string; purpose?: string }): Promise<ChatConversation>;
  join(conversation: string): Promise<ChatConversation>;
  leave(conversation: string): Promise<ChatConversation>;
  invite(conversation: string, users: string[]): Promise<ChatConversation>;
  archive(conversation: string, archived: boolean): Promise<ChatConversation>;
  prefs(conversation: string, prefs: { muted?: boolean; starred?: boolean }): Promise<ChatConversation>;
  typing(conversation: string, thread?: string): Promise<void>;
  subscribe(fn: (event: ChatEvent) => void): () => void;
}
```

The shapes `ChatConversation`, `ChatMessage`, `ChatPerson`, `ChatEvent` and `ChatVia`, and the constant
`SPACEMIX_AUTHOR`, are in `packages/sdk/src/index.ts` with a comment on every field.

**Permission:** `chat`. Without it `host.chat` is `undefined`.

**Who is acting.** Every call acts as the person signed in where the extension runs. The core checks membership
and authorship on every call; an extension can't act for someone else.

**Visibility.** A conversation the caller can't see behaves as if it didn't exist: `No conversation with id "<id>".`

## Rules the core enforces

| Rule | Limit or behavior |
|---|---|
| Message text | 1–4 000 characters (`A message needs some text.`, `Messages are limited to 4,000 characters; this is N.`) |
| Channel name | lowercase letters, digits, `-` and `_`, starting with a letter or digit; 1–80 characters; unique |
| Topic, purpose | at most 250 characters, one line |
| DM | 1–9 people including the caller; members are fixed |
| Reactions | one emoji per reaction (at most 32 bytes), at most 50 different reactions per message |
| `send`, `edit`, `react`, `pin`, `typing` | the caller must be a member (`Join #name to do that.`) and the conversation not archived (`#name is archived.`) |
| Edit | own messages only (`You can only edit your own messages.`) |
| Delete | own messages; the team owner may delete anyone's |
| Rename, archive | the channel's creator or the team owner; `#general` can't be renamed, archived or left |
| Replies | go to a top-level message in the same conversation that isn't deleted |

## Reading

- **`people`**: everyone on the team, with `me` marking the caller. Bridge method: `chat.people`.
- **`conversations`**: public channels (joined or not), the caller's private channels and DMs, archived ones
  included, with the caller's `unread`, `mentions`, `lastRead`, `muted` and `starred`. Creates `#general` on first
  use. Bridge method: `chat.conversations`.
- **`history`**: top-level messages and replies also sent to the channel, oldest first. `before` (a message id)
  pages back; `limit` default 50, at most 200; `more` says whether older ones exist. Bridge method:
  `chat.history`.
- **`thread`**: a root and all its replies, oldest first. Bridge method: `chat.thread`.
- **`search`**: case-insensitive text search over what the caller can see, newest first, optionally in one
  `conversation` or `from` one user id. `limit` default 20, at most 100. Bridge method: `chat.search`.
- **`threads`**: thread roots the caller started, replied to or was mentioned in, latest reply first. `limit`
  default 20, at most 50. Bridge method: `chat.threads`.
- **`mentions`**: messages and replies mentioning the caller (including `@channel`/`@here`), newest first. `limit`
  default 30, at most 100. Bridge method: `chat.mentions`.
- **`pins`**: pinned messages in a conversation, newest first. Bridge method: `chat.pins`.

## Writing

- **`send`**: posts as the caller. `thread` makes it a reply; `broadcast` also shows a reply in the channel. With
  `via`, the message posts as SpaceMIX instead (author `SPACEMIX_AUTHOR`, `via` shown next to it); `via.extension`
  and `via.name` must both be non-empty (`name` is cut to 80 characters). Use `via` only when your extension acts
  on its own (an automation), never to hide that a person wrote something. Bridge method: `chat.send`.
- **`edit`**, **`delete`**: own messages (see the rules). A deleted thread root stays as `deleted: true` with empty
  text so its replies stay reachable. Bridge methods: `chat.edit`, `chat.delete`.
- **`react`**: adds (`on: true`) or removes the caller's reaction. Bridge method: `chat.react`.
- **`pin`**: pins or unpins. Bridge method: `chat.pin`.
- **`markRead`**: the caller has read everything up to `at` (ms). It never moves backwards unless `force`. Bridge
  method: `chat.markRead`.
- **`createChannel`**: creates a channel with the caller and `members`. Bridge method: `chat.createChannel`.
- **`openDm`**: the DM between the caller and `users` (up to 8 others; `[]` is a DM with yourself), created if
  needed. Bridge method: `chat.openDm`.
- **`updateChannel`**: members may change the topic and purpose; renaming is for the creator and the team owner.
  Bridge method: `chat.updateChannel`.
- **`join`** (public channels only), **`leave`**, **`invite`** (members may add people to channels),
  **`archive`**, **`prefs`** (the caller's own `muted` and `starred`). Bridge methods: `chat.join`, `chat.leave`,
  `chat.invite`, `chat.archive`, `chat.prefs`.
- **`typing`**: tells the other members the caller is typing, in `thread` if given. Call it at most every 3
  seconds while they type; the indicator lasts about 5 seconds. Bridge method: `chat.typing`.

## Text format

Message text is Slack-style mrkdwn: `*bold*`, `_italic_`, `~strike~`, `` `code` ``, fenced blocks, `> quotes` and
bare http(s) links. Mentions are tokens: `<@userId>` for a person, `<!channel>` and `<!here>` for everyone in the
conversation; the core parses them into `mentions` and `mentionsAll`. A widget is placed as `<widget:ID|Name>`
(see [widgets.md](../widgets.md)). Emoji are plain Unicode.

## `host.chat.subscribe`

```ts
subscribe(fn: (event: ChatEvent) => void): () => void;
```

Called on every change the caller can see, from any window or browser: `message` (new, edited, deleted, reacted,
pinned), `conversation` (created, renamed, members, archived, prefs), `read` (the caller read it elsewhere),
`typing`. `resync` means events may have been missed: reload everything. Events only say what changed; reload it
with the read calls. Channel: `chat`.

## Notifications and events

The core notifies people about DMs, mentions and thread replies through their notification center, and records
the events `message_sent` and `channel_created`. **Both are sent as the extension `ai.spacemix.chat`**, whichever
extension made the call: clicking such a notification opens that extension's view, with target
`<conversation>`, `<conversation>/<message>` or `<conversation>/<root>/<reply>`.

The events log is readable by every extension with `events` and every API key with `events:read`, so a
`message_sent` event from a **private channel** carries no text: its payload has only the ids (`conversation`,
`name`, `private: true`, `message`, `author`, `thread`) and its summary is `A message in a private channel`. Events
from public channels carry the text; direct messages record no event at all.

The team chat's own tables are `ai.spacemix.chat.conversations`, `ai.spacemix.chat.messages` and
`ai.spacemix.chat.prefs`. They belong to the core, not to an extension: an extension with the id
`ai.spacemix.chat` can't declare collections named `conversations`, `messages` or `prefs`, and using one fails
with `ai.spacemix.chat / <name> is reserved for the team chat.`

## Example

```ts
import { host } from "@spacemix/sdk";

export async function postToGeneral(text: string): Promise<string | null> {
  const chat = host.chat;
  if (!chat) return null;
  const general = (await chat.conversations()).find((c) => c.everyone);
  if (!general) return null;
  return (await chat.send(general.id, text)).id;
}
```

## Stability

Experimental: the core's notifications and events are tied to one extension id (`ai.spacemix.chat`), and `via` is
taken from the caller as given. Both are likely to change before this is frozen.
