@cuylabs/channel-slack-agent-core 0.1.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.
Files changed (68) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +80 -0
  3. package/dist/adapter-Cmd2C90g.d.ts +31 -0
  4. package/dist/adapter.d.ts +23 -0
  5. package/dist/adapter.js +13 -0
  6. package/dist/app-surface.d.ts +71 -0
  7. package/dist/app-surface.js +12 -0
  8. package/dist/app.d.ts +54 -0
  9. package/dist/app.js +14 -0
  10. package/dist/assistant.d.ts +19 -0
  11. package/dist/assistant.js +16 -0
  12. package/dist/bolt.d.ts +8 -0
  13. package/dist/bolt.js +10 -0
  14. package/dist/chunk-2SUAW6MV.js +12 -0
  15. package/dist/chunk-645NNJIM.js +12 -0
  16. package/dist/chunk-ANIZ5NT4.js +12 -0
  17. package/dist/chunk-BFUPAJON.js +662 -0
  18. package/dist/chunk-CYEBGC6G.js +77 -0
  19. package/dist/chunk-DHPD4XH5.js +827 -0
  20. package/dist/chunk-FDRQOG7Q.js +471 -0
  21. package/dist/chunk-GNXWTKQ6.js +48 -0
  22. package/dist/chunk-HFT2FXJP.js +12 -0
  23. package/dist/chunk-I2KLQ2HA.js +22 -0
  24. package/dist/chunk-IWUYIAY5.js +69 -0
  25. package/dist/chunk-IXY3BXU5.js +689 -0
  26. package/dist/chunk-JMLB7A2V.js +85 -0
  27. package/dist/chunk-K2E6A377.js +12 -0
  28. package/dist/chunk-M64Z6TYL.js +198 -0
  29. package/dist/chunk-NDVXBI7Z.js +12 -0
  30. package/dist/chunk-NIPAN4KA.js +76 -0
  31. package/dist/chunk-PX4RGO3N.js +12 -0
  32. package/dist/chunk-RFHXERNL.js +27 -0
  33. package/dist/chunk-VHGV66M7.js +12 -0
  34. package/dist/chunk-WO4BJMF3.js +82 -0
  35. package/dist/diagnostics.d.ts +1 -0
  36. package/dist/diagnostics.js +10 -0
  37. package/dist/express-assistant.d.ts +102 -0
  38. package/dist/express-assistant.js +12 -0
  39. package/dist/express.d.ts +98 -0
  40. package/dist/express.js +11 -0
  41. package/dist/feedback.d.ts +1 -0
  42. package/dist/feedback.js +10 -0
  43. package/dist/history.d.ts +1 -0
  44. package/dist/history.js +10 -0
  45. package/dist/index.d.ts +32 -0
  46. package/dist/index.js +202 -0
  47. package/dist/interactive-o_NZb-Xg.d.ts +47 -0
  48. package/dist/interactive.d.ts +30 -0
  49. package/dist/interactive.js +25 -0
  50. package/dist/mcp.d.ts +84 -0
  51. package/dist/mcp.js +9 -0
  52. package/dist/options-C7OYeNR-.d.ts +71 -0
  53. package/dist/options-Uf-qmQKN.d.ts +263 -0
  54. package/dist/policy.d.ts +1 -0
  55. package/dist/policy.js +10 -0
  56. package/dist/setup.d.ts +1 -0
  57. package/dist/setup.js +10 -0
  58. package/dist/shared.d.ts +129 -0
  59. package/dist/shared.js +20 -0
  60. package/dist/socket.d.ts +137 -0
  61. package/dist/socket.js +16 -0
  62. package/dist/targets.d.ts +1 -0
  63. package/dist/targets.js +10 -0
  64. package/dist/types-BqRzb_Cd.d.ts +346 -0
  65. package/dist/types-Crpil4kb.d.ts +136 -0
  66. package/dist/users.d.ts +1 -0
  67. package/dist/users.js +10 -0
  68. package/package.json +169 -0
