Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions packages/adapter-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import type {
Logger,
ModalElement,
ModalResponse,
PlanContent,
PlanModel,
RawMessage,
ReactionEvent,
Root,
Expand All @@ -50,6 +52,7 @@ import {
parseMarkdown,
StreamingMarkdownRenderer,
toModalElement,
toPlainText,
} from "chat";
import { cardToBlockKit, cardToFallbackText, type SlackBlock } from "./cards";
import type { EncryptedTokenData } from "./crypto";
Expand Down Expand Up @@ -2801,6 +2804,176 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
}
}

// ===========================================================================
// PostableObject (Plan, etc.) support
// ===========================================================================

async postObject(
threadId: string,
kind: string,
data: unknown
): Promise<RawMessage<unknown>> {
if (kind !== "plan") {
// Unsupported kind — post as plain text fallback
return this.postMessage(threadId, `[${kind}]`);
}

const plan = data as PlanModel;
const { channel, threadTs } = this.decodeThreadId(threadId);
const text = this.renderPlanFallbackText(plan);
const blocks = this.planToBlockKit(plan);

try {
this.logger.debug("Slack API: chat.postMessage (plan)", {
channel,
threadTs,
blockCount: blocks.length,
});
const result = await this.client.chat.postMessage(
this.withToken({
channel,
thread_ts: threadTs,
text,
// biome-ignore lint/suspicious/noExplicitAny: Block Kit blocks are platform-specific
blocks: blocks as any[],
unfurl_links: false,
unfurl_media: false,
})
);
return { id: result.ts as string, threadId, raw: result };
} catch (error) {
this.handleSlackError(error);
}
}

async editObject(
threadId: string,
messageId: string,
kind: string,
data: unknown
): Promise<RawMessage<unknown>> {
if (kind !== "plan") {
// Unsupported kind — edit as plain text fallback
return this.editMessage(threadId, messageId, `[${kind}]`);
}

const plan = data as PlanModel;
const { channel } = this.decodeThreadId(threadId);
const text = this.renderPlanFallbackText(plan);
const blocks = this.planToBlockKit(plan);

try {
this.logger.debug("Slack API: chat.update (plan)", {
channel,
messageId,
blockCount: blocks.length,
});
const result = await this.client.chat.update(
this.withToken({
channel,
ts: messageId,
text,
// biome-ignore lint/suspicious/noExplicitAny: Block Kit blocks are platform-specific
blocks: blocks as any[],
})
);

return { id: result.ts as string, threadId, raw: result };
} catch (error) {
this.handleSlackError(error);
}
}

private renderPlanFallbackText(plan: PlanModel): string {
const lines: string[] = [];
lines.push(plan.title || "Plan");
for (const task of plan.tasks) {
lines.push(`- (${task.status}) ${task.title}`);
}
return lines.join("\n");
}

private planToBlockKit(plan: PlanModel): unknown[] {
const tasks = plan.tasks.map((task: PlanModel["tasks"][number]) => {
const details = this.planContentToRichText(task.details);
const output = this.planContentToRichText(task.output);
return {
type: "task_card",
task_id: task.id,
title: task.title,
status: task.status,
...(details ? { details } : null),
...(output ? { output } : null),
};
});
return [
{
type: "plan",
title: plan.title || "Plan",
tasks,
},
];
}

private planContentToPlainText(content: PlanContent | undefined): string {
if (!content) {
return "";
}
if (Array.isArray(content)) {
return content.join("\n");
}
if (typeof content === "string") {
return content;
}
if ("markdown" in content) {
return toPlainText(parseMarkdown(content.markdown));
}
if ("ast" in content) {
return toPlainText(content.ast);
}
return "";
}

private planContentToRichText(
content: PlanContent | undefined
): { type: "rich_text"; elements: unknown[] } | undefined {
if (!content) {
return undefined;
}
if (Array.isArray(content)) {
return {
type: "rich_text",
elements: [
{
type: "rich_text_list",
style: "bullet",
elements: content.map((item) => ({
type: "rich_text_section",
elements: [{ type: "text", text: String(item) }],
})),
},
],
};
}
const text = this.planContentToPlainText(content);
if (!text) {
return undefined;
}
return {
type: "rich_text",
elements: [
{
type: "rich_text_section",
elements: [{ type: "text", text }],
},
],
};
}

// ===========================================================================
// Message deletion and reactions
// ===========================================================================

async deleteMessage(threadId: string, messageId: string): Promise<void> {
const ephemeral = this.decodeEphemeralMessageId(messageId);
if (ephemeral) {
Expand Down
25 changes: 24 additions & 1 deletion packages/chat/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "./markdown";
import { Message } from "./message";
import type { MessageHistoryCache } from "./message-history";
import { isPostableObject, postPostableObject } from "./postable-object";
import type {
Adapter,
AdapterPostableMessage,
Expand All @@ -21,6 +22,7 @@ import type {
ChannelVisibility,
EphemeralMessage,
PostableMessage,
PostableObject,
PostEphemeralOptions,
ScheduledMessage,
SentMessage,
Expand Down Expand Up @@ -263,9 +265,22 @@ export class ChannelImpl<TState = Record<string, unknown>>
};
}

async post<T extends PostableObject>(message: T): Promise<T>;
async post(
message:
| string
| AdapterPostableMessage
| AsyncIterable<string>
| ChatElement
): Promise<SentMessage>;
async post(
message: string | PostableMessage | ChatElement
): Promise<SentMessage> {
): Promise<SentMessage | PostableObject> {
if (isPostableObject(message)) {
await this.handlePostableObject(message);
return message;
}

// Handle AsyncIterable (streaming) — not supported at channel level,
// fall through to postMessage
if (isAsyncIterable(message)) {
Expand Down Expand Up @@ -294,6 +309,14 @@ export class ChannelImpl<TState = Record<string, unknown>>
return this.postSingleMessage(postable);
}

private async handlePostableObject(obj: PostableObject): Promise<void> {
await postPostableObject(obj, this.adapter, this.id, (threadId, message) =>
this.adapter.postChannelMessage
? this.adapter.postChannelMessage(threadId, message)
: this.adapter.postMessage(threadId, message)
);
}

private async postSingleMessage(
postable: AdapterPostableMessage
): Promise<SentMessage> {
Expand Down
19 changes: 18 additions & 1 deletion packages/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ export {
MessageHistoryCache,
type MessageHistoryConfig,
} from "./message-history";
export type {
AddTaskOptions,
CompletePlanOptions,
PlanContent,
PlanModel,
PlanModelTask,
PlanTask,
PlanTaskStatus,
StartPlanOptions,
UpdateTaskInput,
} from "./plan";
export { Plan } from "./plan";
export type {
PostableObject,
PostableObjectContext,
} from "./postable-object";
export { isPostableObject } from "./postable-object";
export { StreamingMarkdownRenderer } from "./streaming-markdown";
export { type SerializedThread, ThreadImpl } from "./thread";

Expand Down Expand Up @@ -255,7 +272,7 @@ export type {
TextInputElement,
TextInputOptions,
} from "./modals";
// Types
// Types (Plan types are exported from ./plan, PostableObject types from ./postable-object)
export type {
ActionEvent,
ActionHandler,
Expand Down
Loading
Loading