Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto acknowledge assistant events #2330

Open
wants to merge 4 commits into
base: autoack
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 66 additions & 48 deletions src/Assistant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
AllMessageEvents,
AssistantThreadsSetStatusResponse,
AssistantThreadsSetSuggestedPromptsResponse,
AssistantThreadsSetTitleResponse,
Expand All @@ -11,6 +12,7 @@
DefaultThreadContextStore,
} from './AssistantThreadContextStore';
import { AssistantInitializationError, AssistantMissingPropertyError } from './errors';
import { autoAcknowledge, isEventArgs, isMessageEventArgs, safelyAcknowledge } from './middleware/builtin';
import processMiddleware from './middleware/process';
import type { AllMiddlewareArgs, AnyMiddlewareArgs, Middleware, SayFn, SlackEventMiddlewareArgs } from './types';

Expand Down Expand Up @@ -92,9 +94,6 @@
export type AllAssistantMiddlewareArgs<T extends AssistantMiddlewareArgs = AssistantMiddlewareArgs> = T &
AllMiddlewareArgs;

/** Constants */
const ASSISTANT_PAYLOAD_TYPES = new Set(['assistant_thread_started', 'assistant_thread_context_changed', 'message']);

export class Assistant {
private threadContextStore: AssistantThreadContextStore;

Expand Down Expand Up @@ -129,34 +128,26 @@

public getMiddleware(): Middleware<AnyMiddlewareArgs> {
return async (args): Promise<void> => {
if (isAssistantEvent(args) && matchesConstraints(args)) {
return this.processEvent(args);
if (isAssistantThreadStartedEvent(args)) {
return this.processEvent(args, [autoAcknowledge, ...this.threadStarted]);
}
if (isAssistantThreadContextChangedEvent(args)) {
return this.processEvent(args, [autoAcknowledge, ...this.threadContextChanged]);
}
if (isUserMessageEventInAssistantThread(args)) {
return this.processEvent(args, [autoAcknowledge, ...this.userMessage]);
}
if (isOtherMessageSubEventInAssistantThread(args)) {
// ignore self message_changed, message_deleted, etc.
return await safelyAcknowledge(args);
}
return args.next();
};
}

private async processEvent(args: AllAssistantMiddlewareArgs): Promise<void> {
const { payload } = args;
private async processEvent(args: AllAssistantMiddlewareArgs, middlewares: AssistantMiddleware): Promise<void> {
const assistantArgs = enrichAssistantArgs(this.threadContextStore, args);
const assistantMiddleware = this.getAssistantMiddleware(payload);
return processAssistantMiddleware(assistantArgs, assistantMiddleware);
}

/**
* `getAssistantMiddleware()` returns the Assistant instance's middleware
*/
private getAssistantMiddleware(payload: AllAssistantMiddlewareArgs['payload']): AssistantMiddleware {
switch (payload.type) {
case 'assistant_thread_started':
return this.threadStarted;
case 'assistant_thread_context_changed':
return this.threadContextChanged;
case 'message':
return this.userMessage;
default:
return [];
}
return processAssistantMiddleware(assistantArgs, middlewares);
}
}

Expand Down Expand Up @@ -184,33 +175,60 @@
return preparedArgs;
}

/**
* `isAssistantEvent()` determines if incoming event is a supported
* Assistant event type.
*/
export function isAssistantEvent(args: AnyMiddlewareArgs): args is AllAssistantMiddlewareArgs {
return ASSISTANT_PAYLOAD_TYPES.has(args.payload.type);
export function isAssistantThreadStartedEvent(args: AnyMiddlewareArgs): args is AllAssistantMiddlewareArgs {
if (isEventArgs(args)) {
return args.payload.type === 'assistant_thread_started';
}
return false;
}

/**
* `matchesConstraints()` determines if the incoming event payload
* is related to the Assistant.
*/
export function matchesConstraints(args: AssistantMiddlewareArgs): args is AssistantMiddlewareArgs {
return args.payload.type === 'message' ? isAssistantMessage(args.payload) : true;
export function isAssistantThreadContextChangedEvent(args: AnyMiddlewareArgs): args is AllAssistantMiddlewareArgs {
if (isEventArgs(args)) {
return args.payload.type === 'assistant_thread_context_changed';
}
return false;
}

/**
* `isAssistantMessage()` evaluates if the message payload is associated
* with the Assistant container.
*/
export function isAssistantMessage(payload: AnyMiddlewareArgs['payload']): boolean {
const isThreadMessage = 'channel' in payload && 'thread_ts' in payload;
const inAssistantContainer =
'channel_type' in payload &&
payload.channel_type === 'im' &&
(!('subtype' in payload) || payload.subtype === 'file_share' || payload.subtype === undefined); // TODO: undefined subtype is a limitation of message event, needs fixing (see https://github.com/slackapi/node-slack-sdk/issues/1904)
return isThreadMessage && inAssistantContainer;
export function isMessageEventInAssistantThread(args: AnyMiddlewareArgs): boolean {
if (isMessageEventArgs(args)) {
return args.event.channel_type === 'im';
}
return false;
}

export function isUserMessageEventInAssistantThread(args: AnyMiddlewareArgs): args is AllAssistantMiddlewareArgs {
if (isMessageEventInAssistantThread(args)) {
return (
(!('subtype' in args.payload) || [undefined, 'file_share'].includes(args.payload.subtype)) &&
'thread_ts' in args.payload &&
args.payload.thread_ts !== undefined &&
!('bot_id' in args.payload)
);
}
return false;
}

export function isOtherMessageSubEventInAssistantThread(args: AnyMiddlewareArgs): boolean {
// message_changed, message_deleted etc.
if (isMessageEventInAssistantThread(args)) {
return (
!isUserMessageEventInAssistantThread(args) &&
(('message' in args.payload && isOtherMessageSubEvent(args.payload.message)) ||
('previous_message' in args.payload && isOtherMessageSubEvent(args.payload.previous_message)))
);
}
return false;
}

function isOtherMessageSubEvent(message: AllMessageEvents | (AllMessageEvents & { thread_ts: string })): boolean {
if ('thread_ts' in message) {
return true;
}
if ('subtype' in message) {
// TODO: add assistant_app_thread as a valid message event subtype
return (message.subtype as string) === 'assistant_app_thread';
}
return false;

Check warning on line 231 in src/Assistant.ts

View check run for this annotation

Codecov / codecov/patch

src/Assistant.ts#L228-L231

Added lines #L228 - L231 were not covered by tests
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ function isViewBody(
return 'view' in body && body.view !== undefined;
}

function isEventArgs(args: AnyMiddlewareArgs): args is SlackEventMiddlewareArgs {
export function isEventArgs(args: AnyMiddlewareArgs): args is SlackEventMiddlewareArgs {
return 'event' in args && args.event !== undefined;
}

function isMessageEventArgs(args: AnyMiddlewareArgs): args is SlackEventMiddlewareArgs<'message'> {
export function isMessageEventArgs(args: AnyMiddlewareArgs): args is SlackEventMiddlewareArgs<'message'> {
return isEventArgs(args) && 'message' in args;
}

Expand Down Expand Up @@ -131,10 +131,14 @@ export const onlyViewActions: Middleware<AnyMiddlewareArgs> = async (args) => {
* Middleware that auto acknowledges the request received
*/
export const autoAcknowledge: Middleware<AnyMiddlewareArgs> = async (args) => {
await safelyAcknowledge(args);
await args.next();
};

export const safelyAcknowledge: Middleware<AnyMiddlewareArgs> = async (args) => {
if ('ack' in args && args.ack !== undefined) {
await args.ack();
}
await args.next();
};

/**
Expand Down Expand Up @@ -318,6 +322,7 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
const { message } = args;
// Look for an event that is identified as a bot message from the same bot ID as this app, and return to skip
if (message.subtype === 'bot_message' && message.bot_id === botId) {
await safelyAcknowledge(args);
return;
}
}
Expand All @@ -332,6 +337,7 @@ export const ignoreSelf: Middleware<AnyMiddlewareArgs> = async (args) => {
args.event.user === botUserId &&
!eventsWhichShouldBeKept.includes(args.event.type)
) {
await safelyAcknowledge(args);
return;
}
}
Expand Down
Loading