Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.2 KB
Raw Blame
import { type RichMessage, toBody } from '../../mentions';
import { unwrap } from '../../utils';
import type { MacroClient } from '../../utils/client';
import type { Document } from '../documents/document';
import type { StaticImage } from '../images/image';
import { Channel } from './channel';
import { Message } from './message';
import { postToChannel } from './post';

/**
 * A thread within a channel, rooted at a top-level message. Replies and
 * thread-scoped posting live here.
 */
export class Thread {
  constructor(
    private readonly client: MacroClient,
    readonly channelId: string,
    /** The id of the message this thread hangs off. */
    readonly rootId: string,
  ) {}

  /** Serialize the thread identity without its authenticated client. */
  toJSON(): { channelId: string; rootId: string } {
    return { channelId: this.channelId, rootId: this.rootId };
  }

  /** The channel this thread is in. */
  channel(): Channel {
    return Channel.byId(this.client, this.channelId);
  }

  /** The message this thread hangs off. */
  root(): Message {
    return Message.byId(this.client, this.channelId, this.rootId);
  }

  /** List the replies in this thread, oldest first. */
  async replies(): Promise<Message[]> {
    const { replies } = unwrap(
      await this.client.storage.entityMessageGetThread({
        path: {
          parent_type: 'channel',
          parent_id: this.channelId,
          id: this.rootId,
        },
      }),
    );
    return replies.map((reply) =>
      Message.from(this.client, this.channelId, reply),
    );
  }

  /**
   * Post a reply in this thread.
   *
   * @param body - Plain text, or a rich body composed with {@link msg}.
   * @param opts.images - Static images to attach, such as one from
   *   `macro.images.generate()`.
   * @param opts.documents - Macro documents to attach as file chips, such as
   *   a file from `macro.documents.upload()`.
   * @returns The created reply.
   */
  async reply(
    body: string | RichMessage,
    opts?: { images?: StaticImage[]; documents?: Document[] },
  ): Promise<Message> {
    const id = await postToChannel(this.client, this, toBody(body), opts);
    return Message.byId(this.client, this.channelId, id);
  }
}