slack-edge
Version:
Slack app development framework for edge functions with streamlined TypeScript support
173 lines • 6.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Assistant = void 0;
const context_1 = require("../context/context");
/**
* A class that manages Slack AI Assistant functionality.
*
* The Assistant class provides a simplified interface for building AI-powered assistants
* that respond to user messages in direct message threads. It handles the lifecycle events
* of assistant threads including thread creation, context changes, and message handling.
*
* @template E - The Slack app environment type
* @example
* ```typescript
* const assistant = new Assistant({
* threadStarted: async ({ context, say }) => {
* await say({ text: "Hello! How can I help you?" });
* },
* userMessage: async ({ context, say, payload }) => {
* await say({ text: `You said: ${payload.text}` });
* },
* });
* app.assistant(assistant);
* ```
*/
class Assistant {
/**
* Creates a new Assistant instance with the specified options.
* @param options - Configuration options for the assistant including event handlers
*/
constructor(options = {}) {
this.threadContextStore = options.threadContextStore;
this.threadStartedHandler = async (req) => {
try {
if ((0, context_1.isAssitantThreadEvent)(req.body)) {
const request = req;
if (options.threadStarted) {
await options.threadStarted(request);
}
else {
const { say, setSuggestedPrompts } = request.context;
await say({ text: ":wave: Hi, how can I help you today?" });
await setSuggestedPrompts({ title: "New chat", prompts: ["What does SLACK stand for?"] });
}
}
}
catch (e) {
console.error(`Failed to execute threadStartedHandler listener: ${e.stack}`);
}
};
this.threadContextChangedHandler = async (req) => {
try {
if ((0, context_1.isAssitantThreadEvent)(req.body)) {
const request = req;
if (options.threadContextChanged) {
await options.threadContextChanged(request);
}
else {
// the defualt implementation
const { context } = request;
await context.saveThreadContextStore({ ...request.payload.assistant_thread.context });
}
}
}
catch (e) {
console.error(`Failed to execute threadContextChangedHandler listener: ${e.stack}`);
}
};
this.userMessageHandler = async (req) => {
try {
if (req.payload.subtype === undefined || req.payload.subtype === "file_share") {
if (options.userMessage) {
await options.userMessage(req);
}
else {
// noop; just ack the request
}
}
}
catch (e) {
console.error(`Failed to execute userMessageHandler listener: ${e.stack}`);
}
};
this.botMessageHandler = async (req) => {
try {
if (req.payload.subtype === undefined && req.payload.user === req.context.botUserId) {
if (options.botMessage) {
await options.botMessage(req);
}
else {
// noop; just ack the request
}
}
}
catch (e) {
console.error(`Failed to execute botMessageHandler listener: ${e.stack}`);
}
};
}
/**
* Registers a handler for the `assistant_thread_started` event.
* This event is triggered when a user opens a new assistant thread (DM) with the app.
* @param handler - The handler function to execute when a new thread is started
*/
threadStarted(handler) {
this.threadStartedHandler = async (req) => {
try {
if ((0, context_1.isAssitantThreadEvent)(req.body)) {
await handler(req);
}
}
catch (e) {
console.error(`Failed to execute threadStartedHandler listener: ${e.stack}`);
}
};
}
/**
* Registers a handler for the `assistant_thread_context_changed` event.
* This event is triggered when the context of an assistant thread is updated,
* typically when metadata about the conversation changes.
* @param handler - The handler function to execute when thread context changes
*/
threadContextChanged(handler) {
this.threadContextChangedHandler = async (req) => {
try {
if ((0, context_1.isAssitantThreadEvent)(req.body)) {
await handler(req);
}
}
catch (e) {
console.error(`Failed to execute threadContextChangedHandler listener: ${e.stack}`);
}
};
}
/**
* Registers a handler for user messages sent in the assistant thread.
* This handler is invoked when a user sends a regular message or shares a file
* in the assistant thread.
* @param handler - The handler function to execute when a user sends a message
*/
userMessage(handler) {
this.userMessageHandler = async (req) => {
try {
if (req.payload.subtype === undefined || req.payload.subtype === "file_share") {
await handler(req);
}
}
catch (e) {
console.error(`Failed to execute userMessageHandler listener: ${e.stack}`);
}
};
}
/**
* Registers a handler for bot messages in the assistant thread.
* This handler is invoked when the assistant bot itself sends a message,
* which can be useful for tracking or processing bot responses.
* @param handler - The handler function to execute when the bot sends a message
*/
botMessage(handler) {
this.botMessageHandler = async (req) => {
try {
if (req.payload.subtype === undefined && req.payload.user === req.context.botUserId) {
await handler(req);
}
}
catch (e) {
console.error(`Failed to execute botMessageHandler listener: ${e.stack}`);
}
};
}
}
exports.Assistant = Assistant;
//# sourceMappingURL=assistant.js.map