@desplega.ai/slack-mock 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/web-api.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // Slack Web API method handlers. Each receives the parsed args and returns the
2
2
  // `ok: true` body; they throw SlackApiError for `ok: false` responses.
3
3
 
4
+ import { validateBlocks } from "./block-limits.ts";
4
5
  import { type Args, bool, num, str } from "./body.ts";
5
6
  import { nextTs, nowUnix, slackId } from "./ids.ts";
6
7
  import type { SocketModeHub } from "./socket-mode.ts";
@@ -80,20 +81,24 @@ function markdownText(args: Args): string | undefined {
80
81
 
81
82
  function messageContent(
82
83
  args: Args,
83
- ): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata"> {
84
- const text = markdownText(args) ?? str(args, "text") ?? "";
84
+ ): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata" | "text_format"> {
85
+ const markdown = markdownText(args);
86
+ const text = markdown ?? str(args, "text") ?? "";
85
87
  const blocks = Array.isArray(args.blocks) ? (args.blocks as unknown[]) : undefined;
86
88
  const attachments = Array.isArray(args.attachments) ? (args.attachments as unknown[]) : undefined;
87
89
  if (!text && !blocks?.length && !attachments?.length) throw new SlackApiError("no_text");
88
- if (blocks && blocks.length > 50)
89
- throw new SlackApiError("invalid_blocks", {
90
- response_metadata: { messages: ["[ERROR] must have at most 50 blocks"] },
91
- });
90
+ validateBlocks(blocks);
92
91
  const metadata =
93
92
  args.metadata && typeof args.metadata === "object"
94
93
  ? (args.metadata as SlackMessage["metadata"])
95
94
  : undefined;
96
- return { text, blocks, attachments, metadata };
95
+ return {
96
+ text,
97
+ blocks,
98
+ attachments,
99
+ metadata,
100
+ ...(markdown !== undefined ? { text_format: "markdown" } : {}),
101
+ };
97
102
  }
98
103
 
99
104
  function channelInfo(store: Store, c: SlackChannel, actor: Actor): Record<string, unknown> {
@@ -205,10 +210,19 @@ export const handlers: Record<string, Handler> = {
205
210
  throw new SlackApiError("cant_update_message");
206
211
  const patch: Partial<SlackMessage> = {};
207
212
  const markdown = markdownText(args);
208
- if (markdown !== undefined) patch.text = markdown;
209
- else if (args.text !== undefined) patch.text = str(args, "text") ?? "";
210
- if (args.blocks !== undefined)
213
+ if (markdown !== undefined) {
214
+ patch.text = markdown;
215
+ patch.text_format = "markdown";
216
+ } else if (args.text !== undefined) {
217
+ patch.text = str(args, "text") ?? "";
218
+ patch.text_format = undefined;
219
+ }
220
+ if (args.blocks !== undefined) {
211
221
  patch.blocks = Array.isArray(args.blocks) ? (args.blocks as unknown[]) : undefined;
222
+ validateBlocks(patch.blocks);
223
+ // On a streamed message the new blocks replace the body rather than trail it.
224
+ if (existing.streaming_state !== undefined) patch.stream_replaced = true;
225
+ }
212
226
  if (args.attachments !== undefined)
213
227
  patch.attachments = Array.isArray(args.attachments)
214
228
  ? (args.attachments as unknown[])
@@ -278,7 +292,11 @@ export const handlers: Record<string, Handler> = {
278
292
  streaming_state: "completed",
279
293
  text: existing.text + streamText(args),
280
294
  };
281
- if (Array.isArray(args.blocks)) patch.blocks = args.blocks as unknown[];
295
+ // Blocks handed to stopStream trail the body, even after a mid-stream chat.update.
296
+ if (Array.isArray(args.blocks)) {
297
+ patch.blocks = args.blocks as unknown[];
298
+ patch.stream_replaced = undefined;
299
+ }
282
300
  const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
283
301
  markEdited: false,
284
302
  });
@@ -368,6 +386,12 @@ export const handlers: Record<string, Handler> = {
368
386
  };
369
387
  },
370
388
 
389
+ "conversations.leave": (ctx, args) => {
390
+ const { notInChannel } = ctx.store.leave(required(args, "channel"), ctx.actor.userId);
391
+ // Slack answers a non-member with this exact shape: ok false, no `error`.
392
+ return notInChannel ? { ok: false, not_in_channel: true } : {};
393
+ },
394
+
371
395
  "conversations.archive": (ctx, args) => {
372
396
  ctx.store.archive(required(args, "channel"));
373
397
  return {};
@@ -610,6 +634,20 @@ export const handlers: Record<string, Handler> = {
610
634
  return {};
611
635
  },
612
636
 
637
+ // Agent session status (the "processing" / "suspended" / "active" / "closed" indicator on an
638
+ // agent thread). Kept on the assistant thread record; the UI does not show it. The mock has
639
+ // one agent per session, so the session status is the caller's own status.
640
+ "agents.sessions.setStatus": (ctx, args) => {
641
+ const channel = ctx.store.channel(required(args, "channel_id"));
642
+ const status = required(args, "status");
643
+ if (!["active", "processing", "suspended", "closed"].includes(status))
644
+ throw new SlackApiError("invalid_status");
645
+ const threadTs = str(args, "thread_ts");
646
+ if (!threadTs) throw new SlackApiError("thread_ts_required");
647
+ ctx.store.assistantThread(channel.id, threadTs).sessionStatus = status;
648
+ return { status, agent_status: status };
649
+ },
650
+
613
651
  "assistant.threads.setTitle": (ctx, args) => {
614
652
  const channel = ctx.store.channel(required(args, "channel_id"));
615
653
  const threadTs = required(args, "thread_ts");