@desplega.ai/slack-mock 0.4.0 → 0.5.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/README.md +9 -4
- package/dist/cli.js +1 -1
- package/dist/{index-y82vvfa1.js → index-z58p5wkc.js} +2000 -1934
- package/dist/index.js +1 -1
- package/dist/render/mrkdwn.d.ts +9 -0
- package/dist/server.d.ts +1 -0
- package/dist/store.d.ts +7 -0
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/render/blocks.ts +3 -0
- package/src/render/index.ts +10 -26
- package/src/render/mrkdwn.ts +39 -0
- package/src/server.ts +2 -1
- package/src/store.ts +19 -2
- package/src/types.ts +4 -0
- package/src/web-api.ts +60 -6
package/dist/index.js
CHANGED
package/dist/render/mrkdwn.d.ts
CHANGED
|
@@ -14,3 +14,12 @@ export declare function displayName(user: SlackUser): string;
|
|
|
14
14
|
export declare function mrkdwnToHtml(text: string, ctx: RenderContext): string;
|
|
15
15
|
/** plain_text objects: escaping and emoji only, no formatting. */
|
|
16
16
|
export declare function plainTextToHtml(text: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Markdown (`markdown` blocks, `markdown_text` on streams) -> mrkdwn, so the mrkdwn renderer
|
|
19
|
+
* shows it: **bold**, *italic*, ~~strike~~, [label](url), headings, bullets and rules.
|
|
20
|
+
* Code spans and fenced code pass through untouched: they are held as placeholders while the
|
|
21
|
+
* enclosing heading, link or style is converted, then put back.
|
|
22
|
+
*/
|
|
23
|
+
export declare function markdownToMrkdwn(text: string): string;
|
|
24
|
+
/** Render Markdown to HTML through the mrkdwn renderer. */
|
|
25
|
+
export declare function markdownToHtml(text: string, ctx: RenderContext): string;
|
package/dist/server.d.ts
CHANGED
package/dist/store.d.ts
CHANGED
|
@@ -90,6 +90,8 @@ export declare class Store {
|
|
|
90
90
|
title?: string;
|
|
91
91
|
prompts?: unknown[];
|
|
92
92
|
promptsTitle?: string;
|
|
93
|
+
/** agents.sessions.setStatus: "active", "processing", "suspended" or "closed". */
|
|
94
|
+
sessionStatus?: string;
|
|
93
95
|
}>;
|
|
94
96
|
private listeners;
|
|
95
97
|
private dataFile?;
|
|
@@ -114,6 +116,9 @@ export declare class Store {
|
|
|
114
116
|
join(channelId: string, userId: string, inviter?: string): {
|
|
115
117
|
alreadyMember: boolean;
|
|
116
118
|
};
|
|
119
|
+
leave(channelId: string, userId: string): {
|
|
120
|
+
notInChannel: boolean;
|
|
121
|
+
};
|
|
117
122
|
archive(channelId: string): void;
|
|
118
123
|
private insertMessage;
|
|
119
124
|
findMessage(channelId: string, ts: string): SlackMessage | undefined;
|
|
@@ -155,6 +160,8 @@ export declare class Store {
|
|
|
155
160
|
title?: string;
|
|
156
161
|
prompts?: unknown[];
|
|
157
162
|
promptsTitle?: string;
|
|
163
|
+
/** agents.sessions.setStatus: "active", "processing", "suspended" or "closed". */
|
|
164
|
+
sessionStatus?: string;
|
|
158
165
|
};
|
|
159
166
|
}
|
|
160
167
|
/** Public (wire) form of a message: mock-only fields removed, file bytes dropped. */
|
package/dist/types.d.ts
CHANGED
|
@@ -116,6 +116,10 @@ export interface SlackMessage {
|
|
|
116
116
|
thumb_64?: string;
|
|
117
117
|
/** Mock-only: the user who can see an ephemeral message. Stripped on the wire. */
|
|
118
118
|
ephemeral_user?: string;
|
|
119
|
+
/** Mock-only: `text` came in as `markdown_text`, so the UI renders it as Markdown. Stripped on the wire. */
|
|
120
|
+
text_format?: "markdown";
|
|
121
|
+
/** Mock-only: chat.update replaced a streamed message's blocks, so they carry the body. Stripped on the wire. */
|
|
122
|
+
stream_replaced?: true;
|
|
119
123
|
}
|
|
120
124
|
export interface SlackView {
|
|
121
125
|
id: string;
|
package/package.json
CHANGED
package/src/render/blocks.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
emojiChar,
|
|
8
8
|
emojiFromUnicode,
|
|
9
9
|
escapeHtml,
|
|
10
|
+
markdownToHtml,
|
|
10
11
|
mrkdwnToHtml,
|
|
11
12
|
plainTextToHtml,
|
|
12
13
|
type RenderContext,
|
|
@@ -296,6 +297,8 @@ function renderBlock(v: unknown, ctx: RenderContext): string {
|
|
|
296
297
|
return `<div class="sm-block sm-file-block">📎 ${escapeHtml(str(block.external_id) || str(block.file_id) || "file")}</div>`;
|
|
297
298
|
case "video":
|
|
298
299
|
return videoBlock(block, ctx);
|
|
300
|
+
case "markdown":
|
|
301
|
+
return `<div class="sm-block sm-markdown">${markdownToHtml(str(block.text), ctx)}</div>`;
|
|
299
302
|
default:
|
|
300
303
|
return `<div class="sm-block">${unsupported(str(block.type))}</div>`;
|
|
301
304
|
}
|
package/src/render/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
displayName,
|
|
10
10
|
emojiChar,
|
|
11
11
|
escapeHtml,
|
|
12
|
+
markdownToHtml,
|
|
12
13
|
mrkdwnToHtml,
|
|
13
14
|
plainTextToHtml,
|
|
14
15
|
type RenderContext,
|
|
@@ -588,14 +589,18 @@ function renderMessage(
|
|
|
588
589
|
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
589
590
|
const interactive = !opts.screenshot && !flags.ephemeral && humanUsers(store).length > 0;
|
|
590
591
|
const body: string[] = [];
|
|
591
|
-
if (m.streaming_state !== undefined) {
|
|
592
|
+
if (m.streaming_state !== undefined && !m.stream_replaced) {
|
|
592
593
|
// A streamed message (chat.startStream) keeps its Markdown body; the blocks handed to
|
|
593
|
-
// chat.stopStream are appended below it, like Slack does.
|
|
594
|
-
|
|
595
|
-
|
|
594
|
+
// chat.stopStream are appended below it, like Slack does. Once chat.update replaces the
|
|
595
|
+
// blocks (agent-swarm resends the answer as a `markdown` block), they carry the body and
|
|
596
|
+
// the message renders like a posted one.
|
|
597
|
+
if (m.text) body.push(`<div class="sm-text">${markdownToHtml(m.text, ctx)}</div>`);
|
|
596
598
|
if (blocks) body.push(renderBlocks(blocks, ctx));
|
|
597
599
|
} else if (blocks) body.push(renderBlocks(blocks, ctx));
|
|
598
|
-
else if (m.text)
|
|
600
|
+
else if (m.text)
|
|
601
|
+
body.push(
|
|
602
|
+
`<div class="sm-text">${m.text_format === "markdown" ? markdownToHtml(m.text, ctx) : mrkdwnToHtml(m.text, ctx)}</div>`,
|
|
603
|
+
);
|
|
599
604
|
if (m.edited) body.push(`<div class="sm-edited">(edited)</div>`);
|
|
600
605
|
if (m.attachments?.length) body.push(renderAttachments(m.attachments, ctx));
|
|
601
606
|
body.push(filesHtml(m.files, store.bot.token));
|
|
@@ -619,27 +624,6 @@ function renderMessage(
|
|
|
619
624
|
return `<div class="sm-msg${flags.ephemeral ? " sm-msg-eph" : ""}${open}" data-ts="${escapeHtml(m.ts)}" data-channel="${escapeHtml(m.channel)}">${avatarHtml(m, name)}<div class="sm-msg-body"><div class="sm-msg-head"><span class="sm-name">${escapeHtml(name)}</span>${isApp ? `<span class="sm-badge">APP</span>` : ""}<span class="sm-time">${timeOf(m.ts)}</span>${streaming}</div>${body.join("")}</div></div>`;
|
|
620
625
|
}
|
|
621
626
|
|
|
622
|
-
/**
|
|
623
|
-
* Streamed messages carry Markdown (`markdown_text`), not mrkdwn. Convert the common
|
|
624
|
-
* constructs so the mrkdwn renderer shows them: **bold**, [label](url), headings and list
|
|
625
|
-
* bullets. Fenced code blocks pass through untouched.
|
|
626
|
-
*/
|
|
627
|
-
function markdownToMrkdwn(text: string): string {
|
|
628
|
-
return text
|
|
629
|
-
.split(/(```[\s\S]*?```)/)
|
|
630
|
-
.map((part, i) =>
|
|
631
|
-
i % 2 === 1
|
|
632
|
-
? part
|
|
633
|
-
: part
|
|
634
|
-
.replace(/\*\*(.+?)\*\*/g, "*$1*")
|
|
635
|
-
.replace(/__(.+?)__/g, "*$1*")
|
|
636
|
-
.replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, "<$2|$1>")
|
|
637
|
-
.replace(/^#{1,6}\s+(.+)$/gm, "*$1*")
|
|
638
|
-
.replace(/^(\s*)[-*]\s+/gm, "$1• "),
|
|
639
|
-
)
|
|
640
|
-
.join("");
|
|
641
|
-
}
|
|
642
|
-
|
|
643
627
|
function renderMessageList(
|
|
644
628
|
store: Store,
|
|
645
629
|
ctx: RenderContext,
|
package/src/render/mrkdwn.ts
CHANGED
|
@@ -319,3 +319,42 @@ export function plainTextToHtml(text: string): string {
|
|
|
319
319
|
if (!text) return "";
|
|
320
320
|
return renderEmoji(reEscape(text.replace(/\r\n/g, "\n"))).replace(/\n/g, "<br>");
|
|
321
321
|
}
|
|
322
|
+
|
|
323
|
+
const BOLD = "\uE002";
|
|
324
|
+
const CODE = /\uE003(\d+)\uE004/g;
|
|
325
|
+
|
|
326
|
+
/** Markdown inline styles and line markers -> mrkdwn, for one stretch of text outside code. */
|
|
327
|
+
function markdownProse(s: string): string {
|
|
328
|
+
return s
|
|
329
|
+
.replace(/^(\s*)[-*+]\s+/gm, "$1• ")
|
|
330
|
+
.replace(/^ {0,3}(?:-{3,}|\*{3,}|_{3,})\s*$/gm, "──────────")
|
|
331
|
+
.replace(/\*\*(.+?)\*\*/g, `${BOLD}$1${BOLD}`)
|
|
332
|
+
.replace(/__(.+?)__/g, `${BOLD}$1${BOLD}`)
|
|
333
|
+
.replace(/(^|[^*\w])\*(?![\s*])([^*\n]*[^\s*])\*(?![*\w])/g, "$1_$2_")
|
|
334
|
+
.replace(/~~(.+?)~~/g, "~$1~")
|
|
335
|
+
.replace(/\[([^\]]+)\]\(((?:https?:\/\/|mailto:)[^)\s]+)\)/g, "<$2|$1>")
|
|
336
|
+
.replace(
|
|
337
|
+
/^ {0,3}#{1,6}\s+(.+?)\s*#*\s*$/gm,
|
|
338
|
+
(_m, title: string) => `${BOLD}${title.replaceAll(BOLD, "")}${BOLD}`,
|
|
339
|
+
)
|
|
340
|
+
.replaceAll(BOLD, "*");
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Markdown (`markdown` blocks, `markdown_text` on streams) -> mrkdwn, so the mrkdwn renderer
|
|
345
|
+
* shows it: **bold**, *italic*, ~~strike~~, [label](url), headings, bullets and rules.
|
|
346
|
+
* Code spans and fenced code pass through untouched: they are held as placeholders while the
|
|
347
|
+
* enclosing heading, link or style is converted, then put back.
|
|
348
|
+
*/
|
|
349
|
+
export function markdownToMrkdwn(text: string): string {
|
|
350
|
+
const code: string[] = [];
|
|
351
|
+
const held = text
|
|
352
|
+
.replace(/\r\n/g, "\n")
|
|
353
|
+
.replace(/```[\s\S]*?```|`[^`\n]+`/g, (m) => `\uE003${code.push(m) - 1}\uE004`);
|
|
354
|
+
return markdownProse(held).replace(CODE, (_m, i: string) => code[Number(i)] ?? "");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Render Markdown to HTML through the mrkdwn renderer. */
|
|
358
|
+
export function markdownToHtml(text: string, ctx: RenderContext): string {
|
|
359
|
+
return mrkdwnToHtml(markdownToMrkdwn(text), ctx);
|
|
360
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -460,7 +460,8 @@ export class SlackMock {
|
|
|
460
460
|
};
|
|
461
461
|
try {
|
|
462
462
|
const body = handler(ctx, args);
|
|
463
|
-
|
|
463
|
+
// A handler may return `ok: false` for a documented failure without an `error` code.
|
|
464
|
+
record(body.ok !== false);
|
|
464
465
|
return Response.json({ ok: true, ...body });
|
|
465
466
|
} catch (e) {
|
|
466
467
|
if (e instanceof SlackApiError) {
|
package/src/store.ts
CHANGED
|
@@ -119,7 +119,14 @@ export class Store {
|
|
|
119
119
|
readonly apiCalls: ApiCall[] = [];
|
|
120
120
|
readonly assistantThreads = new Map<
|
|
121
121
|
string,
|
|
122
|
-
{
|
|
122
|
+
{
|
|
123
|
+
status?: string;
|
|
124
|
+
title?: string;
|
|
125
|
+
prompts?: unknown[];
|
|
126
|
+
promptsTitle?: string;
|
|
127
|
+
/** agents.sessions.setStatus: "active", "processing", "suspended" or "closed". */
|
|
128
|
+
sessionStatus?: string;
|
|
129
|
+
}
|
|
123
130
|
>();
|
|
124
131
|
private listeners = new Set<(change: Change) => void>();
|
|
125
132
|
private dataFile?: string;
|
|
@@ -359,6 +366,16 @@ export class Store {
|
|
|
359
366
|
return { alreadyMember: false };
|
|
360
367
|
}
|
|
361
368
|
|
|
369
|
+
leave(channelId: string, userId: string): { notInChannel: boolean } {
|
|
370
|
+
const channel = this.channel(channelId);
|
|
371
|
+
if (channel.is_archived) throw new SlackApiError("is_archived");
|
|
372
|
+
if (channel.is_general) throw new SlackApiError("cant_leave_general");
|
|
373
|
+
if (!channel.members.includes(userId)) return { notInChannel: true };
|
|
374
|
+
channel.members = channel.members.filter((m) => m !== userId);
|
|
375
|
+
this.emit({ kind: "channel.update", channel });
|
|
376
|
+
return { notInChannel: false };
|
|
377
|
+
}
|
|
378
|
+
|
|
362
379
|
archive(channelId: string): void {
|
|
363
380
|
const channel = this.channel(channelId);
|
|
364
381
|
if (channel.is_archived) throw new SlackApiError("already_archived");
|
|
@@ -656,7 +673,7 @@ function guessMime(ext: string): string {
|
|
|
656
673
|
|
|
657
674
|
/** Public (wire) form of a message: mock-only fields removed, file bytes dropped. */
|
|
658
675
|
export function wireMessage(m: SlackMessage): Record<string, unknown> {
|
|
659
|
-
const { ephemeral_user: _e, ...rest } = m;
|
|
676
|
+
const { ephemeral_user: _e, text_format: _f, stream_replaced: _r, ...rest } = m;
|
|
660
677
|
if (rest.files) rest.files = rest.files.map(wireFile);
|
|
661
678
|
return rest;
|
|
662
679
|
}
|
package/src/types.ts
CHANGED
|
@@ -103,6 +103,10 @@ export interface SlackMessage {
|
|
|
103
103
|
thumb_64?: string;
|
|
104
104
|
/** Mock-only: the user who can see an ephemeral message. Stripped on the wire. */
|
|
105
105
|
ephemeral_user?: string;
|
|
106
|
+
/** Mock-only: `text` came in as `markdown_text`, so the UI renders it as Markdown. Stripped on the wire. */
|
|
107
|
+
text_format?: "markdown";
|
|
108
|
+
/** Mock-only: chat.update replaced a streamed message's blocks, so they carry the body. Stripped on the wire. */
|
|
109
|
+
stream_replaced?: true;
|
|
106
110
|
}
|
|
107
111
|
|
|
108
112
|
export interface SlackView {
|
package/src/web-api.ts
CHANGED
|
@@ -65,10 +65,24 @@ function botFields(ctx: ApiContext): Partial<SlackMessage> {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* `markdown_text` is Slack's markdown input (`**bold**`, lists, code fences).
|
|
70
|
+
* Slack rejects it next to `text` or `blocks` and renders it server-side; the
|
|
71
|
+
* mock keeps the markdown as the message `text`.
|
|
72
|
+
*/
|
|
73
|
+
function markdownText(args: Args): string | undefined {
|
|
74
|
+
const markdown = str(args, "markdown_text");
|
|
75
|
+
if (markdown === undefined) return undefined;
|
|
76
|
+
if (str(args, "text") || (Array.isArray(args.blocks) && args.blocks.length))
|
|
77
|
+
throw new SlackApiError("markdown_text_conflict");
|
|
78
|
+
return markdown;
|
|
79
|
+
}
|
|
80
|
+
|
|
68
81
|
function messageContent(
|
|
69
82
|
args: Args,
|
|
70
|
-
): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata"> {
|
|
71
|
-
const
|
|
83
|
+
): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata" | "text_format"> {
|
|
84
|
+
const markdown = markdownText(args);
|
|
85
|
+
const text = markdown ?? str(args, "text") ?? "";
|
|
72
86
|
const blocks = Array.isArray(args.blocks) ? (args.blocks as unknown[]) : undefined;
|
|
73
87
|
const attachments = Array.isArray(args.attachments) ? (args.attachments as unknown[]) : undefined;
|
|
74
88
|
if (!text && !blocks?.length && !attachments?.length) throw new SlackApiError("no_text");
|
|
@@ -80,7 +94,13 @@ function messageContent(
|
|
|
80
94
|
args.metadata && typeof args.metadata === "object"
|
|
81
95
|
? (args.metadata as SlackMessage["metadata"])
|
|
82
96
|
: undefined;
|
|
83
|
-
return {
|
|
97
|
+
return {
|
|
98
|
+
text,
|
|
99
|
+
blocks,
|
|
100
|
+
attachments,
|
|
101
|
+
metadata,
|
|
102
|
+
...(markdown !== undefined ? { text_format: "markdown" } : {}),
|
|
103
|
+
};
|
|
84
104
|
}
|
|
85
105
|
|
|
86
106
|
function channelInfo(store: Store, c: SlackChannel, actor: Actor): Record<string, unknown> {
|
|
@@ -191,9 +211,19 @@ export const handlers: Record<string, Handler> = {
|
|
|
191
211
|
if (existing.user !== ctx.actor.userId && !ctx.actor.isBot)
|
|
192
212
|
throw new SlackApiError("cant_update_message");
|
|
193
213
|
const patch: Partial<SlackMessage> = {};
|
|
194
|
-
|
|
195
|
-
if (
|
|
214
|
+
const markdown = markdownText(args);
|
|
215
|
+
if (markdown !== undefined) {
|
|
216
|
+
patch.text = markdown;
|
|
217
|
+
patch.text_format = "markdown";
|
|
218
|
+
} else if (args.text !== undefined) {
|
|
219
|
+
patch.text = str(args, "text") ?? "";
|
|
220
|
+
patch.text_format = undefined;
|
|
221
|
+
}
|
|
222
|
+
if (args.blocks !== undefined) {
|
|
196
223
|
patch.blocks = Array.isArray(args.blocks) ? (args.blocks as unknown[]) : undefined;
|
|
224
|
+
// On a streamed message the new blocks replace the body rather than trail it.
|
|
225
|
+
if (existing.streaming_state !== undefined) patch.stream_replaced = true;
|
|
226
|
+
}
|
|
197
227
|
if (args.attachments !== undefined)
|
|
198
228
|
patch.attachments = Array.isArray(args.attachments)
|
|
199
229
|
? (args.attachments as unknown[])
|
|
@@ -263,7 +293,11 @@ export const handlers: Record<string, Handler> = {
|
|
|
263
293
|
streaming_state: "completed",
|
|
264
294
|
text: existing.text + streamText(args),
|
|
265
295
|
};
|
|
266
|
-
|
|
296
|
+
// Blocks handed to stopStream trail the body, even after a mid-stream chat.update.
|
|
297
|
+
if (Array.isArray(args.blocks)) {
|
|
298
|
+
patch.blocks = args.blocks as unknown[];
|
|
299
|
+
patch.stream_replaced = undefined;
|
|
300
|
+
}
|
|
267
301
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
|
|
268
302
|
markEdited: false,
|
|
269
303
|
});
|
|
@@ -353,6 +387,12 @@ export const handlers: Record<string, Handler> = {
|
|
|
353
387
|
};
|
|
354
388
|
},
|
|
355
389
|
|
|
390
|
+
"conversations.leave": (ctx, args) => {
|
|
391
|
+
const { notInChannel } = ctx.store.leave(required(args, "channel"), ctx.actor.userId);
|
|
392
|
+
// Slack answers a non-member with this exact shape: ok false, no `error`.
|
|
393
|
+
return notInChannel ? { ok: false, not_in_channel: true } : {};
|
|
394
|
+
},
|
|
395
|
+
|
|
356
396
|
"conversations.archive": (ctx, args) => {
|
|
357
397
|
ctx.store.archive(required(args, "channel"));
|
|
358
398
|
return {};
|
|
@@ -595,6 +635,20 @@ export const handlers: Record<string, Handler> = {
|
|
|
595
635
|
return {};
|
|
596
636
|
},
|
|
597
637
|
|
|
638
|
+
// Agent session status (the "processing" / "suspended" / "active" / "closed" indicator on an
|
|
639
|
+
// agent thread). Kept on the assistant thread record; the UI does not show it. The mock has
|
|
640
|
+
// one agent per session, so the session status is the caller's own status.
|
|
641
|
+
"agents.sessions.setStatus": (ctx, args) => {
|
|
642
|
+
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
643
|
+
const status = required(args, "status");
|
|
644
|
+
if (!["active", "processing", "suspended", "closed"].includes(status))
|
|
645
|
+
throw new SlackApiError("invalid_status");
|
|
646
|
+
const threadTs = str(args, "thread_ts");
|
|
647
|
+
if (!threadTs) throw new SlackApiError("thread_ts_required");
|
|
648
|
+
ctx.store.assistantThread(channel.id, threadTs).sessionStatus = status;
|
|
649
|
+
return { status, agent_status: status };
|
|
650
|
+
},
|
|
651
|
+
|
|
598
652
|
"assistant.threads.setTitle": (ctx, args) => {
|
|
599
653
|
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
600
654
|
const threadTs = required(args, "thread_ts");
|