(cfg: AttachAssistantConfig)
| 42 | |
| 43 | /** Register the Bolt `Assistant` middleware and bridge it to the engine sink. */ |
| 44 | export function attachAssistant(cfg: AttachAssistantConfig): AssistantHandle { |
| 45 | const { app, sink, opts, resolveUser } = cfg; |
| 46 | |
| 47 | // Pane threads seen this process (channelId::threadTs). Populated on |
| 48 | // thread_started and (defensively, after a restart) on the first message. |
| 49 | const assistantThreads = new Set<string>(); |
| 50 | // Threads we've already auto-titled, so only the FIRST user message titles. |
| 51 | const titled = new Set<string>(); |
| 52 | |
| 53 | const titleAuto = opts.title !== false; // default "auto" |
| 54 | |
| 55 | const assistant = new Assistant({ |
| 56 | threadStarted: async ({ event, say, setSuggestedPrompts }) => { |
| 57 | const at = (event as { assistant_thread?: AssistantThreadMeta }) |
| 58 | .assistant_thread; |
| 59 | if (!at?.channel_id || !at.thread_ts) return; |
| 60 | const channelId = at.channel_id; |
| 61 | const threadTs = at.thread_ts; |
| 62 | assistantThreads.add(threadKey(channelId, threadTs)); |
| 63 | |
| 64 | // ── Static defaults first (greeting + prompts). Degrade, never throw. ── |
| 65 | if (opts.greeting) { |
| 66 | try { |
| 67 | await say(opts.greeting); |
| 68 | } catch (err) { |
| 69 | console.error("[slack-assistant] greeting failed:", err); |
| 70 | } |
| 71 | } |
| 72 | if (opts.suggestedPrompts && opts.suggestedPrompts.length > 0) { |
| 73 | try { |
| 74 | await setSuggestedPrompts({ |
| 75 | prompts: opts.suggestedPrompts.map((p) => ({ |
| 76 | title: p.title, |
| 77 | message: p.message, |
| 78 | })) as [ |
| 79 | { title: string; message: string }, |
| 80 | ...{ title: string; message: string }[], |
| 81 | ], |
| 82 | }); |
| 83 | } catch (err) { |
| 84 | console.error("[slack-assistant] setSuggestedPrompts failed:", err); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // ── Then hand off to the engine (onThreadStarted handlers layer on top). ── |
| 89 | const user = at.user_id ? await resolveUser(at.user_id) : undefined; |
| 90 | await sink.onThreadStarted({ |
| 91 | conversationKey: conversationKeyOf({ channelId, scope: threadTs }), |
| 92 | replyTarget: { |
| 93 | channel: channelId, |
| 94 | threadTs, |
| 95 | recipientUserId: at.user_id, |
| 96 | }, |
| 97 | user, |
| 98 | platform: "slack", |
| 99 | }); |
| 100 | }, |
| 101 |
searching dependent graphs…