fix: Notification {last_message} variable not populating (#399)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH = 250;
|
||||
const DEFAULT_NOTIFICATION_SUMMARY_THRESHOLD = 200;
|
||||
const DEFAULT_NOTIFICATION_SUMMARY_LENGTH = 100;
|
||||
|
||||
const resolvePositiveNumber = (value, fallback) => {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
||||
return fallback;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
export const truncateNotificationText = (text, maxLength = DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH) => {
|
||||
if (typeof text !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const safeMaxLength = resolvePositiveNumber(maxLength, DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH);
|
||||
if (text.length <= safeMaxLength) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return `${text.slice(0, safeMaxLength)}...`;
|
||||
};
|
||||
|
||||
export const prepareNotificationLastMessage = async ({ message, settings, summarize }) => {
|
||||
const originalMessage = typeof message === 'string' ? message : '';
|
||||
if (!originalMessage) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const shouldSummarize = settings?.summarizeLastMessage === true && typeof summarize === 'function';
|
||||
const summaryThreshold = resolvePositiveNumber(settings?.summaryThreshold, DEFAULT_NOTIFICATION_SUMMARY_THRESHOLD);
|
||||
const summaryLength = resolvePositiveNumber(settings?.summaryLength, DEFAULT_NOTIFICATION_SUMMARY_LENGTH);
|
||||
const maxLastMessageLength = resolvePositiveNumber(settings?.maxLastMessageLength, DEFAULT_NOTIFICATION_MESSAGE_MAX_LENGTH);
|
||||
|
||||
let messageForNotification = originalMessage;
|
||||
if (shouldSummarize && originalMessage.length > summaryThreshold) {
|
||||
try {
|
||||
const summary = await summarize(originalMessage, summaryLength);
|
||||
if (typeof summary === 'string' && summary.trim().length > 0) {
|
||||
messageForNotification = summary;
|
||||
}
|
||||
} catch {
|
||||
messageForNotification = originalMessage;
|
||||
}
|
||||
}
|
||||
|
||||
return truncateNotificationText(messageForNotification, maxLastMessageLength);
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
|
||||
import { prepareNotificationLastMessage, truncateNotificationText } from './notification-message.js';
|
||||
|
||||
describe('notification message helpers', () => {
|
||||
it('truncates oversized notification text', () => {
|
||||
expect(truncateNotificationText('abcdef', 3)).toBe('abc...');
|
||||
});
|
||||
|
||||
it('falls back to original message when summarization fails', async () => {
|
||||
const message = '0123456789';
|
||||
const summarize = async () => {
|
||||
throw new Error('summarization failed');
|
||||
};
|
||||
|
||||
const result = await prepareNotificationLastMessage({
|
||||
message,
|
||||
summarize,
|
||||
settings: {
|
||||
summarizeLastMessage: true,
|
||||
summaryThreshold: 5,
|
||||
summaryLength: 3,
|
||||
maxLastMessageLength: 4,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('0123...');
|
||||
});
|
||||
|
||||
it('falls back to original message when summary is empty', async () => {
|
||||
const result = await prepareNotificationLastMessage({
|
||||
message: '0123456789',
|
||||
summarize: async () => ' ',
|
||||
settings: {
|
||||
summarizeLastMessage: true,
|
||||
summaryThreshold: 5,
|
||||
summaryLength: 3,
|
||||
maxLastMessageLength: 4,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('0123...');
|
||||
});
|
||||
|
||||
it('uses summary when summarization succeeds', async () => {
|
||||
const result = await prepareNotificationLastMessage({
|
||||
message: '0123456789',
|
||||
summarize: async () => 'short summary',
|
||||
settings: {
|
||||
summarizeLastMessage: true,
|
||||
summaryThreshold: 5,
|
||||
summaryLength: 3,
|
||||
maxLastMessageLength: 100,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('short summary');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user