mirror of https://github.com/buster-so/buster.git
Refactor message post-processing task to improve error handling and logging. Updated database update logic to include error logging and added type definitions for branch results. Adjusted integration test with a new message ID.
This commit is contained in:
parent
3b01e5727b
commit
51c85c4384
|
@ -125,7 +125,7 @@ describe.skipIf(skipIntegrationTests)('messagePostProcessingTask integration', (
|
||||||
|
|
||||||
it('should handle message with no conversation history', async () => {
|
it('should handle message with no conversation history', async () => {
|
||||||
// Use prepopulated message ID
|
// Use prepopulated message ID
|
||||||
const messageId = 'a3206f20-35d1-4a6c-84a7-48f8f222c39f';
|
const messageId = '203744bd-439f-4b3c-9ea2-ddfe243c5afe';
|
||||||
|
|
||||||
// Execute task
|
// Execute task
|
||||||
const result = await tasks.triggerAndPoll<typeof messagePostProcessingTask>(
|
const result = await tasks.triggerAndPoll<typeof messagePostProcessingTask>(
|
||||||
|
|
|
@ -180,7 +180,14 @@ export const messagePostProcessingTask: ReturnType<
|
||||||
|
|
||||||
// Handle branch results - the result will have one of the branch step IDs as a key
|
// Handle branch results - the result will have one of the branch step IDs as a key
|
||||||
let validatedOutput: PostProcessingWorkflowOutput;
|
let validatedOutput: PostProcessingWorkflowOutput;
|
||||||
const branchResult = workflowResult.result as any; // Type assertion needed for branch results
|
|
||||||
|
// Define the expected shape of branch results
|
||||||
|
type BranchResult = {
|
||||||
|
'format-follow-up-message'?: PostProcessingWorkflowOutput;
|
||||||
|
'format-initial-message'?: PostProcessingWorkflowOutput;
|
||||||
|
};
|
||||||
|
|
||||||
|
const branchResult = workflowResult.result as BranchResult;
|
||||||
|
|
||||||
if ('format-follow-up-message' in branchResult && branchResult['format-follow-up-message']) {
|
if ('format-follow-up-message' in branchResult && branchResult['format-follow-up-message']) {
|
||||||
validatedOutput = branchResult['format-follow-up-message'] as PostProcessingWorkflowOutput;
|
validatedOutput = branchResult['format-follow-up-message'] as PostProcessingWorkflowOutput;
|
||||||
|
@ -195,6 +202,11 @@ export const messagePostProcessingTask: ReturnType<
|
||||||
resultKeys: Object.keys(branchResult),
|
resultKeys: Object.keys(branchResult),
|
||||||
result: branchResult,
|
result: branchResult,
|
||||||
});
|
});
|
||||||
|
console.error('Unexpected workflow result structure:', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
resultKeys: Object.keys(branchResult),
|
||||||
|
result: branchResult,
|
||||||
|
});
|
||||||
throw new Error('Post-processing workflow returned unexpected result structure');
|
throw new Error('Post-processing workflow returned unexpected result structure');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,53 +226,93 @@ export const messagePostProcessingTask: ReturnType<
|
||||||
messageId: payload.messageId,
|
messageId: payload.messageId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const db = getDb();
|
|
||||||
|
|
||||||
const dbData = extractDbFields(validatedOutput, messageContext.userName);
|
const dbData = extractDbFields(validatedOutput, messageContext.userName);
|
||||||
await db
|
|
||||||
.update(messages)
|
try {
|
||||||
.set({
|
const db = getDb();
|
||||||
postProcessingMessage: dbData,
|
await db
|
||||||
updatedAt: new Date().toISOString(),
|
.update(messages)
|
||||||
})
|
.set({
|
||||||
.where(eq(messages.id, payload.messageId));
|
postProcessingMessage: dbData,
|
||||||
|
updatedAt: new Date().toISOString(),
|
||||||
|
})
|
||||||
|
.where(eq(messages.id, payload.messageId));
|
||||||
|
|
||||||
|
logger.log('Database update successful', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
});
|
||||||
|
} catch (dbError) {
|
||||||
|
const errorMessage = dbError instanceof Error ? dbError.message : 'Unknown database error';
|
||||||
|
logger.error('Failed to update database with post-processing result', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
error: errorMessage,
|
||||||
|
});
|
||||||
|
console.error('Failed to update database with post-processing result:', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
error: errorMessage,
|
||||||
|
stack: dbError instanceof Error ? dbError.stack : undefined,
|
||||||
|
});
|
||||||
|
// Throw the error to ensure the task fails when database update fails
|
||||||
|
throw new Error(`Database update failed: ${errorMessage}`);
|
||||||
|
}
|
||||||
|
|
||||||
// Step 6: Send Slack notification if conditions are met
|
// Step 6: Send Slack notification if conditions are met
|
||||||
logger.log('Checking Slack notification conditions', {
|
let slackNotificationSent = false;
|
||||||
messageId: payload.messageId,
|
|
||||||
organizationId: messageContext.organizationId,
|
|
||||||
summaryTitle: dbData.summaryTitle,
|
|
||||||
summaryMessage: dbData.summaryMessage,
|
|
||||||
formattedMessage: dbData.formattedMessage,
|
|
||||||
toolCalled: dbData.toolCalled,
|
|
||||||
});
|
|
||||||
|
|
||||||
const slackResult = await sendSlackNotification({
|
try {
|
||||||
organizationId: messageContext.organizationId,
|
logger.log('Checking Slack notification conditions', {
|
||||||
userName: messageContext.userName,
|
|
||||||
summaryTitle: dbData.summaryTitle,
|
|
||||||
summaryMessage: dbData.summaryMessage,
|
|
||||||
formattedMessage: dbData.formattedMessage,
|
|
||||||
toolCalled: dbData.toolCalled,
|
|
||||||
message: dbData.message,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (slackResult.sent) {
|
|
||||||
logger.log('Slack notification sent successfully', {
|
|
||||||
messageId: payload.messageId,
|
messageId: payload.messageId,
|
||||||
organizationId: messageContext.organizationId,
|
organizationId: messageContext.organizationId,
|
||||||
|
summaryTitle: dbData.summaryTitle,
|
||||||
|
summaryMessage: dbData.summaryMessage,
|
||||||
|
formattedMessage: dbData.formattedMessage,
|
||||||
|
toolCalled: dbData.toolCalled,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
logger.log('Slack notification not sent', {
|
const slackResult = await sendSlackNotification({
|
||||||
|
organizationId: messageContext.organizationId,
|
||||||
|
userName: messageContext.userName,
|
||||||
|
summaryTitle: dbData.summaryTitle,
|
||||||
|
summaryMessage: dbData.summaryMessage,
|
||||||
|
formattedMessage: dbData.formattedMessage,
|
||||||
|
toolCalled: dbData.toolCalled,
|
||||||
|
message: dbData.message,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (slackResult.sent) {
|
||||||
|
slackNotificationSent = true;
|
||||||
|
logger.log('Slack notification sent successfully', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
organizationId: messageContext.organizationId,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.log('Slack notification not sent', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
organizationId: messageContext.organizationId,
|
||||||
|
reason: slackResult.error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (slackError) {
|
||||||
|
const errorMessage =
|
||||||
|
slackError instanceof Error ? slackError.message : 'Unknown Slack error';
|
||||||
|
logger.error('Failed to send Slack notification', {
|
||||||
messageId: payload.messageId,
|
messageId: payload.messageId,
|
||||||
organizationId: messageContext.organizationId,
|
organizationId: messageContext.organizationId,
|
||||||
reason: slackResult.error,
|
error: errorMessage,
|
||||||
});
|
});
|
||||||
|
console.error('Failed to send Slack notification:', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
organizationId: messageContext.organizationId,
|
||||||
|
error: errorMessage,
|
||||||
|
stack: slackError instanceof Error ? slackError.stack : undefined,
|
||||||
|
});
|
||||||
|
// Don't throw - this is a non-critical error
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log('Message post-processing completed successfully', {
|
logger.log('Message post-processing completed successfully', {
|
||||||
messageId: payload.messageId,
|
messageId: payload.messageId,
|
||||||
executionTimeMs: Date.now() - startTime,
|
executionTimeMs: Date.now() - startTime,
|
||||||
|
slackNotificationSent,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait 500ms to allow Braintrust to clean up its trace before completing
|
// Wait 500ms to allow Braintrust to clean up its trace before completing
|
||||||
|
@ -285,6 +337,13 @@ export const messagePostProcessingTask: ReturnType<
|
||||||
executionTimeMs: Date.now() - startTime,
|
executionTimeMs: Date.now() - startTime,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.error('Post-processing task execution failed:', {
|
||||||
|
messageId: payload.messageId,
|
||||||
|
error: errorMessage,
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
executionTimeMs: Date.now() - startTime,
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
messageId: payload.messageId,
|
messageId: payload.messageId,
|
||||||
|
|
Loading…
Reference in New Issue