@desplega.ai/slack-mock 0.4.1 → 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 +8 -5
- package/dist/cli.js +1 -1
- package/dist/{index-86bw4z6k.js → index-z58p5wkc.js} +69 -14
- 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 +46 -7
package/README.md
CHANGED
|
@@ -134,12 +134,15 @@ Stitch the PNGs into a GIF with ffmpeg if you want motion; the mock only produce
|
|
|
134
134
|
`assistant_thread_context_changed`. The bot's own messages are echoed back
|
|
135
135
|
like Slack does (Bolt's `ignoreSelf` drops them).
|
|
136
136
|
- Web API: auth, chat (post, update, delete, ephemeral, permalink, streams,
|
|
137
|
-
`markdown_text` kept as the message text
|
|
138
|
-
`text` or `blocks`),
|
|
139
|
-
conversations (list, info, create, join, invite, archive, history,
|
|
140
|
-
members, open), users (info, list, lookupByEmail), reactions, files
|
|
137
|
+
`markdown_text` kept as the message text and rendered as Markdown,
|
|
138
|
+
`markdown_text_conflict` next to `text` or `blocks`),
|
|
139
|
+
conversations (list, info, create, join, invite, leave, archive, history,
|
|
140
|
+
replies, members, open), users (info, list, lookupByEmail), reactions, files
|
|
141
141
|
(info, upload v2 flow, download with bearer token), views (open, update,
|
|
142
|
-
push), assistant.threads (status, title, prompts),
|
|
142
|
+
push), assistant.threads (status, title, prompts), agents.sessions.setStatus,
|
|
143
|
+
pins, bots, team.
|
|
144
|
+
- UI: Block Kit `markdown` blocks render as Markdown (headings, bold, italic,
|
|
145
|
+
strike, links, lists, code); unknown blocks show a gray `unsupported: <type>` box.
|
|
143
146
|
Errors come back as `{ ok: false, error }` with the real codes
|
|
144
147
|
(`not_in_channel`, `already_reacted`, `message_not_found`, `name_taken`,
|
|
145
148
|
`expired_trigger_id`, `message_not_in_streaming_state`, ...).
|
package/dist/cli.js
CHANGED
|
@@ -274,6 +274,20 @@ function plainTextToHtml(text) {
|
|
|
274
274
|
return renderEmoji(reEscape(text.replace(/\r\n/g, `
|
|
275
275
|
`))).replace(/\n/g, "<br>");
|
|
276
276
|
}
|
|
277
|
+
var BOLD = "\uE002";
|
|
278
|
+
var CODE = /\uE003(\d+)\uE004/g;
|
|
279
|
+
function markdownProse(s) {
|
|
280
|
+
return s.replace(/^(\s*)[-*+]\s+/gm, "$1\u2022 ").replace(/^ {0,3}(?:-{3,}|\*{3,}|_{3,})\s*$/gm, "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500").replace(/\*\*(.+?)\*\*/g, `${BOLD}$1${BOLD}`).replace(/__(.+?)__/g, `${BOLD}$1${BOLD}`).replace(/(^|[^*\w])\*(?![\s*])([^*\n]*[^\s*])\*(?![*\w])/g, "$1_$2_").replace(/~~(.+?)~~/g, "~$1~").replace(/\[([^\]]+)\]\(((?:https?:\/\/|mailto:)[^)\s]+)\)/g, "<$2|$1>").replace(/^ {0,3}#{1,6}\s+(.+?)\s*#*\s*$/gm, (_m, title) => `${BOLD}${title.replaceAll(BOLD, "")}${BOLD}`).replaceAll(BOLD, "*");
|
|
281
|
+
}
|
|
282
|
+
function markdownToMrkdwn(text) {
|
|
283
|
+
const code = [];
|
|
284
|
+
const held = text.replace(/\r\n/g, `
|
|
285
|
+
`).replace(/```[\s\S]*?```|`[^`\n]+`/g, (m) => `\uE003${code.push(m) - 1}\uE004`);
|
|
286
|
+
return markdownProse(held).replace(CODE, (_m, i) => code[Number(i)] ?? "");
|
|
287
|
+
}
|
|
288
|
+
function markdownToHtml(text, ctx) {
|
|
289
|
+
return mrkdwnToHtml(markdownToMrkdwn(text), ctx);
|
|
290
|
+
}
|
|
277
291
|
|
|
278
292
|
// src/render/blocks.ts
|
|
279
293
|
function rec(v) {
|
|
@@ -516,6 +530,8 @@ function renderBlock(v, ctx) {
|
|
|
516
530
|
return `<div class="sm-block sm-file-block">\uD83D\uDCCE ${escapeHtml(str(block.external_id) || str(block.file_id) || "file")}</div>`;
|
|
517
531
|
case "video":
|
|
518
532
|
return videoBlock(block, ctx);
|
|
533
|
+
case "markdown":
|
|
534
|
+
return `<div class="sm-block sm-markdown">${markdownToHtml(str(block.text), ctx)}</div>`;
|
|
519
535
|
default:
|
|
520
536
|
return `<div class="sm-block">${unsupported(str(block.type))}</div>`;
|
|
521
537
|
}
|
|
@@ -1104,15 +1120,15 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1104
1120
|
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
1105
1121
|
const interactive = !opts.screenshot && !flags.ephemeral && humanUsers(store).length > 0;
|
|
1106
1122
|
const body = [];
|
|
1107
|
-
if (m.streaming_state !== undefined) {
|
|
1123
|
+
if (m.streaming_state !== undefined && !m.stream_replaced) {
|
|
1108
1124
|
if (m.text)
|
|
1109
|
-
body.push(`<div class="sm-text">${
|
|
1125
|
+
body.push(`<div class="sm-text">${markdownToHtml(m.text, ctx)}</div>`);
|
|
1110
1126
|
if (blocks)
|
|
1111
1127
|
body.push(renderBlocks(blocks, ctx));
|
|
1112
1128
|
} else if (blocks)
|
|
1113
1129
|
body.push(renderBlocks(blocks, ctx));
|
|
1114
1130
|
else if (m.text)
|
|
1115
|
-
body.push(`<div class="sm-text">${mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1131
|
+
body.push(`<div class="sm-text">${m.text_format === "markdown" ? markdownToHtml(m.text, ctx) : mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1116
1132
|
if (m.edited)
|
|
1117
1133
|
body.push(`<div class="sm-edited">(edited)</div>`);
|
|
1118
1134
|
if (m.attachments?.length)
|
|
@@ -1132,9 +1148,6 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1132
1148
|
const open = flags.openTs && flags.openTs === m.ts ? " sm-msg-open" : "";
|
|
1133
1149
|
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>`;
|
|
1134
1150
|
}
|
|
1135
|
-
function markdownToMrkdwn(text) {
|
|
1136
|
-
return text.split(/(```[\s\S]*?```)/).map((part, i) => i % 2 === 1 ? part : part.replace(/\*\*(.+?)\*\*/g, "*$1*").replace(/__(.+?)__/g, "*$1*").replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, "<$2|$1>").replace(/^#{1,6}\s+(.+)$/gm, "*$1*").replace(/^(\s*)[-*]\s+/gm, "$1\u2022 ")).join("");
|
|
1137
|
-
}
|
|
1138
1151
|
function renderMessageList(store, ctx, messages, opts, flags = {}) {
|
|
1139
1152
|
const out = [];
|
|
1140
1153
|
let day = "";
|
|
@@ -1881,6 +1894,18 @@ class Store2 {
|
|
|
1881
1894
|
this.emit({ kind: "member.join", channel, user: userId, inviter });
|
|
1882
1895
|
return { alreadyMember: false };
|
|
1883
1896
|
}
|
|
1897
|
+
leave(channelId, userId) {
|
|
1898
|
+
const channel = this.channel(channelId);
|
|
1899
|
+
if (channel.is_archived)
|
|
1900
|
+
throw new SlackApiError2("is_archived");
|
|
1901
|
+
if (channel.is_general)
|
|
1902
|
+
throw new SlackApiError2("cant_leave_general");
|
|
1903
|
+
if (!channel.members.includes(userId))
|
|
1904
|
+
return { notInChannel: true };
|
|
1905
|
+
channel.members = channel.members.filter((m) => m !== userId);
|
|
1906
|
+
this.emit({ kind: "channel.update", channel });
|
|
1907
|
+
return { notInChannel: false };
|
|
1908
|
+
}
|
|
1884
1909
|
archive(channelId) {
|
|
1885
1910
|
const channel = this.channel(channelId);
|
|
1886
1911
|
if (channel.is_archived)
|
|
@@ -2124,7 +2149,7 @@ function guessMime(ext) {
|
|
|
2124
2149
|
return map[ext] ?? "application/octet-stream";
|
|
2125
2150
|
}
|
|
2126
2151
|
function wireMessage(m) {
|
|
2127
|
-
const { ephemeral_user: _e, ...rest } = m;
|
|
2152
|
+
const { ephemeral_user: _e, text_format: _f, stream_replaced: _r, ...rest } = m;
|
|
2128
2153
|
if (rest.files)
|
|
2129
2154
|
rest.files = rest.files.map(wireFile);
|
|
2130
2155
|
return rest;
|
|
@@ -2569,7 +2594,8 @@ function markdownText(args) {
|
|
|
2569
2594
|
return markdown;
|
|
2570
2595
|
}
|
|
2571
2596
|
function messageContent(args) {
|
|
2572
|
-
const
|
|
2597
|
+
const markdown = markdownText(args);
|
|
2598
|
+
const text = markdown ?? str2(args, "text") ?? "";
|
|
2573
2599
|
const blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2574
2600
|
const attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2575
2601
|
if (!text && !blocks?.length && !attachments?.length)
|
|
@@ -2579,7 +2605,13 @@ function messageContent(args) {
|
|
|
2579
2605
|
response_metadata: { messages: ["[ERROR] must have at most 50 blocks"] }
|
|
2580
2606
|
});
|
|
2581
2607
|
const metadata = args.metadata && typeof args.metadata === "object" ? args.metadata : undefined;
|
|
2582
|
-
return {
|
|
2608
|
+
return {
|
|
2609
|
+
text,
|
|
2610
|
+
blocks,
|
|
2611
|
+
attachments,
|
|
2612
|
+
metadata,
|
|
2613
|
+
...markdown !== undefined ? { text_format: "markdown" } : {}
|
|
2614
|
+
};
|
|
2583
2615
|
}
|
|
2584
2616
|
function channelInfo(store, c, actor) {
|
|
2585
2617
|
const { members, ...rest } = c;
|
|
@@ -2679,12 +2711,18 @@ var handlers = {
|
|
|
2679
2711
|
throw new SlackApiError2("cant_update_message");
|
|
2680
2712
|
const patch = {};
|
|
2681
2713
|
const markdown = markdownText(args);
|
|
2682
|
-
if (markdown !== undefined)
|
|
2714
|
+
if (markdown !== undefined) {
|
|
2683
2715
|
patch.text = markdown;
|
|
2684
|
-
|
|
2716
|
+
patch.text_format = "markdown";
|
|
2717
|
+
} else if (args.text !== undefined) {
|
|
2685
2718
|
patch.text = str2(args, "text") ?? "";
|
|
2686
|
-
|
|
2719
|
+
patch.text_format = undefined;
|
|
2720
|
+
}
|
|
2721
|
+
if (args.blocks !== undefined) {
|
|
2687
2722
|
patch.blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2723
|
+
if (existing.streaming_state !== undefined)
|
|
2724
|
+
patch.stream_replaced = true;
|
|
2725
|
+
}
|
|
2688
2726
|
if (args.attachments !== undefined)
|
|
2689
2727
|
patch.attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2690
2728
|
if (args.metadata !== undefined)
|
|
@@ -2735,8 +2773,10 @@ var handlers = {
|
|
|
2735
2773
|
streaming_state: "completed",
|
|
2736
2774
|
text: existing.text + streamText(args)
|
|
2737
2775
|
};
|
|
2738
|
-
if (Array.isArray(args.blocks))
|
|
2776
|
+
if (Array.isArray(args.blocks)) {
|
|
2739
2777
|
patch.blocks = args.blocks;
|
|
2778
|
+
patch.stream_replaced = undefined;
|
|
2779
|
+
}
|
|
2740
2780
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
|
|
2741
2781
|
markEdited: false
|
|
2742
2782
|
});
|
|
@@ -2819,6 +2859,10 @@ var handlers = {
|
|
|
2819
2859
|
...errors.length ? { errors } : {}
|
|
2820
2860
|
};
|
|
2821
2861
|
},
|
|
2862
|
+
"conversations.leave": (ctx, args) => {
|
|
2863
|
+
const { notInChannel } = ctx.store.leave(required(args, "channel"), ctx.actor.userId);
|
|
2864
|
+
return notInChannel ? { ok: false, not_in_channel: true } : {};
|
|
2865
|
+
},
|
|
2822
2866
|
"conversations.archive": (ctx, args) => {
|
|
2823
2867
|
ctx.store.archive(required(args, "channel"));
|
|
2824
2868
|
return {};
|
|
@@ -3023,6 +3067,17 @@ var handlers = {
|
|
|
3023
3067
|
});
|
|
3024
3068
|
return {};
|
|
3025
3069
|
},
|
|
3070
|
+
"agents.sessions.setStatus": (ctx, args) => {
|
|
3071
|
+
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
3072
|
+
const status = required(args, "status");
|
|
3073
|
+
if (!["active", "processing", "suspended", "closed"].includes(status))
|
|
3074
|
+
throw new SlackApiError2("invalid_status");
|
|
3075
|
+
const threadTs = str2(args, "thread_ts");
|
|
3076
|
+
if (!threadTs)
|
|
3077
|
+
throw new SlackApiError2("thread_ts_required");
|
|
3078
|
+
ctx.store.assistantThread(channel.id, threadTs).sessionStatus = status;
|
|
3079
|
+
return { status, agent_status: status };
|
|
3080
|
+
},
|
|
3026
3081
|
"assistant.threads.setTitle": (ctx, args) => {
|
|
3027
3082
|
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
3028
3083
|
const threadTs = required(args, "thread_ts");
|
|
@@ -3381,7 +3436,7 @@ class SlackMock2 {
|
|
|
3381
3436
|
};
|
|
3382
3437
|
try {
|
|
3383
3438
|
const body = handler(ctx, args);
|
|
3384
|
-
record(
|
|
3439
|
+
record(body.ok !== false);
|
|
3385
3440
|
return Response.json({ ok: true, ...body });
|
|
3386
3441
|
} catch (e) {
|
|
3387
3442
|
if (e instanceof SlackApiError2) {
|
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
|
@@ -80,8 +80,9 @@ function markdownText(args: Args): string | undefined {
|
|
|
80
80
|
|
|
81
81
|
function messageContent(
|
|
82
82
|
args: Args,
|
|
83
|
-
): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata"> {
|
|
84
|
-
const
|
|
83
|
+
): Pick<SlackMessage, "text" | "blocks" | "attachments" | "metadata" | "text_format"> {
|
|
84
|
+
const markdown = markdownText(args);
|
|
85
|
+
const text = markdown ?? str(args, "text") ?? "";
|
|
85
86
|
const blocks = Array.isArray(args.blocks) ? (args.blocks as unknown[]) : undefined;
|
|
86
87
|
const attachments = Array.isArray(args.attachments) ? (args.attachments as unknown[]) : undefined;
|
|
87
88
|
if (!text && !blocks?.length && !attachments?.length) throw new SlackApiError("no_text");
|
|
@@ -93,7 +94,13 @@ function messageContent(
|
|
|
93
94
|
args.metadata && typeof args.metadata === "object"
|
|
94
95
|
? (args.metadata as SlackMessage["metadata"])
|
|
95
96
|
: undefined;
|
|
96
|
-
return {
|
|
97
|
+
return {
|
|
98
|
+
text,
|
|
99
|
+
blocks,
|
|
100
|
+
attachments,
|
|
101
|
+
metadata,
|
|
102
|
+
...(markdown !== undefined ? { text_format: "markdown" } : {}),
|
|
103
|
+
};
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
function channelInfo(store: Store, c: SlackChannel, actor: Actor): Record<string, unknown> {
|
|
@@ -205,10 +212,18 @@ export const handlers: Record<string, Handler> = {
|
|
|
205
212
|
throw new SlackApiError("cant_update_message");
|
|
206
213
|
const patch: Partial<SlackMessage> = {};
|
|
207
214
|
const markdown = markdownText(args);
|
|
208
|
-
if (markdown !== undefined)
|
|
209
|
-
|
|
210
|
-
|
|
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) {
|
|
211
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
|
+
}
|
|
212
227
|
if (args.attachments !== undefined)
|
|
213
228
|
patch.attachments = Array.isArray(args.attachments)
|
|
214
229
|
? (args.attachments as unknown[])
|
|
@@ -278,7 +293,11 @@ export const handlers: Record<string, Handler> = {
|
|
|
278
293
|
streaming_state: "completed",
|
|
279
294
|
text: existing.text + streamText(args),
|
|
280
295
|
};
|
|
281
|
-
|
|
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
|
+
}
|
|
282
301
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
|
|
283
302
|
markEdited: false,
|
|
284
303
|
});
|
|
@@ -368,6 +387,12 @@ export const handlers: Record<string, Handler> = {
|
|
|
368
387
|
};
|
|
369
388
|
},
|
|
370
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
|
+
|
|
371
396
|
"conversations.archive": (ctx, args) => {
|
|
372
397
|
ctx.store.archive(required(args, "channel"));
|
|
373
398
|
return {};
|
|
@@ -610,6 +635,20 @@ export const handlers: Record<string, Handler> = {
|
|
|
610
635
|
return {};
|
|
611
636
|
},
|
|
612
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
|
+
|
|
613
652
|
"assistant.threads.setTitle": (ctx, args) => {
|
|
614
653
|
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
615
654
|
const threadTs = required(args, "thread_ts");
|