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

chore: add plugin for logging context #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions examples/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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, slackThreadNormalizerPlugin } from '@callstack/byorg-slack';
Expand Down Expand Up @@ -28,7 +28,12 @@ const app = createApp({
chatModel,
systemPrompt: SYSTEM_PROMPT,
// Normalize messages coming from Slack AI apps
plugins: [slackThreadNormalizerPlugin],
plugins: [
contextLoggerBuilder([]),
contextLoggerBuilder(['messages']),
Copy link
Member

Choose a reason for hiding this comment

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

just put it once in the example

slackThreadNormalizerPlugin,
contextLoggerBuilder(),
],
});

const slack = createSlackApp({
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/plugins/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ export const loggingPlugin: ApplicationPlugin = {
},
};

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

if (!fieldsToLog || fieldsToLog.length === 0) {
logger.info(inspect(context, false, null, true));
return next();
}

for (const field of fieldsToLog) {
if (field in context) {
toLog[field] = context[field];
Expand All @@ -41,7 +46,7 @@ export const contextLoggerBuilder = (fieldsToLog: (keyof RequestContext)[]): App
}

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

// Continue middleware chain
Expand Down
Loading