@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/README.md +42 -5
- package/dist/block-limits.d.ts +2 -0
- package/dist/cli.js +19 -2
- package/dist/http-delivery.d.ts +61 -0
- package/dist/{index-86bw4z6k.js → index-5hsxaq74.js} +543 -39
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -2
- package/dist/render/mrkdwn.d.ts +9 -0
- package/dist/server.d.ts +9 -0
- package/dist/socket-mode.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/block-limits.ts +170 -0
- package/src/cli.ts +21 -0
- package/src/http-delivery.ts +240 -0
- package/src/index.ts +7 -0
- package/src/render/blocks.ts +168 -1
- package/src/render/index.ts +30 -26
- package/src/render/mrkdwn.ts +39 -0
- package/src/server.ts +35 -15
- package/src/socket-mode.ts +1 -1
- package/src/store.ts +19 -2
- package/src/types.ts +4 -0
- package/src/web-api.ts +49 -11
|
@@ -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) {
|
|
@@ -410,7 +424,7 @@ function richElement(v, ctx) {
|
|
|
410
424
|
return `<span class="sm-pill">@${escapeHtml(str(el.range) || "channel")}</span>`;
|
|
411
425
|
case "emoji": {
|
|
412
426
|
const char = emojiChar(str(el.name)) ?? (el.unicode ? emojiFromUnicode(str(el.unicode)) : undefined);
|
|
413
|
-
return char
|
|
427
|
+
return char ? escapeHtml(char) : `<span class="sm-emoji-name">:${escapeHtml(str(el.name))}:</span>`;
|
|
414
428
|
}
|
|
415
429
|
case "date":
|
|
416
430
|
return plainTextToHtml(str(el.fallback) || str(el.timestamp));
|
|
@@ -491,6 +505,113 @@ function videoBlock(block, ctx) {
|
|
|
491
505
|
const inner = `${thumb ? `<img class="sm-video-thumb" src="${thumb}" alt="${escapeHtml(str(block.alt_text))}">` : ""}<div class="sm-video-title">\u25B6 ${title}</div>`;
|
|
492
506
|
return `<div class="sm-block sm-video">${url ? `<a class="sm-video-link" href="${url}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`;
|
|
493
507
|
}
|
|
508
|
+
function tableCell(v, ctx) {
|
|
509
|
+
const cell = rec(v);
|
|
510
|
+
if (!cell)
|
|
511
|
+
return "";
|
|
512
|
+
if (str(cell.type) === "rich_text")
|
|
513
|
+
return arr(cell.elements).map((e) => richSection(e, ctx)).join("");
|
|
514
|
+
if (str(cell.type) === "action_cell") {
|
|
515
|
+
const el = rec(cell.element);
|
|
516
|
+
return el && str(el.type) === "button" ? button(el, ctx) : tableCell(cell.fallback, ctx);
|
|
517
|
+
}
|
|
518
|
+
return plainTextToHtml(str(cell.text) || (cell.value === undefined ? "" : String(cell.value)));
|
|
519
|
+
}
|
|
520
|
+
function tableBlock(block, ctx) {
|
|
521
|
+
const settings = arr(block.column_settings).map(rec);
|
|
522
|
+
const rows = arr(block.rows).map(arr);
|
|
523
|
+
const ps = Number(block.page_size);
|
|
524
|
+
const pageSize = block.type === "data_table" ? Number.isInteger(ps) && ps >= 1 ? ps : 5 : 0;
|
|
525
|
+
const head = block.type === "data_table" ? 1 : 0;
|
|
526
|
+
const body = rows.slice(head);
|
|
527
|
+
const per = pageSize || body.length || 1;
|
|
528
|
+
const pages = [];
|
|
529
|
+
for (let i = 0;i < body.length || i === 0; i += per)
|
|
530
|
+
pages.push(body.slice(i, i + per));
|
|
531
|
+
const row = (cells, tag) => `<tr>${cells.map((c, i) => {
|
|
532
|
+
const align = str(settings[i]?.align);
|
|
533
|
+
const cls = [
|
|
534
|
+
align === "center" || align === "right" ? `sm-ta-${align}` : "",
|
|
535
|
+
settings[i]?.is_wrapped === true ? "sm-td-wrap" : ""
|
|
536
|
+
].filter(Boolean);
|
|
537
|
+
return `<${tag}${cls.length ? ` class="${cls.join(" ")}"` : ""}>${tableCell(c, ctx)}</${tag}>`;
|
|
538
|
+
}).join("")}</tr>`;
|
|
539
|
+
const caption = str(block.caption) ? `<div class="sm-table-caption">${plainTextToHtml(str(block.caption))}</div>` : "";
|
|
540
|
+
const paged = pages.length > 1;
|
|
541
|
+
const range = (p) => `${p * per + 1}\u2013${p * per + (pages[p]?.length ?? 0)} of ${body.length}`;
|
|
542
|
+
const pager = paged ? `<div class="sm-table-pager"><button type="button" class="sm-pager-btn" data-step="-1" aria-label="Previous page" disabled>\u2039</button><span class="sm-pager-label">${range(0)}</span><button type="button" class="sm-pager-btn" data-step="1" aria-label="Next page">\u203A</button></div>` : "";
|
|
543
|
+
const tbodies = pages.map((pg, p) => `<tbody${paged ? ` data-range="${range(p)}"` : ""}${p ? " hidden" : ""}>${pg.map((r) => row(r, "td")).join("")}</tbody>`).join("");
|
|
544
|
+
return `<div class="sm-block sm-table-block">${caption}<div class="sm-table-wrap"><table class="sm-bk-table">${head && rows.length ? `<thead>${row(rows[0] ?? [], "th")}</thead>` : ""}${tbodies}</table></div>${pager}</div>`;
|
|
545
|
+
}
|
|
546
|
+
var CHART_COLORS = ["#1264a3", "#2eb67d", "#ecb22e", "#e01e5a", "#36c5f0", "#7c3085"];
|
|
547
|
+
var color = (i) => CHART_COLORS[i % CHART_COLORS.length];
|
|
548
|
+
var num = (v) => Number.isFinite(Number(v)) ? Number(v) : 0;
|
|
549
|
+
var svgText = (x, y, text, attrs = "") => `<text x="${x}" y="${y}" ${attrs}>${escapeHtml(text)}</text>`;
|
|
550
|
+
function legend(labels) {
|
|
551
|
+
return `<div class="sm-chart-legend">${labels.map((l, i) => `<span class="sm-chart-key"><i style="background:${color(i)}"></i>${escapeHtml(l)}</span>`).join("")}</div>`;
|
|
552
|
+
}
|
|
553
|
+
function pieChart(chart) {
|
|
554
|
+
const segments = arr(chart.segments).map(rec);
|
|
555
|
+
const total = segments.reduce((t, s) => t + Math.max(0, num(s?.value)), 0) || 1;
|
|
556
|
+
let angle = -Math.PI / 2;
|
|
557
|
+
const slices = segments.map((s, i) => {
|
|
558
|
+
const frac = Math.max(0, num(s?.value)) / total;
|
|
559
|
+
if (frac >= 0.9999)
|
|
560
|
+
return `<circle cx="100" cy="100" r="90" fill="${color(i)}"/>`;
|
|
561
|
+
const a0 = angle;
|
|
562
|
+
angle += frac * 2 * Math.PI;
|
|
563
|
+
const p = (a) => `${100 + 90 * Math.cos(a)} ${100 + 90 * Math.sin(a)}`;
|
|
564
|
+
return `<path d="M100 100 L${p(a0)} A90 90 0 ${frac > 0.5 ? 1 : 0} 1 ${p(angle)} Z" fill="${color(i)}"/>`;
|
|
565
|
+
});
|
|
566
|
+
return `<svg class="sm-chart-svg" viewBox="0 0 200 200" width="200" height="200">${slices.join("")}</svg>${legend(segments.map((s) => `${str(s?.label)} (${num(s?.value)})`))}`;
|
|
567
|
+
}
|
|
568
|
+
function seriesChart(chart, type) {
|
|
569
|
+
const axis = rec(chart.axis_config) ?? {};
|
|
570
|
+
const series = arr(chart.series).map((s) => rec(s) ?? {});
|
|
571
|
+
const categories = arr(axis.categories).map(str);
|
|
572
|
+
const valueAt = (s, c) => num(rec(arr(s.data).find((p) => rec(p)?.label === c))?.value);
|
|
573
|
+
const values = series.flatMap((s) => categories.map((c) => valueAt(s, c)));
|
|
574
|
+
const min = Math.min(0, ...values);
|
|
575
|
+
const max = Math.max(min < 0 ? 0 : 1, ...values);
|
|
576
|
+
const [W, H, L, T, B] = [480, 220, 44, 10, 30];
|
|
577
|
+
const plotW = W - L - 10;
|
|
578
|
+
const plotH = H - T - B;
|
|
579
|
+
const y = (v) => T + plotH - (v - min) / (max - min) * plotH;
|
|
580
|
+
const zero = y(0);
|
|
581
|
+
const step = plotW / Math.max(1, categories.length);
|
|
582
|
+
const parts = [];
|
|
583
|
+
for (let k = 0;k <= 4; k++) {
|
|
584
|
+
const v = min + (max - min) * k / 4;
|
|
585
|
+
parts.push(`<line x1="${L}" x2="${W - 10}" y1="${y(v)}" y2="${y(v)}" stroke="#e8e8e8"/>`, svgText(L - 6, y(v) + 4, String(Math.round(v * 100) / 100), 'text-anchor="end"'));
|
|
586
|
+
}
|
|
587
|
+
categories.forEach((c, i) => {
|
|
588
|
+
parts.push(svgText(L + step * (i + 0.5), H - B + 16, c, 'text-anchor="middle"'));
|
|
589
|
+
});
|
|
590
|
+
series.forEach((s, si) => {
|
|
591
|
+
if (type === "bar") {
|
|
592
|
+
const bw = step * 0.7 / Math.max(1, series.length);
|
|
593
|
+
categories.forEach((c, i) => {
|
|
594
|
+
const v = valueAt(s, c);
|
|
595
|
+
parts.push(`<rect x="${L + step * i + step * 0.15 + bw * si}" y="${Math.min(zero, y(v))}" width="${bw}" height="${Math.abs(zero - y(v))}" fill="${color(si)}"/>`);
|
|
596
|
+
});
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const pts = categories.map((c, i) => `${L + step * (i + 0.5)},${y(valueAt(s, c))}`);
|
|
600
|
+
if (type === "area" && pts.length)
|
|
601
|
+
parts.push(`<polygon points="${L + step * 0.5},${zero} ${pts.join(" ")} ${L + step * (categories.length - 0.5)},${zero}" fill="${color(si)}" fill-opacity=".25"/>`);
|
|
602
|
+
parts.push(`<polyline points="${pts.join(" ")}" fill="none" stroke="${color(si)}" stroke-width="2"/>`);
|
|
603
|
+
});
|
|
604
|
+
const xLabel = str(axis.x_label);
|
|
605
|
+
const yLabel = str(axis.y_label);
|
|
606
|
+
const labels = `${yLabel ? `<div class="sm-chart-axis">${plainTextToHtml(yLabel)} \u2191</div>` : ""}`;
|
|
607
|
+
return `${labels}<svg class="sm-chart-svg" viewBox="0 0 ${W} ${H}" width="${W}" height="${H}">${parts.join("")}<line x1="${L}" x2="${W - 10}" y1="${zero}" y2="${zero}" stroke="#bbb"/></svg>${xLabel ? `<div class="sm-chart-axis sm-chart-axis-x">${plainTextToHtml(xLabel)} \u2192</div>` : ""}${legend(series.map((s) => str(s.name)))}`;
|
|
608
|
+
}
|
|
609
|
+
function dataVisualizationBlock(block) {
|
|
610
|
+
const chart = rec(block.chart) ?? {};
|
|
611
|
+
const type = str(chart.type);
|
|
612
|
+
const body = type === "pie" ? pieChart(chart) : type === "bar" || type === "line" || type === "area" ? seriesChart(chart, type) : unsupported(`chart ${type}`);
|
|
613
|
+
return `<div class="sm-block sm-chart" data-chart="${escapeHtml(type)}"><div class="sm-chart-title">${plainTextToHtml(str(block.title))}</div>${body}</div>`;
|
|
614
|
+
}
|
|
494
615
|
function renderBlock(v, ctx) {
|
|
495
616
|
const block = rec(v);
|
|
496
617
|
if (!block)
|
|
@@ -516,6 +637,13 @@ function renderBlock(v, ctx) {
|
|
|
516
637
|
return `<div class="sm-block sm-file-block">\uD83D\uDCCE ${escapeHtml(str(block.external_id) || str(block.file_id) || "file")}</div>`;
|
|
517
638
|
case "video":
|
|
518
639
|
return videoBlock(block, ctx);
|
|
640
|
+
case "markdown":
|
|
641
|
+
return `<div class="sm-block sm-markdown">${markdownToHtml(str(block.text), ctx)}</div>`;
|
|
642
|
+
case "table":
|
|
643
|
+
case "data_table":
|
|
644
|
+
return tableBlock(block, ctx);
|
|
645
|
+
case "data_visualization":
|
|
646
|
+
return dataVisualizationBlock(block);
|
|
519
647
|
default:
|
|
520
648
|
return `<div class="sm-block">${unsupported(str(block.type))}</div>`;
|
|
521
649
|
}
|
|
@@ -782,6 +910,25 @@ a:hover{text-decoration:underline}
|
|
|
782
910
|
.sm-video-title{font-size:14px;font-weight:700;margin-top:4px}
|
|
783
911
|
.sm-unsupported{display:inline-block;background:#f4f4f4;border:1px solid #ddd;border-radius:4px;padding:4px 8px;font-size:12px;color:#616061;font-family:Monaco,Menlo,monospace}
|
|
784
912
|
.sm-rt-section{margin:2px 0}
|
|
913
|
+
.sm-table-caption{font-size:13px;font-weight:700;color:#454245;margin-bottom:4px}
|
|
914
|
+
.sm-table-wrap{width:fit-content;overflow-x:auto;border:1px solid #ddd;border-radius:6px;max-width:100%}
|
|
915
|
+
.sm-bk-table{border-collapse:collapse;font-size:14px;line-height:20px}
|
|
916
|
+
.sm-bk-table th,.sm-bk-table td{padding:6px 12px;text-align:left;border-bottom:1px solid #e8e8e8;white-space:nowrap;vertical-align:top}
|
|
917
|
+
.sm-bk-table th{font-weight:700;background:#f8f8f8}
|
|
918
|
+
.sm-bk-table tr:last-child td{border-bottom:0}
|
|
919
|
+
.sm-bk-table .sm-ta-center{text-align:center}
|
|
920
|
+
.sm-bk-table .sm-ta-right{text-align:right}
|
|
921
|
+
.sm-bk-table .sm-td-wrap{white-space:normal}
|
|
922
|
+
.sm-table-pager{display:flex;align-items:center;gap:6px;font-size:12px;color:#616061;margin-top:4px}
|
|
923
|
+
.sm-pager-btn{border:1px solid #ddd;border-radius:4px;background:#fff;color:#1d1c1d;font-size:14px;line-height:16px;padding:0 6px;cursor:pointer}
|
|
924
|
+
.sm-pager-btn:disabled{color:#bbb;cursor:default}
|
|
925
|
+
.sm-chart{border:1px solid #ddd;border-radius:6px;padding:10px 12px;max-width:520px}
|
|
926
|
+
.sm-chart-title{font-size:14px;font-weight:700;margin-bottom:6px}
|
|
927
|
+
.sm-chart-svg{display:block;max-width:100%;height:auto;font-size:11px;fill:#616061}
|
|
928
|
+
.sm-chart-axis{font-size:11px;color:#616061}
|
|
929
|
+
.sm-chart-axis-x{text-align:center}
|
|
930
|
+
.sm-chart-legend{display:flex;flex-wrap:wrap;gap:4px 12px;font-size:12px;color:#454245;margin-top:6px}
|
|
931
|
+
.sm-chart-key i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:4px;vertical-align:-1px}
|
|
785
932
|
|
|
786
933
|
.sm-atts{margin-top:4px}
|
|
787
934
|
.sm-att{border-left:4px solid #ddd;border-radius:2px;padding:2px 0 2px 12px;margin:6px 0;max-width:600px}
|
|
@@ -1104,15 +1251,15 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1104
1251
|
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
1105
1252
|
const interactive = !opts.screenshot && !flags.ephemeral && humanUsers(store).length > 0;
|
|
1106
1253
|
const body = [];
|
|
1107
|
-
if (m.streaming_state !== undefined) {
|
|
1254
|
+
if (m.streaming_state !== undefined && !m.stream_replaced) {
|
|
1108
1255
|
if (m.text)
|
|
1109
|
-
body.push(`<div class="sm-text">${
|
|
1256
|
+
body.push(`<div class="sm-text">${markdownToHtml(m.text, ctx)}</div>`);
|
|
1110
1257
|
if (blocks)
|
|
1111
1258
|
body.push(renderBlocks(blocks, ctx));
|
|
1112
1259
|
} else if (blocks)
|
|
1113
1260
|
body.push(renderBlocks(blocks, ctx));
|
|
1114
1261
|
else if (m.text)
|
|
1115
|
-
body.push(`<div class="sm-text">${mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1262
|
+
body.push(`<div class="sm-text">${m.text_format === "markdown" ? markdownToHtml(m.text, ctx) : mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1116
1263
|
if (m.edited)
|
|
1117
1264
|
body.push(`<div class="sm-edited">(edited)</div>`);
|
|
1118
1265
|
if (m.attachments?.length)
|
|
@@ -1132,9 +1279,6 @@ function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
|
1132
1279
|
const open = flags.openTs && flags.openTs === m.ts ? " sm-msg-open" : "";
|
|
1133
1280
|
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
1281
|
}
|
|
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
1282
|
function renderMessageList(store, ctx, messages, opts, flags = {}) {
|
|
1139
1283
|
const out = [];
|
|
1140
1284
|
let day = "";
|
|
@@ -1271,6 +1415,7 @@ function fitViewport(){if(viewport&&viewport.scale!==1)return;var style=document
|
|
|
1271
1415
|
fitViewport();addEventListener("resize",fitViewport);
|
|
1272
1416
|
if(viewport){viewport.addEventListener("resize",fitViewport);viewport.addEventListener("scroll",fitViewport)}
|
|
1273
1417
|
document.addEventListener("change",function(e){if(e.target.classList.contains("sm-channel-switch"))location.href=e.target.value});
|
|
1418
|
+
document.addEventListener("click",function(e){var b=e.target.closest(".sm-pager-btn");if(!b)return;var block=b.closest(".sm-table-block"),pages=block.querySelectorAll("tbody"),cur=0;pages.forEach(function(t,i){if(!t.hidden)cur=i});var next=Math.min(Math.max(cur+Number(b.dataset.step),0),pages.length-1);pages.forEach(function(t,i){t.hidden=i!==next});block.querySelector(".sm-pager-label").textContent=pages[next].dataset.range;var btns=block.querySelectorAll(".sm-pager-btn");btns[0].disabled=next===0;btns[1].disabled=next===pages.length-1});
|
|
1274
1419
|
var W="sm-panel-w",DEF=${JSON.stringify(panelWidth(opts))},FIXED=${opts.panelWidth ? "true" : "false"};
|
|
1275
1420
|
${autoScroll ? `function scroll(){document.querySelectorAll(".sm-scroll").forEach(function(el){el.scrollTop=el.scrollHeight})}
|
|
1276
1421
|
scroll();addEventListener("load",scroll);` : ""}
|
|
@@ -1573,7 +1718,7 @@ function str2(args, key) {
|
|
|
1573
1718
|
const v = args[key];
|
|
1574
1719
|
return typeof v === "string" ? v : v == null ? undefined : String(v);
|
|
1575
1720
|
}
|
|
1576
|
-
function
|
|
1721
|
+
function num2(args, key, fallback) {
|
|
1577
1722
|
const v = args[key];
|
|
1578
1723
|
const n = typeof v === "number" ? v : typeof v === "string" ? Number.parseInt(v, 10) : Number.NaN;
|
|
1579
1724
|
return Number.isFinite(n) ? n : fallback;
|
|
@@ -1881,6 +2026,18 @@ class Store2 {
|
|
|
1881
2026
|
this.emit({ kind: "member.join", channel, user: userId, inviter });
|
|
1882
2027
|
return { alreadyMember: false };
|
|
1883
2028
|
}
|
|
2029
|
+
leave(channelId, userId) {
|
|
2030
|
+
const channel = this.channel(channelId);
|
|
2031
|
+
if (channel.is_archived)
|
|
2032
|
+
throw new SlackApiError2("is_archived");
|
|
2033
|
+
if (channel.is_general)
|
|
2034
|
+
throw new SlackApiError2("cant_leave_general");
|
|
2035
|
+
if (!channel.members.includes(userId))
|
|
2036
|
+
return { notInChannel: true };
|
|
2037
|
+
channel.members = channel.members.filter((m) => m !== userId);
|
|
2038
|
+
this.emit({ kind: "channel.update", channel });
|
|
2039
|
+
return { notInChannel: false };
|
|
2040
|
+
}
|
|
1884
2041
|
archive(channelId) {
|
|
1885
2042
|
const channel = this.channel(channelId);
|
|
1886
2043
|
if (channel.is_archived)
|
|
@@ -2124,7 +2281,7 @@ function guessMime(ext) {
|
|
|
2124
2281
|
return map[ext] ?? "application/octet-stream";
|
|
2125
2282
|
}
|
|
2126
2283
|
function wireMessage(m) {
|
|
2127
|
-
const { ephemeral_user: _e, ...rest } = m;
|
|
2284
|
+
const { ephemeral_user: _e, text_format: _f, stream_replaced: _r, ...rest } = m;
|
|
2128
2285
|
if (rest.files)
|
|
2129
2286
|
rest.files = rest.files.map(wireFile);
|
|
2130
2287
|
return rest;
|
|
@@ -2347,6 +2504,9 @@ function viewSubmissionPayload(store, input) {
|
|
|
2347
2504
|
};
|
|
2348
2505
|
}
|
|
2349
2506
|
|
|
2507
|
+
// src/http-delivery.ts
|
|
2508
|
+
import { createHmac } from "crypto";
|
|
2509
|
+
|
|
2350
2510
|
// src/socket-mode.ts
|
|
2351
2511
|
class SocketModeClosedError extends Error {
|
|
2352
2512
|
}
|
|
@@ -2521,6 +2681,318 @@ class SocketModeHub {
|
|
|
2521
2681
|
}
|
|
2522
2682
|
}
|
|
2523
2683
|
|
|
2684
|
+
// src/http-delivery.ts
|
|
2685
|
+
function slackSignature2(secret, timestamp, body) {
|
|
2686
|
+
return `v0=${createHmac("sha256", secret).update(`v0:${timestamp}:${body}`).digest("hex")}`;
|
|
2687
|
+
}
|
|
2688
|
+
|
|
2689
|
+
class HttpDeliveryError2 extends Error {
|
|
2690
|
+
}
|
|
2691
|
+
|
|
2692
|
+
class HttpDeliveryStoppedError2 extends HttpDeliveryError2 {
|
|
2693
|
+
constructor() {
|
|
2694
|
+
super("mock stopped");
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
class HttpDelivery2 {
|
|
2699
|
+
history = [];
|
|
2700
|
+
opts;
|
|
2701
|
+
stopController = new AbortController;
|
|
2702
|
+
retryTimers = new Set;
|
|
2703
|
+
constructor(opts) {
|
|
2704
|
+
this.opts = {
|
|
2705
|
+
eventsUrl: opts.eventsUrl,
|
|
2706
|
+
interactivityUrl: opts.interactivityUrl ?? opts.eventsUrl,
|
|
2707
|
+
commandsUrl: opts.commandsUrl ?? opts.eventsUrl,
|
|
2708
|
+
signingSecret: opts.signingSecret,
|
|
2709
|
+
retryDelaysMs: opts.retryDelaysMs ?? [60000, 300000, 1800000],
|
|
2710
|
+
timeoutMs: opts.timeoutMs ?? 3000,
|
|
2711
|
+
log: opts.log ?? (() => {})
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2714
|
+
shutdown() {
|
|
2715
|
+
this.stopController.abort();
|
|
2716
|
+
for (const t of this.retryTimers) {
|
|
2717
|
+
clearTimeout(t.timer);
|
|
2718
|
+
t.wake();
|
|
2719
|
+
}
|
|
2720
|
+
this.retryTimers.clear();
|
|
2721
|
+
}
|
|
2722
|
+
get stopped() {
|
|
2723
|
+
return this.stopController.signal.aborted;
|
|
2724
|
+
}
|
|
2725
|
+
waitToRetry(ms) {
|
|
2726
|
+
return new Promise((resolve) => {
|
|
2727
|
+
const entry = {
|
|
2728
|
+
timer: setTimeout(() => {
|
|
2729
|
+
this.retryTimers.delete(entry);
|
|
2730
|
+
resolve();
|
|
2731
|
+
}, ms),
|
|
2732
|
+
wake: resolve
|
|
2733
|
+
};
|
|
2734
|
+
this.retryTimers.add(entry);
|
|
2735
|
+
});
|
|
2736
|
+
}
|
|
2737
|
+
get connectionCount() {
|
|
2738
|
+
return 1;
|
|
2739
|
+
}
|
|
2740
|
+
waitForConnection() {
|
|
2741
|
+
return Promise.resolve();
|
|
2742
|
+
}
|
|
2743
|
+
async verifyUrl() {
|
|
2744
|
+
const challenge = crypto.randomUUID();
|
|
2745
|
+
const body = JSON.stringify({
|
|
2746
|
+
token: "mock-verification-token",
|
|
2747
|
+
challenge,
|
|
2748
|
+
type: "url_verification"
|
|
2749
|
+
});
|
|
2750
|
+
const res = await this.post(this.opts.eventsUrl, body, "application/json", {});
|
|
2751
|
+
if (!res.ok)
|
|
2752
|
+
return false;
|
|
2753
|
+
return res.payload?.challenge === challenge || res.text === challenge;
|
|
2754
|
+
}
|
|
2755
|
+
async send(type, payload, _acceptsResponsePayload = false) {
|
|
2756
|
+
if (this.stopped)
|
|
2757
|
+
throw new HttpDeliveryStoppedError2;
|
|
2758
|
+
const record = {
|
|
2759
|
+
envelope_id: crypto.randomUUID(),
|
|
2760
|
+
type,
|
|
2761
|
+
name: envelopeName(type, payload),
|
|
2762
|
+
sentAt: new Date().toISOString(),
|
|
2763
|
+
attempts: 0,
|
|
2764
|
+
acked: false
|
|
2765
|
+
};
|
|
2766
|
+
this.history.push(record);
|
|
2767
|
+
const { url, body, contentType } = this.encode(type, payload);
|
|
2768
|
+
const delays = type === "events_api" ? this.opts.retryDelaysMs : [];
|
|
2769
|
+
let retry;
|
|
2770
|
+
for (let attempt = 0;; attempt++) {
|
|
2771
|
+
record.attempts = attempt + 1;
|
|
2772
|
+
const res = await this.post(url, body, contentType, retry ? this.retryHeaders(retry) : {});
|
|
2773
|
+
if (this.stopped)
|
|
2774
|
+
throw new HttpDeliveryStoppedError2;
|
|
2775
|
+
if (res.ok) {
|
|
2776
|
+
record.acked = true;
|
|
2777
|
+
record.ackPayload = res.payload;
|
|
2778
|
+
return { envelope_id: record.envelope_id, payload: res.payload, attempts: record.attempts };
|
|
2779
|
+
}
|
|
2780
|
+
const delay = delays[attempt];
|
|
2781
|
+
if (delay === undefined) {
|
|
2782
|
+
throw new HttpDeliveryError2(`${record.name} not acknowledged (${res.reason}) after ${record.attempts} attempt(s)`);
|
|
2783
|
+
}
|
|
2784
|
+
this.opts.log(`${record.name} to ${url}: ${res.reason}; retry ${attempt + 1} in ${delay}ms`);
|
|
2785
|
+
await this.waitToRetry(delay);
|
|
2786
|
+
if (this.stopped)
|
|
2787
|
+
throw new HttpDeliveryStoppedError2;
|
|
2788
|
+
retry = { num: attempt + 1, reason: res.reason };
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
retryHeaders(retry) {
|
|
2792
|
+
return { "X-Slack-Retry-Num": String(retry.num), "X-Slack-Retry-Reason": retry.reason };
|
|
2793
|
+
}
|
|
2794
|
+
encode(type, payload) {
|
|
2795
|
+
if (type === "events_api") {
|
|
2796
|
+
return {
|
|
2797
|
+
url: this.opts.eventsUrl,
|
|
2798
|
+
body: JSON.stringify(payload),
|
|
2799
|
+
contentType: "application/json"
|
|
2800
|
+
};
|
|
2801
|
+
}
|
|
2802
|
+
const form = new URLSearchParams;
|
|
2803
|
+
if (type === "interactive") {
|
|
2804
|
+
form.set("payload", JSON.stringify(payload));
|
|
2805
|
+
} else {
|
|
2806
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
2807
|
+
if (value !== undefined && value !== null)
|
|
2808
|
+
form.set(key, String(value));
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
return {
|
|
2812
|
+
url: type === "interactive" ? this.opts.interactivityUrl : this.opts.commandsUrl,
|
|
2813
|
+
body: form.toString(),
|
|
2814
|
+
contentType: "application/x-www-form-urlencoded"
|
|
2815
|
+
};
|
|
2816
|
+
}
|
|
2817
|
+
async post(url, body, contentType, extraHeaders) {
|
|
2818
|
+
if (this.stopped)
|
|
2819
|
+
return { ok: false, reason: "stopped" };
|
|
2820
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
2821
|
+
try {
|
|
2822
|
+
const res = await fetch(url, {
|
|
2823
|
+
method: "POST",
|
|
2824
|
+
headers: {
|
|
2825
|
+
"Content-Type": contentType,
|
|
2826
|
+
"X-Slack-Request-Timestamp": String(timestamp),
|
|
2827
|
+
"X-Slack-Signature": slackSignature2(this.opts.signingSecret, timestamp, body),
|
|
2828
|
+
"User-Agent": "Slackbot 1.0 (+https://api.slack.com/robots)",
|
|
2829
|
+
...extraHeaders
|
|
2830
|
+
},
|
|
2831
|
+
body,
|
|
2832
|
+
signal: AbortSignal.any([
|
|
2833
|
+
AbortSignal.timeout(this.opts.timeoutMs),
|
|
2834
|
+
this.stopController.signal
|
|
2835
|
+
])
|
|
2836
|
+
});
|
|
2837
|
+
if (!res.ok) {
|
|
2838
|
+
await res.body?.cancel();
|
|
2839
|
+
return { ok: false, reason: "http_error" };
|
|
2840
|
+
}
|
|
2841
|
+
const text = await res.text();
|
|
2842
|
+
let payload;
|
|
2843
|
+
try {
|
|
2844
|
+
payload = text ? JSON.parse(text) : undefined;
|
|
2845
|
+
} catch {
|
|
2846
|
+
payload = undefined;
|
|
2847
|
+
}
|
|
2848
|
+
return { ok: true, payload, text };
|
|
2849
|
+
} catch (e) {
|
|
2850
|
+
const timedOut = e instanceof Error && (e.name === "TimeoutError" || e.name === "AbortError");
|
|
2851
|
+
return { ok: false, reason: timedOut ? "http_timeout" : "connection_failed" };
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
// src/block-limits.ts
|
|
2857
|
+
var rec2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v) ? v : undefined;
|
|
2858
|
+
var arr2 = (v) => Array.isArray(v) ? v : [];
|
|
2859
|
+
var str3 = (v) => typeof v === "string" ? v : "";
|
|
2860
|
+
function cellChars(v) {
|
|
2861
|
+
const cell = rec2(v);
|
|
2862
|
+
if (!cell)
|
|
2863
|
+
return 0;
|
|
2864
|
+
if (cell.type === "action_cell")
|
|
2865
|
+
return str3(rec2(rec2(cell.element)?.text)?.text).length;
|
|
2866
|
+
if (typeof cell.text === "string")
|
|
2867
|
+
return cell.text.length;
|
|
2868
|
+
return arr2(cell.elements).reduce((n, e) => n + cellChars(e), 0);
|
|
2869
|
+
}
|
|
2870
|
+
function tableIssues(b, at, data) {
|
|
2871
|
+
const out = [];
|
|
2872
|
+
const rows = arr2(b.rows);
|
|
2873
|
+
const [minRows, maxRows, maxChars] = data ? [2, 201, 20000] : [1, 100, 1e4];
|
|
2874
|
+
if (rows.length < minRows || rows.length > maxRows)
|
|
2875
|
+
out.push(`rows must have between ${minRows} and ${maxRows} items [json-pointer:${at}/rows]`);
|
|
2876
|
+
const cellTypes = data ? ["raw_text", "raw_number", "rich_text", "action_cell"] : ["raw_text", "raw_number", "rich_text"];
|
|
2877
|
+
let chars = 0;
|
|
2878
|
+
rows.forEach((r, i) => {
|
|
2879
|
+
const cells = arr2(r);
|
|
2880
|
+
if (!Array.isArray(r) || cells.length < 1 || cells.length > 20)
|
|
2881
|
+
out.push(`row must have between 1 and 20 cells [json-pointer:${at}/rows/${i}]`);
|
|
2882
|
+
if (data && cells.length !== arr2(rows[0]).length)
|
|
2883
|
+
out.push(`all rows must have the same number of cells [json-pointer:${at}/rows/${i}]`);
|
|
2884
|
+
cells.forEach((c, j) => {
|
|
2885
|
+
const type = str3(rec2(c)?.type);
|
|
2886
|
+
if (!cellTypes.includes(type) || data && i === 0 && type === "rich_text")
|
|
2887
|
+
out.push(`invalid cell type "${type}" [json-pointer:${at}/rows/${i}/${j}]`);
|
|
2888
|
+
else if (type === "action_cell" && str3(rec2(rec2(c)?.element)?.type) !== "button")
|
|
2889
|
+
out.push(`action_cell element must be a button [json-pointer:${at}/rows/${i}/${j}/element]`);
|
|
2890
|
+
chars += cellChars(c);
|
|
2891
|
+
});
|
|
2892
|
+
});
|
|
2893
|
+
if (chars > maxChars)
|
|
2894
|
+
out.push(`cells must have at most ${maxChars} characters in total [json-pointer:${at}/rows]`);
|
|
2895
|
+
if (data) {
|
|
2896
|
+
if (!str3(b.caption))
|
|
2897
|
+
out.push(`caption is required [json-pointer:${at}/caption]`);
|
|
2898
|
+
const page = b.page_size;
|
|
2899
|
+
if (page !== undefined && !(Number.isInteger(page) && page >= 1 && page <= 100))
|
|
2900
|
+
out.push(`page_size must be an integer between 1 and 100 [json-pointer:${at}/page_size]`);
|
|
2901
|
+
} else if (arr2(b.column_settings).length > 20) {
|
|
2902
|
+
out.push(`column_settings must have at most 20 items [json-pointer:${at}/column_settings]`);
|
|
2903
|
+
}
|
|
2904
|
+
return { out, chars };
|
|
2905
|
+
}
|
|
2906
|
+
var label = (v) => str3(v).length >= 1 && str3(v).length <= 20;
|
|
2907
|
+
function chartIssues(b, at) {
|
|
2908
|
+
const out = [];
|
|
2909
|
+
const title = str3(b.title);
|
|
2910
|
+
if (title.length < 1 || title.length > 50)
|
|
2911
|
+
out.push(`title must have between 1 and 50 characters [json-pointer:${at}/title]`);
|
|
2912
|
+
const chart = rec2(b.chart);
|
|
2913
|
+
const type = str3(chart?.type);
|
|
2914
|
+
if (type === "pie") {
|
|
2915
|
+
const segments = arr2(chart?.segments);
|
|
2916
|
+
if (segments.length < 1 || segments.length > 12)
|
|
2917
|
+
out.push(`segments must have between 1 and 12 items [json-pointer:${at}/chart/segments]`);
|
|
2918
|
+
segments.forEach((s, i) => {
|
|
2919
|
+
const seg = rec2(s);
|
|
2920
|
+
if (!label(seg?.label) || !(typeof seg?.value === "number" && seg.value > 0))
|
|
2921
|
+
out.push(`segment needs a 1-20 character label and a value above 0 [json-pointer:${at}/chart/segments/${i}]`);
|
|
2922
|
+
});
|
|
2923
|
+
return out;
|
|
2924
|
+
}
|
|
2925
|
+
if (!["bar", "line", "area"].includes(type))
|
|
2926
|
+
return [...out, `chart type must be pie, bar, line or area [json-pointer:${at}/chart/type]`];
|
|
2927
|
+
const axis = rec2(chart?.axis_config);
|
|
2928
|
+
const categories = arr2(axis?.categories).map(str3);
|
|
2929
|
+
if (!categories.length)
|
|
2930
|
+
out.push(`axis_config.categories is required [json-pointer:${at}/chart/axis_config]`);
|
|
2931
|
+
categories.forEach((c, i) => {
|
|
2932
|
+
if (c.length > 20)
|
|
2933
|
+
out.push(`category must have at most 20 characters [json-pointer:${at}/chart/axis_config/categories/${i}]`);
|
|
2934
|
+
});
|
|
2935
|
+
for (const k of ["x_label", "y_label"])
|
|
2936
|
+
if (str3(axis?.[k]).length > 50)
|
|
2937
|
+
out.push(`${k} must have at most 50 characters [json-pointer:${at}/chart/axis_config/${k}]`);
|
|
2938
|
+
const series = arr2(chart?.series);
|
|
2939
|
+
if (series.length < 1 || series.length > 12)
|
|
2940
|
+
out.push(`series must have between 1 and 12 items [json-pointer:${at}/chart/series]`);
|
|
2941
|
+
const names = new Set;
|
|
2942
|
+
series.forEach((s, i) => {
|
|
2943
|
+
const sr = rec2(s);
|
|
2944
|
+
const name = str3(sr?.name);
|
|
2945
|
+
if (!label(name) || names.has(name))
|
|
2946
|
+
out.push(`series name must be unique and 1-20 characters [json-pointer:${at}/chart/series/${i}/name]`);
|
|
2947
|
+
names.add(name);
|
|
2948
|
+
const data = arr2(sr?.data).map((p) => rec2(p));
|
|
2949
|
+
const labels = data.map((p) => str3(p?.label));
|
|
2950
|
+
if (data.length < 1 || data.length > 20)
|
|
2951
|
+
out.push(`data must have between 1 and 20 items [json-pointer:${at}/chart/series/${i}/data]`);
|
|
2952
|
+
if (data.some((p) => !label(p?.label) || typeof p?.value !== "number"))
|
|
2953
|
+
out.push(`data points need a 1-20 character label and a number value [json-pointer:${at}/chart/series/${i}/data]`);
|
|
2954
|
+
const count = (c) => labels.filter((l) => l === c).length;
|
|
2955
|
+
if (labels.some((l) => !categories.includes(l)) || categories.some((c) => count(c) !== 1))
|
|
2956
|
+
out.push(`data labels must match axis_config.categories [json-pointer:${at}/chart/series/${i}/data]`);
|
|
2957
|
+
});
|
|
2958
|
+
return out;
|
|
2959
|
+
}
|
|
2960
|
+
function validateBlocks(blocks) {
|
|
2961
|
+
if (!blocks)
|
|
2962
|
+
return;
|
|
2963
|
+
const issues = [];
|
|
2964
|
+
if (blocks.length > 50)
|
|
2965
|
+
issues.push("must have at most 50 blocks");
|
|
2966
|
+
let charts = 0;
|
|
2967
|
+
const budget = { table: 0, data_table: 0 };
|
|
2968
|
+
blocks.forEach((v, i) => {
|
|
2969
|
+
const b = rec2(v);
|
|
2970
|
+
const at = `/blocks/${i}`;
|
|
2971
|
+
switch (str3(b?.type)) {
|
|
2972
|
+
case "table":
|
|
2973
|
+
case "data_table": {
|
|
2974
|
+
const t = tableIssues(b, at, b?.type === "data_table");
|
|
2975
|
+
issues.push(...t.out);
|
|
2976
|
+
budget[b?.type] += t.chars;
|
|
2977
|
+
break;
|
|
2978
|
+
}
|
|
2979
|
+
case "data_visualization":
|
|
2980
|
+
if (++charts > 2)
|
|
2981
|
+
issues.push(`at most 2 data_visualization blocks per message [json-pointer:${at}]`);
|
|
2982
|
+
issues.push(...chartIssues(b, at));
|
|
2983
|
+
break;
|
|
2984
|
+
}
|
|
2985
|
+
});
|
|
2986
|
+
if (budget.table > 1e4)
|
|
2987
|
+
issues.push("table cells must have at most 10000 characters in total per message");
|
|
2988
|
+
if (budget.data_table > 20000)
|
|
2989
|
+
issues.push("data_table cells must have at most 20000 characters in total per message");
|
|
2990
|
+
if (issues.length)
|
|
2991
|
+
throw new SlackApiError2("invalid_blocks", {
|
|
2992
|
+
response_metadata: { messages: issues.map((m) => `[ERROR] ${m}`) }
|
|
2993
|
+
});
|
|
2994
|
+
}
|
|
2995
|
+
|
|
2524
2996
|
// src/web-api.ts
|
|
2525
2997
|
function required(args, key) {
|
|
2526
2998
|
const v = str2(args, key);
|
|
@@ -2569,17 +3041,21 @@ function markdownText(args) {
|
|
|
2569
3041
|
return markdown;
|
|
2570
3042
|
}
|
|
2571
3043
|
function messageContent(args) {
|
|
2572
|
-
const
|
|
3044
|
+
const markdown = markdownText(args);
|
|
3045
|
+
const text = markdown ?? str2(args, "text") ?? "";
|
|
2573
3046
|
const blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2574
3047
|
const attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2575
3048
|
if (!text && !blocks?.length && !attachments?.length)
|
|
2576
3049
|
throw new SlackApiError2("no_text");
|
|
2577
|
-
|
|
2578
|
-
throw new SlackApiError2("invalid_blocks", {
|
|
2579
|
-
response_metadata: { messages: ["[ERROR] must have at most 50 blocks"] }
|
|
2580
|
-
});
|
|
3050
|
+
validateBlocks(blocks);
|
|
2581
3051
|
const metadata = args.metadata && typeof args.metadata === "object" ? args.metadata : undefined;
|
|
2582
|
-
return {
|
|
3052
|
+
return {
|
|
3053
|
+
text,
|
|
3054
|
+
blocks,
|
|
3055
|
+
attachments,
|
|
3056
|
+
metadata,
|
|
3057
|
+
...markdown !== undefined ? { text_format: "markdown" } : {}
|
|
3058
|
+
};
|
|
2583
3059
|
}
|
|
2584
3060
|
function channelInfo(store, c, actor) {
|
|
2585
3061
|
const { members, ...rest } = c;
|
|
@@ -2679,12 +3155,19 @@ var handlers = {
|
|
|
2679
3155
|
throw new SlackApiError2("cant_update_message");
|
|
2680
3156
|
const patch = {};
|
|
2681
3157
|
const markdown = markdownText(args);
|
|
2682
|
-
if (markdown !== undefined)
|
|
3158
|
+
if (markdown !== undefined) {
|
|
2683
3159
|
patch.text = markdown;
|
|
2684
|
-
|
|
3160
|
+
patch.text_format = "markdown";
|
|
3161
|
+
} else if (args.text !== undefined) {
|
|
2685
3162
|
patch.text = str2(args, "text") ?? "";
|
|
2686
|
-
|
|
3163
|
+
patch.text_format = undefined;
|
|
3164
|
+
}
|
|
3165
|
+
if (args.blocks !== undefined) {
|
|
2687
3166
|
patch.blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
3167
|
+
validateBlocks(patch.blocks);
|
|
3168
|
+
if (existing.streaming_state !== undefined)
|
|
3169
|
+
patch.stream_replaced = true;
|
|
3170
|
+
}
|
|
2688
3171
|
if (args.attachments !== undefined)
|
|
2689
3172
|
patch.attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2690
3173
|
if (args.metadata !== undefined)
|
|
@@ -2735,8 +3218,10 @@ var handlers = {
|
|
|
2735
3218
|
streaming_state: "completed",
|
|
2736
3219
|
text: existing.text + streamText(args)
|
|
2737
3220
|
};
|
|
2738
|
-
if (Array.isArray(args.blocks))
|
|
3221
|
+
if (Array.isArray(args.blocks)) {
|
|
2739
3222
|
patch.blocks = args.blocks;
|
|
3223
|
+
patch.stream_replaced = undefined;
|
|
3224
|
+
}
|
|
2740
3225
|
const message = ctx.store.updateMessage(channel.id, ts, patch, ctx.actor.userId, {
|
|
2741
3226
|
markEdited: false
|
|
2742
3227
|
});
|
|
@@ -2765,7 +3250,7 @@ var handlers = {
|
|
|
2765
3250
|
return types.has("private_channel");
|
|
2766
3251
|
return types.has("public_channel");
|
|
2767
3252
|
});
|
|
2768
|
-
const limit =
|
|
3253
|
+
const limit = num2(args, "limit", 100);
|
|
2769
3254
|
const start = cursorOffset(str2(args, "cursor"));
|
|
2770
3255
|
const slice = all.slice(start, start + limit);
|
|
2771
3256
|
const hasMore = start + limit < all.length;
|
|
@@ -2819,6 +3304,10 @@ var handlers = {
|
|
|
2819
3304
|
...errors.length ? { errors } : {}
|
|
2820
3305
|
};
|
|
2821
3306
|
},
|
|
3307
|
+
"conversations.leave": (ctx, args) => {
|
|
3308
|
+
const { notInChannel } = ctx.store.leave(required(args, "channel"), ctx.actor.userId);
|
|
3309
|
+
return notInChannel ? { ok: false, not_in_channel: true } : {};
|
|
3310
|
+
},
|
|
2822
3311
|
"conversations.archive": (ctx, args) => {
|
|
2823
3312
|
ctx.store.archive(required(args, "channel"));
|
|
2824
3313
|
return {};
|
|
@@ -2841,7 +3330,7 @@ var handlers = {
|
|
|
2841
3330
|
oldest: str2(args, "oldest"),
|
|
2842
3331
|
latest: str2(args, "latest"),
|
|
2843
3332
|
inclusive: bool(args, "inclusive"),
|
|
2844
|
-
limit:
|
|
3333
|
+
limit: num2(args, "limit", 100),
|
|
2845
3334
|
cursor: str2(args, "cursor")
|
|
2846
3335
|
});
|
|
2847
3336
|
return {
|
|
@@ -2861,7 +3350,7 @@ var handlers = {
|
|
|
2861
3350
|
oldest: str2(args, "oldest"),
|
|
2862
3351
|
latest: str2(args, "latest"),
|
|
2863
3352
|
inclusive: bool(args, "inclusive"),
|
|
2864
|
-
limit:
|
|
3353
|
+
limit: num2(args, "limit", 1000),
|
|
2865
3354
|
cursor: str2(args, "cursor")
|
|
2866
3355
|
});
|
|
2867
3356
|
return {
|
|
@@ -2910,7 +3399,7 @@ var handlers = {
|
|
|
2910
3399
|
},
|
|
2911
3400
|
"files.getUploadURLExternal": (ctx, args) => {
|
|
2912
3401
|
const filename = required(args, "filename");
|
|
2913
|
-
const length =
|
|
3402
|
+
const length = num2(args, "length", -1);
|
|
2914
3403
|
if (length < 0)
|
|
2915
3404
|
throw new SlackApiError2("invalid_arguments", {
|
|
2916
3405
|
response_metadata: { messages: ["[ERROR] missing required field: length"] }
|
|
@@ -3023,6 +3512,17 @@ var handlers = {
|
|
|
3023
3512
|
});
|
|
3024
3513
|
return {};
|
|
3025
3514
|
},
|
|
3515
|
+
"agents.sessions.setStatus": (ctx, args) => {
|
|
3516
|
+
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
3517
|
+
const status = required(args, "status");
|
|
3518
|
+
if (!["active", "processing", "suspended", "closed"].includes(status))
|
|
3519
|
+
throw new SlackApiError2("invalid_status");
|
|
3520
|
+
const threadTs = str2(args, "thread_ts");
|
|
3521
|
+
if (!threadTs)
|
|
3522
|
+
throw new SlackApiError2("thread_ts_required");
|
|
3523
|
+
ctx.store.assistantThread(channel.id, threadTs).sessionStatus = status;
|
|
3524
|
+
return { status, agent_status: status };
|
|
3525
|
+
},
|
|
3026
3526
|
"assistant.threads.setTitle": (ctx, args) => {
|
|
3027
3527
|
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
3028
3528
|
const threadTs = required(args, "thread_ts");
|
|
@@ -3126,6 +3626,7 @@ var DEFAULT_EVENTS = [
|
|
|
3126
3626
|
class SlackMock2 {
|
|
3127
3627
|
store;
|
|
3128
3628
|
hub;
|
|
3629
|
+
delivery;
|
|
3129
3630
|
opts;
|
|
3130
3631
|
server;
|
|
3131
3632
|
triggerIds = new Map;
|
|
@@ -3153,6 +3654,7 @@ class SlackMock2 {
|
|
|
3153
3654
|
maxRetries: opts.maxRetries,
|
|
3154
3655
|
log: this.log
|
|
3155
3656
|
});
|
|
3657
|
+
this.delivery = opts.httpDelivery ? new HttpDelivery2({ log: this.log, ...opts.httpDelivery }) : this.hub;
|
|
3156
3658
|
this.subscribed = new Set(opts.subscribedEvents ?? manifest?.settings?.event_subscriptions?.bot_events ?? DEFAULT_EVENTS);
|
|
3157
3659
|
if (manifest?.features?.slash_commands)
|
|
3158
3660
|
this.commands = new Set(manifest.features.slash_commands.map((c) => c.command));
|
|
@@ -3246,20 +3748,22 @@ class SlackMock2 {
|
|
|
3246
3748
|
SLACK_BOT_TOKEN: this.store.bot.token,
|
|
3247
3749
|
SLACK_APP_TOKEN: this.store.bot.appToken,
|
|
3248
3750
|
SLACK_API_URL: this.apiUrl,
|
|
3249
|
-
SLACK_SIGNING_SECRET: "mock-signing-secret"
|
|
3751
|
+
SLACK_SIGNING_SECRET: this.opts.httpDelivery?.signingSecret ?? "mock-signing-secret"
|
|
3250
3752
|
};
|
|
3251
3753
|
}
|
|
3252
3754
|
get bot() {
|
|
3253
3755
|
return this.store.bot;
|
|
3254
3756
|
}
|
|
3255
3757
|
get connectionCount() {
|
|
3256
|
-
return this.
|
|
3758
|
+
return this.delivery.connectionCount;
|
|
3257
3759
|
}
|
|
3258
3760
|
get team() {
|
|
3259
3761
|
return this.store.team;
|
|
3260
3762
|
}
|
|
3261
3763
|
async stop() {
|
|
3262
3764
|
this.hub.shutdown();
|
|
3765
|
+
if (this.delivery instanceof HttpDelivery2)
|
|
3766
|
+
this.delivery.shutdown();
|
|
3263
3767
|
await Promise.allSettled([...this.inflight]);
|
|
3264
3768
|
this.server.stop(true);
|
|
3265
3769
|
}
|
|
@@ -3381,7 +3885,7 @@ class SlackMock2 {
|
|
|
3381
3885
|
};
|
|
3382
3886
|
try {
|
|
3383
3887
|
const body = handler(ctx, args);
|
|
3384
|
-
record(
|
|
3888
|
+
record(body.ok !== false);
|
|
3385
3889
|
return Response.json({ ok: true, ...body });
|
|
3386
3890
|
} catch (e) {
|
|
3387
3891
|
if (e instanceof SlackApiError2) {
|
|
@@ -3501,7 +4005,7 @@ class SlackMock2 {
|
|
|
3501
4005
|
const opts = {
|
|
3502
4006
|
screenshot,
|
|
3503
4007
|
refreshSec: Number.isFinite(refresh) && refresh > 0 ? refresh : undefined,
|
|
3504
|
-
connections: this.
|
|
4008
|
+
connections: this.delivery.connectionCount,
|
|
3505
4009
|
panelWidth: panel ? /^\d+$/.test(panel) ? `${panel}%` : /^\d+(px|%|vw|rem)$/.test(panel) ? panel : undefined : undefined,
|
|
3506
4010
|
threadView: url.searchParams.has("full") ? "full" : "panel",
|
|
3507
4011
|
live: url.searchParams.get("live") !== "0",
|
|
@@ -3577,7 +4081,7 @@ data: ${JSON.stringify(data)}
|
|
|
3577
4081
|
}
|
|
3578
4082
|
track(p) {
|
|
3579
4083
|
const wrapped = p.then(() => {}, (e) => {
|
|
3580
|
-
if (!(e instanceof SocketModeClosedError))
|
|
4084
|
+
if (!(e instanceof SocketModeClosedError || e instanceof HttpDeliveryStoppedError2))
|
|
3581
4085
|
this.log(`delivery failed: ${e instanceof Error ? e.message : e}`);
|
|
3582
4086
|
});
|
|
3583
4087
|
this.inflight.add(wrapped);
|
|
@@ -3585,14 +4089,14 @@ data: ${JSON.stringify(data)}
|
|
|
3585
4089
|
return wrapped;
|
|
3586
4090
|
}
|
|
3587
4091
|
push(event) {
|
|
3588
|
-
if (this.
|
|
4092
|
+
if (this.delivery.connectionCount === 0) {
|
|
3589
4093
|
this.log(`dropping ${event.type}${event.subtype ? `/${event.subtype}` : ""} event: no app connected`);
|
|
3590
4094
|
return Promise.resolve();
|
|
3591
4095
|
}
|
|
3592
4096
|
const send = async () => {
|
|
3593
4097
|
if (this.opts.eventDelayMs)
|
|
3594
4098
|
await Bun.sleep(this.opts.eventDelayMs);
|
|
3595
|
-
await this.
|
|
4099
|
+
await this.delivery.send("events_api", eventCallback(this.store, event));
|
|
3596
4100
|
};
|
|
3597
4101
|
return this.track(send());
|
|
3598
4102
|
}
|
|
@@ -3793,7 +4297,7 @@ data: ${JSON.stringify(data)}
|
|
|
3793
4297
|
responseUrl,
|
|
3794
4298
|
triggerId: this.issueTriggerId()
|
|
3795
4299
|
});
|
|
3796
|
-
const ack = await this.
|
|
4300
|
+
const ack = await this.delivery.send("slash_commands", payload, true);
|
|
3797
4301
|
let response;
|
|
3798
4302
|
const body = ack.payload;
|
|
3799
4303
|
if (body && (body.text || body.blocks)) {
|
|
@@ -3842,7 +4346,7 @@ data: ${JSON.stringify(data)}
|
|
|
3842
4346
|
}
|
|
3843
4347
|
]
|
|
3844
4348
|
});
|
|
3845
|
-
const ack = await this.
|
|
4349
|
+
const ack = await this.delivery.send("interactive", payload, true);
|
|
3846
4350
|
await this.flush();
|
|
3847
4351
|
return ack;
|
|
3848
4352
|
}
|
|
@@ -3859,7 +4363,7 @@ data: ${JSON.stringify(data)}
|
|
|
3859
4363
|
values: input.values,
|
|
3860
4364
|
triggerId: this.issueTriggerId()
|
|
3861
4365
|
});
|
|
3862
|
-
const ack = await this.
|
|
4366
|
+
const ack = await this.delivery.send("interactive", payload, true);
|
|
3863
4367
|
const response = ack.payload ?? {};
|
|
3864
4368
|
if (response.response_action === "errors") {
|
|
3865
4369
|
view.state = { values: input.values };
|
|
@@ -3905,14 +4409,14 @@ data: ${JSON.stringify(data)}
|
|
|
3905
4409
|
});
|
|
3906
4410
|
const threadTs = root.ts;
|
|
3907
4411
|
if (this.isSubscribed("assistant_thread_started")) {
|
|
3908
|
-
await this.track(this.
|
|
4412
|
+
await this.track(this.delivery.send("events_api", eventCallback(this.store, assistantThreadStartedEvent(this.store, dm, user.id, threadTs, input.context))));
|
|
3909
4413
|
}
|
|
3910
4414
|
return { channel: dm.id, thread_ts: threadTs };
|
|
3911
4415
|
}
|
|
3912
4416
|
async changeAssistantContext(input) {
|
|
3913
4417
|
const user = input.user ? this.user(input.user) : this.defaultHuman();
|
|
3914
4418
|
const dm = this.resolveChannel(input.channel);
|
|
3915
|
-
await this.track(this.
|
|
4419
|
+
await this.track(this.delivery.send("events_api", eventCallback(this.store, assistantThreadContextChangedEvent(this.store, dm, user.id, input.thread_ts, input.context))));
|
|
3916
4420
|
}
|
|
3917
4421
|
assistantThread(channel, thread_ts) {
|
|
3918
4422
|
return this.store.assistantThreads.get(`${this.resolveChannel(channel).id}:${thread_ts}`) ?? {};
|
|
@@ -3931,7 +4435,7 @@ data: ${JSON.stringify(data)}
|
|
|
3931
4435
|
this.faults.push({ ...fault, remaining: fault.times ?? 1 });
|
|
3932
4436
|
}
|
|
3933
4437
|
deliveries(name) {
|
|
3934
|
-
return name ? this.
|
|
4438
|
+
return name ? this.delivery.history.filter((d) => d.name === name) : [...this.delivery.history];
|
|
3935
4439
|
}
|
|
3936
4440
|
apiCalls(method) {
|
|
3937
4441
|
return method ? this.store.apiCalls.filter((c) => c.method === method) : [...this.store.apiCalls];
|
|
@@ -4000,7 +4504,7 @@ data: ${JSON.stringify(data)}
|
|
|
4000
4504
|
});
|
|
4001
4505
|
}
|
|
4002
4506
|
waitForConnection(timeoutMs = 15000) {
|
|
4003
|
-
return this.
|
|
4507
|
+
return this.delivery.waitForConnection(timeoutMs);
|
|
4004
4508
|
}
|
|
4005
4509
|
disconnectSockets(reason) {
|
|
4006
4510
|
this.hub.disconnectAll(reason);
|
|
@@ -4019,7 +4523,7 @@ data: ${JSON.stringify(data)}
|
|
|
4019
4523
|
team: this.store.team,
|
|
4020
4524
|
app: this.store.app,
|
|
4021
4525
|
bot: { userId: this.store.bot.userId, botId: this.store.bot.botId },
|
|
4022
|
-
connections: this.
|
|
4526
|
+
connections: this.delivery.connectionCount,
|
|
4023
4527
|
users: [...this.store.users.values()],
|
|
4024
4528
|
channels: [...this.store.channels.values()]
|
|
4025
4529
|
});
|
|
@@ -4181,4 +4685,4 @@ async function frames2(opts) {
|
|
|
4181
4685
|
}
|
|
4182
4686
|
}
|
|
4183
4687
|
|
|
4184
|
-
export { SlackApiError2, Store2, SlackMock2, frames2 };
|
|
4688
|
+
export { SlackApiError2, Store2, slackSignature2, HttpDeliveryError2, HttpDeliveryStoppedError2, HttpDelivery2, SlackMock2, frames2 };
|