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: remove slack new chat message #38

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/bright-garlics-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'slack-example': patch
'@callstack/byorg-slack': patch
---

Added useful plugins for normalizing threads (Slack started adding "New chat" message). Updated example slack app.
3 changes: 2 additions & 1 deletion examples/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelChatModelAdapter, createApp } from '@callstack/byorg-core';
import { createOpenAI } from '@ai-sdk/openai';
import { logger, requireEnv } from '@callstack/byorg-utils';
import { createSlackApp } from '@callstack/byorg-slack';
import { createSlackApp, slackThreadNormalizerPlugin } from '@callstack/byorg-slack';

const LANGUAGE_MODEL = 'gpt-4o-2024-11-20';
const API_KEY = requireEnv('OPENAI_API_KEY');
Expand All @@ -27,6 +27,7 @@ const SYSTEM_PROMPT = 'Your name is Byorg. You are a helpful AI Assistant.';
const app = createApp({
chatModel,
systemPrompt: SYSTEM_PROMPT,
plugins: [slackThreadNormalizerPlugin],
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
});

const slack = createSlackApp({
Expand Down
24 changes: 24 additions & 0 deletions packages/slack/src/__tests__/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createApp, createMockChatModel, Message } from '@callstack/byorg-core';
import { describe, expect, it, vitest } from 'vitest';
import { slackThreadNormalizerPlugin } from '../plugins/thread-normalization.js';

describe('threadNormalizer', () => {
it('should remove slack "New chat" initial message', async () => {
const messages: Message[] = [
{ role: 'user', content: 'New chat\n' },
{ role: 'user', content: 'Hello!' },
];
const baseModel = createMockChatModel({ delay: 0, seed: 3 });

const modelSpy = vitest.spyOn(baseModel, 'generateResponse');

const app = createApp({
chatModel: baseModel,
plugins: [slackThreadNormalizerPlugin],
});

await app.processMessages(messages);

expect(modelSpy.mock.calls[0][0]['messages']).toEqual([{ role: 'user', content: 'Hello!' }]);
});
});
1 change: 1 addition & 0 deletions packages/slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export {
slackEntityResolverPlugin,
extractUserMentionsFromMessage,
} from './plugins/entity-resolver.js';
export { slackThreadNormalizerPlugin } from './plugins/thread-normalization.js';
20 changes: 20 additions & 0 deletions packages/slack/src/plugins/thread-normalization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApplicationPlugin, MessageResponse } from '@callstack/byorg-core';
import { logger } from '@callstack/byorg-utils';

/**
* Slack plugin for normalizing messages coming from Slack [AI apps](https://api.slack.com/docs/apps/ai).

Check failure on line 5 in packages/slack/src/plugins/thread-normalization.ts

View workflow job for this annotation

GitHub Actions / Validate

Delete `·`
*/
export const slackThreadNormalizerPlugin: ApplicationPlugin = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add /** xx */ comment to explain the purpose of this plugin. Also update docs to mention in.

mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
name: 'slack-thread-normalizer',
middleware: async (context, next): Promise<MessageResponse> => {
const { messages } = context;

if (messages[0].content === 'New chat\n' && messages[1]?.role === 'user') {
context.messages = messages.slice(1);
logger.debug('Removed Slack "New Chat" message');
}

// Continue middleware chain
return await next();
},
};
Loading