The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 5 |
| * |
| 6 | * Instead of manually building `MessageContentPart[]` arrays: |
| 7 | * ```ts |
| 8 | * // Before — manual, error-prone |
| 9 | * await ws.sendChannelMessage(channelId, { |
| 10 | * content: [ |
| 11 | * { type: "text", text: "Hey " }, |
| 12 | * { type: "mention", userId: "u_123", displayName: "Alice" }, |
| 13 | * { type: "text", text: " check this out!" }, |
| 14 | * ], |
| 15 | * }); |
| 16 | * |
| 17 | * // After — fluent builder |
| 18 | * await ws.sendChannelMessage(channelId, { |
| 19 | * content: Message.text("Hey ").mention("u_123", "Alice").text(" check this out!").build(), |
| 20 | * }); |
| 21 | * |
| 22 | * // Or use the shorthand: |
| 23 | * await ws.sendChannelMessage(channelId, Message.text("Hello everyone!").toInput()); |
| 24 | * ``` |
| 25 | */ |
| 26 | export class Message { |
| 27 | private readonly parts: MessageContentPart[] = []; |
| 28 |
| 29 | private constructor(parts?: MessageContentPart[]) { |
| 30 | if (parts) this.parts = parts; |
| 31 | } |
| 32 |
| 33 | /** Start building a message with a text segment. */ |
| 34 | static text(text: string): Message { |
| 35 | const m = new Message(); |
| 36 | m.parts.push({ type: "text", text }); |
| 37 | return m; |
| 38 | } |
| 39 |
| 40 | /** Start building a message with a mention. */ |
| 41 | static mention(userId: string, displayName: string): Message { |
| 42 | const m = new Message(); |
| 43 | m.parts.push({ type: "mention", userId, displayName }); |
| 44 | return m; |
| 45 | } |
| 46 |
| 47 | /** Start building from raw parts. */ |
| 48 | static from(parts: MessageContentPart[]): Message { |
| 49 | return new Message([...parts]); |
| 50 | } |
| 51 |
| 52 | /** Append a text segment. */ |
| 53 | text(text: string): Message { |
| 54 | this.parts.push({ type: "text", text }); |
| 55 | return this; |
| 56 | } |
| 57 |
| 58 | /** Append a user mention. */ |
| 59 | mention(userId: string, displayName: string): Message { |
| 60 | this.parts.push({ type: "mention", userId, displayName }); |
| 61 | return this; |
| 62 | } |
| 63 |
| 64 | /** Append a newline. */ |
| 65 | newline(): Message { |
| 66 | this.parts.push({ type: "text", text: "\n" }); |
| 67 | return this; |
| 68 | } |
| 69 |
| 70 | /** Get the built content parts array. */ |
| 71 | build(): MessageContentPart[] { |
| 72 | return [...this.parts]; |
| 73 | } |
| 74 |
| 75 | /** |
| 76 | * Build a `SendMessageInput`-shaped object ready to pass directly |
| 77 | * to `sendChannelMessage` / `sendDmMessage` / `replyToThread`. |
| 78 | */ |
| 79 | toInput(options?: { |
| 80 | replyToMessageId?: string | null; |
| 81 | threadRootMessageId?: string | null; |
| 82 | }): { content: MessageContentPart[]; replyToMessageId?: string | null; threadRootMessageId?: string | null } { |
| 83 | return { |
| 84 | content: this.build(), |
| 85 | ...(options?.replyToMessageId !== undefined |
| 86 | ? { replyToMessageId: options.replyToMessageId } |
| 87 | : {}), |
| 88 | ...(options?.threadRootMessageId !== undefined |
| 89 | ? { threadRootMessageId: options.threadRootMessageId } |
| 90 | : {}), |
| 91 | }; |
| 92 | } |
| 93 |
| 94 | /** Get the plain text representation (mentions become @displayName). */ |
| 95 | toPlaintext(): string { |
| 96 | return this.parts |
| 97 | .map((p) => (p.type === "text" ? p.text : `@${p.displayName}`)) |
| 98 | .join(""); |
| 99 | } |
| 100 | } |
| 101 |