Skip to content

Commit

Permalink
fix: remove slack new chat message, add context logger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Q1w1N committed Dec 16, 2024
1 parent 4463e11 commit 4211991
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
5 changes: 3 additions & 2 deletions 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 { VercelChatModelAdapter, contextLoggerBuilder, 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, contextLoggerBuilder(['messages'])],
});

const slack = createSlackApp({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export { VercelChatModelAdapter } from './ai/vercel.js';

export type { Command, CommandsPluginConfig } from './plugins/commands.js';
export { createCommandsPlugin } from './plugins/commands.js';
export { loggingPlugin } from './plugins/logging.js';
export { loggingPlugin, contextLoggerBuilder } from './plugins/logging.js';

export type { ApplicationTool } from './tools.js';

Expand Down
28 changes: 27 additions & 1 deletion packages/core/src/plugins/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApplicationPlugin, MessageResponse } from '../index.js';
import { inspect } from 'util';
import { logger } from '@callstack/byorg-utils';
import { ApplicationPlugin, MessageResponse, RequestContext } from '../index.js';

const getFormattedNow = () => new Date().toISOString();

Expand All @@ -23,3 +25,27 @@ export const loggingPlugin: ApplicationPlugin = {
}
},
};

export const contextLoggerBuilder = (fieldsToLog: (keyof RequestContext)[]): ApplicationPlugin => {
return {
name: 'context-logger',
middleware: (context, next): Promise<MessageResponse> => {
const toLog: Record<string, unknown> = {};

for (const field of fieldsToLog) {
if (field in context) {
toLog[field] = context[field];
} else {
logger.debug(`No ${field} in context.`);
}
}

if (Object.keys(toLog).length > 0) {
console.log(inspect(toLog, false, null, true));
}

// Continue middleware chain
return next();
},
};
};
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';
17 changes: 17 additions & 0 deletions packages/slack/src/plugins/thread-normalization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApplicationPlugin, MessageResponse } from '@callstack/byorg-core';
import { logger } from '@callstack/byorg-utils';

export const slackThreadNormalizerPlugin: ApplicationPlugin = {
name: 'slack-thread-normalizer-resolver',
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();
},
};

0 comments on commit 4211991

Please sign in to comment.