@@ -0,0 +1,689 @@
1
+ // src/interactive/store.ts
2
+ function createInMemorySlackInteractiveRequestStore() {
3
+ const records = /* @__PURE__ */ new Map();
4
+ return {
5
+ async get(requestId) {
6
+ const record = records.get(requestId);
7
+ return record ? cloneRecord(record) : void 0;
8
+ },
9
+ async upsert(record) {
10
+ const existing = records.get(record.id);
11
+ if (existing?.status === "resolved") {
12
+ return cloneRecord(existing);
13
+ }
14
+ const next = existing ? {
15
+ ...existing,
16
+ ...record,
17
+ target: record.target ?? existing.target,
18
+ resolution: record.resolution ?? existing.resolution,
19
+ updatedAt: nowIso()
20
+ } : { ...record };
21
+ records.set(record.id, cloneRecord(next));
22
+ return cloneRecord(next);
23
+ },
24
+ async attachTarget(requestId, target) {
25
+ const existing = records.get(requestId);
26
+ if (!existing) return void 0;
27
+ const next = {
28
+ ...existing,
29
+ target: cloneTarget(target),
30
+ updatedAt: nowIso()
31
+ };
32
+ records.set(requestId, cloneRecord(next));
33
+ return cloneRecord(next);
34
+ },
35
+ async resolve(requestId, resolution) {
36
+ const existing = records.get(requestId);
37
+ if (!existing) return void 0;
38
+ if (existing.status === "resolved") {
39
+ return cloneRecord(existing);
40
+ }
41
+ const next = {
42
+ ...existing,
43
+ status: "resolved",
44
+ resolution: cloneResolution(resolution),
45
+ updatedAt: nowIso()
46
+ };
47
+ records.set(requestId, cloneRecord(next));
48
+ return cloneRecord(next);
49
+ },
50
+ async delete(requestId) {
51
+ records.delete(requestId);
52
+ }
53
+ };
54
+ }
55
+ function nowIso() {
56
+ return (/* @__PURE__ */ new Date()).toISOString();
57
+ }
58
+ function cloneRecord(record) {
59
+ return {
60
+ ...record,
61
+ request: structuredClone(record.request),
62
+ ...record.target ? { target: cloneTarget(record.target) } : {},
63
+ ...record.resolution ? { resolution: cloneResolution(record.resolution) } : {}
64
+ };
65
+ }
66
+ function cloneTarget(target) {
67
+ return { ...target };
68
+ }
69
+ function cloneResolution(resolution) {
70
+ return structuredClone(resolution);
71
+ }
72
+
73
+ // src/interactive/blocks.ts
74
+ var MAX_BLOCK_TEXT = 2800;
75
+ function buildApprovalRequestMessage(request, actionIds) {
76
+ const rememberScopes = request.rememberScopes ?? [];
77
+ const canRemember = rememberScopes.length > 0 || Boolean(request.defaultRememberScope);
78
+ const text = `Approval required for ${request.tool}`;
79
+ return {
80
+ text,
81
+ blocks: [
82
+ section(
83
+ `*Approval required*
84
+ ${escapeMrkdwn(request.description || request.tool)}`
85
+ ),
86
+ fields([
87
+ `*Tool*
88
+ ${escapeMrkdwn(request.tool)}`,
89
+ `*Risk*
90
+ ${escapeMrkdwn(request.risk)}`
91
+ ]),
92
+ section(`*Arguments*
93
+ \`\`\`${truncate(formatArgs(request.args))}\`\`\``),
94
+ {
95
+ type: "actions",
96
+ elements: [
97
+ button("Allow", "primary", actionIds.approvalAllow, {
98
+ requestId: request.id
99
+ }),
100
+ button("Deny", "danger", actionIds.approvalDeny, {
101
+ requestId: request.id
102
+ }),
103
+ ...canRemember ? [
104
+ button("Remember", void 0, actionIds.approvalRemember, {
105
+ requestId: request.id,
106
+ rememberScope: request.defaultRememberScope ?? rememberScopes[0]
107
+ })
108
+ ] : []
109
+ ]
110
+ }
111
+ ]
112
+ };
113
+ }
114
+ function buildHumanInputRequestMessage(request, actionIds) {
115
+ const text = request.title || "Input required";
116
+ const confirmLabel = request.confirmLabel ?? "Submit";
117
+ const denyLabel = request.denyLabel ?? "Cancel";
118
+ if (request.kind === "confirm") {
119
+ return {
120
+ text,
121
+ blocks: [
122
+ section(`*${escapeMrkdwn(text)}*
123
+ ${escapeMrkdwn(request.question)}`),
124
+ {
125
+ type: "actions",
126
+ elements: [
127
+ button(confirmLabel, "primary", actionIds.humanConfirm, {
128
+ requestId: request.id
129
+ }),
130
+ button(denyLabel, "danger", actionIds.humanDeny, {
131
+ requestId: request.id
132
+ })
133
+ ]
134
+ }
135
+ ]
136
+ };
137
+ }
138
+ return {
139
+ text,
140
+ blocks: [
141
+ section(`*${escapeMrkdwn(text)}*
142
+ ${escapeMrkdwn(request.question)}`),
143
+ {
144
+ type: "actions",
145
+ elements: [
146
+ button(confirmLabel, "primary", actionIds.humanOpen, {
147
+ requestId: request.id
148
+ }),
149
+ button(denyLabel, "danger", actionIds.humanDeny, {
150
+ requestId: request.id
151
+ })
152
+ ]
153
+ }
154
+ ]
155
+ };
156
+ }
157
+ function buildResolvedMessage(label, resolution) {
158
+ const text = resolution.kind === "approval" ? resolution.action === "deny" ? "Approval denied" : "Approval granted" : "Input submitted";
159
+ return {
160
+ text,
161
+ blocks: [section(`*${escapeMrkdwn(text)}*
162
+ ${escapeMrkdwn(label)}`)]
163
+ };
164
+ }
165
+ function buildHumanInputModal(request, actionIds) {
166
+ const title = truncatePlain(request.title || "Input required", 24);
167
+ const submit = truncatePlain(request.confirmLabel ?? "Submit", 24);
168
+ const close = truncatePlain(request.denyLabel ?? "Cancel", 24);
169
+ if (request.kind === "choice") {
170
+ const options = (request.options ?? []).slice(0, 100).map((option) => ({
171
+ text: {
172
+ type: "plain_text",
173
+ text: truncatePlain(option.label, 75)
174
+ },
175
+ value: option.value ?? option.label,
176
+ ...option.description ? {
177
+ description: {
178
+ type: "plain_text",
179
+ text: truncatePlain(option.description, 75)
180
+ }
181
+ } : {}
182
+ }));
183
+ return {
184
+ type: "modal",
185
+ callback_id: actionIds.humanSubmit,
186
+ private_metadata: JSON.stringify({ requestId: request.id }),
187
+ title: { type: "plain_text", text: title },
188
+ submit: { type: "plain_text", text: submit },
189
+ close: { type: "plain_text", text: close },
190
+ blocks: [
191
+ {
192
+ type: "input",
193
+ block_id: "input",
194
+ label: {
195
+ type: "plain_text",
196
+ text: truncatePlain(request.question, 200)
197
+ },
198
+ element: {
199
+ type: request.allowMultiple ? "checkboxes" : "radio_buttons",
200
+ action_id: "value",
201
+ options
202
+ }
203
+ }
204
+ ]
205
+ };
206
+ }
207
+ return {
208
+ type: "modal",
209
+ callback_id: actionIds.humanSubmit,
210
+ private_metadata: JSON.stringify({ requestId: request.id }),
211
+ title: { type: "plain_text", text: title },
212
+ submit: { type: "plain_text", text: submit },
213
+ close: { type: "plain_text", text: close },
214
+ blocks: [
215
+ {
216
+ type: "input",
217
+ block_id: "input",
218
+ label: {
219
+ type: "plain_text",
220
+ text: truncatePlain(request.question, 200)
221
+ },
222
+ element: {
223
+ type: "plain_text_input",
224
+ action_id: "value",
225
+ multiline: true,
226
+ ...request.placeholder ? {
227
+ placeholder: {
228
+ type: "plain_text",
229
+ text: truncatePlain(request.placeholder, 150)
230
+ }
231
+ } : {}
232
+ }
233
+ }
234
+ ]
235
+ };
236
+ }
237
+ function encodeActionValue(payload) {
238
+ return JSON.stringify(payload);
239
+ }
240
+ function decodeActionValue(value) {
241
+ if (typeof value !== "string") return {};
242
+ try {
243
+ const parsed = JSON.parse(value);
244
+ return parsed && typeof parsed === "object" ? parsed : {};
245
+ } catch {
246
+ return {};
247
+ }
248
+ }
249
+ function button(text, style, actionId, value) {
250
+ return {
251
+ type: "button",
252
+ text: { type: "plain_text", text },
253
+ action_id: actionId,
254
+ value: encodeActionValue(value),
255
+ ...style ? { style } : {}
256
+ };
257
+ }
258
+ function section(text) {
259
+ return { type: "section", text: { type: "mrkdwn", text: truncate(text) } };
260
+ }
261
+ function fields(items) {
262
+ return {
263
+ type: "section",
264
+ fields: items.map((text) => ({ type: "mrkdwn", text: truncate(text) }))
265
+ };
266
+ }
267
+ function formatArgs(args) {
268
+ if (typeof args === "string") return args;
269
+ try {
270
+ return JSON.stringify(args, null, 2);
271
+ } catch {
272
+ return String(args);
273
+ }
274
+ }
275
+ function truncate(value, max = MAX_BLOCK_TEXT) {
276
+ return value.length <= max ? value : `${value.slice(0, max - 3)}...`;
277
+ }
278
+ function truncatePlain(value, max) {
279
+ const normalized = value.trim() || "Input";
280
+ return normalized.length <= max ? normalized : `${normalized.slice(0, max - 3)}...`;
281
+ }
282
+ function escapeMrkdwn(value) {
283
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
284
+ }
285
+
286
+ // src/interactive/controller.ts
287
+ var DEFAULT_NAMESPACE = "agent_slack";
288
+ var DEFAULT_REQUEST_TIMEOUT_MS = 5 * 60 * 1e3;
289
+ var installedActionIds = /* @__PURE__ */ new WeakMap();
290
+ function createSlackInteractiveController(options = {}) {
291
+ const store = options.store ?? createInMemorySlackInteractiveRequestStore();
292
+ const actionIds = resolveActionIds(
293
+ options.namespace ?? DEFAULT_NAMESPACE,
294
+ options.actionIds
295
+ );
296
+ const requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
297
+ const waiters = /* @__PURE__ */ new Map();
298
+ const pendingIds = /* @__PURE__ */ new Set();
299
+ async function ensurePending(kind, request) {
300
+ const existing = await store.get(request.id);
301
+ if (existing) {
302
+ pendingIds.add(request.id);
303
+ return existing;
304
+ }
305
+ const createdAt = nowIso();
306
+ const record = await store.upsert({
307
+ id: request.id,
308
+ kind,
309
+ request,
310
+ status: "pending",
311
+ createdAt,
312
+ updatedAt: createdAt
313
+ });
314
+ pendingIds.add(request.id);
315
+ return record;
316
+ }
317
+ async function waitForResolution(kind, request, waitOptions = {}) {
318
+ const existing = await ensurePending(kind, request);
319
+ if (existing.status === "resolved" && existing.resolution) {
320
+ return existing.resolution;
321
+ }
322
+ if (waiters.has(request.id)) {
323
+ throw new Error(
324
+ `Slack interactive request is already waiting: ${request.id}. Resolve or cancel the in-flight request before requesting again.`
325
+ );
326
+ }
327
+ return await new Promise((resolve, reject) => {
328
+ const cleanupCallbacks = [];
329
+ const timeoutMs = waitOptions.timeoutMs ?? requestTimeoutMs;
330
+ if (timeoutMs > 0) {
331
+ const timeoutId = setTimeout(() => {
332
+ void cancel(request.id, "Slack interactive request timed out.");
333
+ }, timeoutMs);
334
+ cleanupCallbacks.push(() => clearTimeout(timeoutId));
335
+ }
336
+ let abortImmediately = false;
337
+ if (waitOptions.signal) {
338
+ const onAbort = () => {
339
+ void cancel(request.id, "Slack interactive request aborted.");
340
+ };
341
+ if (waitOptions.signal.aborted) {
342
+ abortImmediately = true;
343
+ } else {
344
+ waitOptions.signal.addEventListener("abort", onAbort, {
345
+ once: true
346
+ });
347
+ cleanupCallbacks.push(
348
+ () => waitOptions.signal?.removeEventListener("abort", onAbort)
349
+ );
350
+ }
351
+ }
352
+ waiters.set(request.id, {
353
+ resolve,
354
+ reject,
355
+ cleanup: () => {
356
+ for (const cleanup of cleanupCallbacks.splice(0)) {
357
+ cleanup();
358
+ }
359
+ }
360
+ });
361
+ if (abortImmediately) {
362
+ void cancel(request.id, "Slack interactive request aborted.");
363
+ }
364
+ });
365
+ }
366
+ async function resolveRequest(requestId, resolution) {
367
+ const record = await store.resolve(requestId, resolution);
368
+ const waiter = waiters.get(requestId);
369
+ if (waiter) {
370
+ waiters.delete(requestId);
371
+ waiter.cleanup();
372
+ waiter.resolve(resolution);
373
+ }
374
+ if (record) {
375
+ pendingIds.delete(requestId);
376
+ await options.onResolve?.(requestId, resolution);
377
+ }
378
+ return record;
379
+ }
380
+ async function cancel(requestId, reason = "Cancelled") {
381
+ const waiter = waiters.get(requestId);
382
+ if (waiter) {
383
+ waiters.delete(requestId);
384
+ waiter.cleanup();
385
+ waiter.reject(new Error(reason));
386
+ }
387
+ const existing = await store.get(requestId);
388
+ if (existing?.status === "pending") {
389
+ await store.delete(requestId);
390
+ pendingIds.delete(requestId);
391
+ return true;
392
+ }
393
+ pendingIds.delete(requestId);
394
+ return Boolean(waiter);
395
+ }
396
+ async function cancelAll(reason = "Cancelled") {
397
+ await Promise.all(
398
+ [...pendingIds].map((requestId) => cancel(requestId, reason))
399
+ );
400
+ }
401
+ async function handleInteractiveRequest(context) {
402
+ const request = context.request;
403
+ const record = await ensurePending(context.kind, request);
404
+ if (record.target) {
405
+ return true;
406
+ }
407
+ const message = context.kind === "approval" ? buildApprovalRequestMessage(
408
+ request,
409
+ actionIds
410
+ ) : buildHumanInputRequestMessage(
411
+ request,
412
+ actionIds
413
+ );
414
+ const ref = await context.responder.postMessage(message);
415
+ await store.attachTarget(request.id, {
416
+ channel: ref.channel,
417
+ ts: ref.ts,
418
+ userId: context.user.userId,
419
+ teamId: context.user.teamId,
420
+ ...context.slackActivity.threadTs ? { threadTs: context.slackActivity.threadTs } : {}
421
+ });
422
+ return true;
423
+ }
424
+ function install(app) {
425
+ assertActionIdsCanInstall(app, actionIds);
426
+ app.action(actionIds.approvalAllow, async (args) => {
427
+ await handleAction(args, {
428
+ kind: "approval",
429
+ action: "allow"
430
+ });
431
+ });
432
+ app.action(actionIds.approvalDeny, async (args) => {
433
+ await handleAction(args, {
434
+ kind: "approval",
435
+ action: "deny"
436
+ });
437
+ });
438
+ app.action(actionIds.approvalRemember, async (args) => {
439
+ const value = firstActionValue(args);
440
+ const rememberScope = typeof value.rememberScope === "string" ? value.rememberScope : void 0;
441
+ await handleAction(args, {
442
+ kind: "approval",
443
+ action: "remember",
444
+ ...rememberScope ? { rememberScope } : {}
445
+ });
446
+ });
447
+ app.action(actionIds.humanConfirm, async (args) => {
448
+ await handleAction(args, {
449
+ kind: "human-input",
450
+ response: { kind: "confirm", confirmed: true, text: "Confirmed" }
451
+ });
452
+ });
453
+ app.action(actionIds.humanDeny, async (args) => {
454
+ await handleAction(args, {
455
+ kind: "human-input",
456
+ response: { kind: "confirm", confirmed: false, text: "Cancelled" }
457
+ });
458
+ });
459
+ app.action(actionIds.humanOpen, async (args) => {
460
+ await openHumanInputModal(args);
461
+ });
462
+ app.view(actionIds.humanSubmit, async (args) => {
463
+ await submitHumanInputModal(args);
464
+ });
465
+ }
466
+ async function handleAction(args, resolutionInput) {
467
+ const actionArgs = args;
468
+ await actionArgs.ack();
469
+ const requestId = extractRequestId(firstActionValue(args));
470
+ if (!requestId) return;
471
+ const record = await store.get(requestId);
472
+ if (!record || record.status === "resolved") {
473
+ return;
474
+ }
475
+ const actor = extractActor(actionArgs.body);
476
+ if (!await isAuthorized(record, actor)) {
477
+ await postEphemeral(
478
+ actionArgs,
479
+ "Only the original requester can resolve this request."
480
+ );
481
+ return;
482
+ }
483
+ const resolution = resolutionInput;
484
+ const resolved = await resolveRequest(requestId, resolution);
485
+ if (resolved?.target) {
486
+ await updateOriginalMessage(actionArgs, resolved, resolution);
487
+ }
488
+ }
489
+ async function openHumanInputModal(args) {
490
+ const actionArgs = args;
491
+ await actionArgs.ack();
492
+ const requestId = extractRequestId(firstActionValue(args));
493
+ if (!requestId || !actionArgs.body.trigger_id) return;
494
+ const record = await store.get(requestId);
495
+ if (!record || record.kind !== "human-input") return;
496
+ const actor = extractActor(actionArgs.body);
497
+ if (!await isAuthorized(record, actor)) {
498
+ await postEphemeral(
499
+ actionArgs,
500
+ "Only the original requester can answer this request."
501
+ );
502
+ return;
503
+ }
504
+ await actionArgs.client.views.open({
505
+ trigger_id: actionArgs.body.trigger_id,
506
+ view: buildHumanInputModal(
507
+ record.request,
508
+ actionIds
509
+ )
510
+ });
511
+ }
512
+ async function submitHumanInputModal(args) {
513
+ const viewArgs = args;
514
+ await viewArgs.ack();
515
+ const requestId = extractRequestIdFromView(viewArgs.view);
516
+ if (!requestId) return;
517
+ const record = await store.get(requestId);
518
+ if (!record || record.kind !== "human-input" || record.status === "resolved") {
519
+ return;
520
+ }
521
+ const actor = extractActor(viewArgs.body);
522
+ if (!await isAuthorized(record, actor)) {
523
+ return;
524
+ }
525
+ const response = responseFromView(
526
+ record.request,
527
+ viewArgs.view
528
+ );
529
+ const resolution = {
530
+ kind: "human-input",
531
+ response
532
+ };
533
+ const resolved = await resolveRequest(requestId, resolution);
534
+ if (resolved?.target) {
535
+ await viewArgs.client.chat.update({
536
+ channel: resolved.target.channel,
537
+ ts: resolved.target.ts,
538
+ ...buildResolvedMessage("Slack response received.", resolution)
539
+ });
540
+ }
541
+ }
542
+ async function isAuthorized(record, actor) {
543
+ if (options.authorize) {
544
+ return await options.authorize(record, actor);
545
+ }
546
+ return record.target?.userId === actor.userId;
547
+ }
548
+ async function updateOriginalMessage(args, record, resolution) {
549
+ const target = record.target;
550
+ if (!target) return;
551
+ const label = resolution.kind === "approval" ? `${resolution.action} selected.` : resolution.response.text;
552
+ await args.client.chat.update({
553
+ channel: target.channel,
554
+ ts: target.ts,
555
+ ...buildResolvedMessage(label, resolution)
556
+ });
557
+ }
558
+ return {
559
+ actionIds,
560
+ store,
561
+ approval: {
562
+ async onRequest(request, options2) {
563
+ const resolution = await waitForResolution(
564
+ "approval",
565
+ request,
566
+ options2
567
+ );
568
+ if (resolution.kind !== "approval") {
569
+ throw new Error(
570
+ `Unexpected human-input resolution for ${request.id}.`
571
+ );
572
+ }
573
+ return {
574
+ action: resolution.action,
575
+ ...resolution.feedback ? { feedback: resolution.feedback } : {},
576
+ ...resolution.rememberScope ? { rememberScope: resolution.rememberScope } : {}
577
+ };
578
+ }
579
+ },
580
+ humanInput: {
581
+ async onRequest(request, options2) {
582
+ const resolution = await waitForResolution(
583
+ "human-input",
584
+ request,
585
+ options2
586
+ );
587
+ if (resolution.kind !== "human-input") {
588
+ throw new Error(`Unexpected approval resolution for ${request.id}.`);
589
+ }
590
+ return resolution.response;
591
+ }
592
+ },
593
+ cancel,
594
+ cancelAll,
595
+ handleInteractiveRequest,
596
+ install
597
+ };
598
+ }
599
+ function resolveActionIds(namespace, overrides) {
600
+ const prefix = normalizeActionIdNamespace(namespace);
601
+ return {
602
+ approvalAllow: `${prefix}_approval_allow`,
603
+ approvalDeny: `${prefix}_approval_deny`,
604
+ approvalRemember: `${prefix}_approval_remember`,
605
+ humanConfirm: `${prefix}_human_confirm`,
606
+ humanDeny: `${prefix}_human_deny`,
607
+ humanOpen: `${prefix}_human_open`,
608
+ humanSubmit: `${prefix}_human_submit`,
609
+ ...overrides ?? {}
610
+ };
611
+ }
612
+ function normalizeActionIdNamespace(namespace) {
613
+ const trimmed = namespace.trim();
614
+ if (!trimmed) {
615
+ throw new Error("Slack interactive action namespace cannot be empty.");
616
+ }
617
+ return trimmed;
618
+ }
619
+ function assertActionIdsCanInstall(app, actionIds) {
620
+ const ids = Object.values(actionIds);
621
+ const duplicateWithinController = ids.find(
622
+ (id, index) => ids.indexOf(id) !== index
623
+ );
624
+ if (duplicateWithinController) {
625
+ throw new Error(
626
+ `Duplicate Slack interactive action id configured: ${duplicateWithinController}`
627
+ );
628
+ }
629
+ const appKey = app;
630
+ const installed = installedActionIds.get(appKey) ?? /* @__PURE__ */ new Set();
631
+ const duplicate = ids.find((id) => installed.has(id));
632
+ if (duplicate) {
633
+ throw new Error(
634
+ `Slack interactive action id '${duplicate}' is already installed on this Bolt app. Provide a unique createSlackInteractiveController({ namespace }) or actionIds config.`
635
+ );
636
+ }
637
+ for (const id of ids) {
638
+ installed.add(id);
639
+ }
640
+ installedActionIds.set(appKey, installed);
641
+ }
642
+ function firstActionValue(args) {
643
+ const body = args.body;
644
+ return decodeActionValue(body.actions?.[0]?.value);
645
+ }
646
+ function extractRequestId(value) {
647
+ return typeof value.requestId === "string" && value.requestId.length > 0 ? value.requestId : void 0;
648
+ }
649
+ function extractRequestIdFromView(view) {
650
+ return extractRequestId(decodeActionValue(view.private_metadata));
651
+ }
652
+ function extractActor(body) {
653
+ return {
654
+ userId: body.user?.id ?? "unknown",
655
+ ...body.user?.team_id ?? body.team?.id ? { teamId: body.user?.team_id ?? body.team?.id } : {}
656
+ };
657
+ }
658
+ async function postEphemeral(args, text) {
659
+ const channel = args.body.channel?.id;
660
+ const user = args.body.user?.id;
661
+ if (!channel || !user || !args.client.chat.postEphemeral) return;
662
+ await args.client.chat.postEphemeral({ channel, user, text });
663
+ }
664
+ function responseFromView(request, view) {
665
+ const input = view.state?.values?.input?.value;
666
+ if (request.kind === "choice") {
667
+ const selected = input?.selected_options?.map((option) => option.value ?? "").filter(Boolean) ?? (input?.selected_option?.value ? [input.selected_option.value] : []);
668
+ return {
669
+ kind: "choice",
670
+ selected,
671
+ text: selected.join(", ")
672
+ };
673
+ }
674
+ const text = input?.value ?? "";
675
+ return { kind: "text", text };
676
+ }
677
+
678
+ export {
679
+ createInMemorySlackInteractiveRequestStore,
680
+ nowIso,
681
+ cloneRecord,
682
+ buildApprovalRequestMessage,
683
+ buildHumanInputRequestMessage,
684
+ buildResolvedMessage,
685
+ buildHumanInputModal,
686
+ encodeActionValue,
687
+ decodeActionValue,
688
+ createSlackInteractiveController
689
+ };