If slack message is canceled send a message instead of fail and retry

This commit is contained in:
Wells Bunker 2025-09-25 15:05:36 -06:00
parent 3baabc1a62
commit d798b0f360
No known key found for this signature in database
GPG Key ID: DB16D6F2679B78FC
1 changed files with 91 additions and 2 deletions

View File

@ -383,7 +383,12 @@ export const slackAgentTask: ReturnType<
const maxRapidPolls = 20; // 20 attempts = 20 seconds total const maxRapidPolls = 20; // 20 attempts = 20 seconds total
let rapidPollCount = 0; let rapidPollCount = 0;
let isComplete = false; let isComplete = false;
let analystResult: { ok: boolean; output?: unknown; error?: unknown } | null = null; let analystResult: {
ok: boolean;
output?: unknown;
error?: unknown;
canceled?: boolean;
} | null = null;
while (rapidPollCount < maxRapidPolls && !hasStartedRunning && !isComplete) { while (rapidPollCount < maxRapidPolls && !hasStartedRunning && !isComplete) {
await wait.for({ seconds: rapidPollInterval / 1000 }); await wait.for({ seconds: rapidPollInterval / 1000 });
@ -491,6 +496,8 @@ export const slackAgentTask: ReturnType<
isComplete = true; isComplete = true;
if (run.status === 'COMPLETED') { if (run.status === 'COMPLETED') {
analystResult = { ok: true, output: run.output }; analystResult = { ok: true, output: run.output };
} else if (run.status === 'CANCELED') {
analystResult = { ok: false, canceled: true };
} else { } else {
analystResult = { ok: false, error: run.error || 'Task failed' }; analystResult = { ok: false, error: run.error || 'Task failed' };
} }
@ -605,10 +612,12 @@ export const slackAgentTask: ReturnType<
if (run.status === 'COMPLETED') { if (run.status === 'COMPLETED') {
isComplete = true; isComplete = true;
analystResult = { ok: true, output: run.output }; analystResult = { ok: true, output: run.output };
} else if (run.status === 'CANCELED') {
isComplete = true;
analystResult = { ok: false, canceled: true };
} else if ( } else if (
run.status === 'SYSTEM_FAILURE' || run.status === 'SYSTEM_FAILURE' ||
run.status === 'CRASHED' || run.status === 'CRASHED' ||
run.status === 'CANCELED' ||
run.status === 'TIMED_OUT' run.status === 'TIMED_OUT'
) { ) {
isComplete = true; isComplete = true;
@ -627,6 +636,86 @@ export const slackAgentTask: ReturnType<
analystResult = { ok: false, error: 'Task timed out' }; analystResult = { ok: false, error: 'Task timed out' };
} }
if (analystResult && !analystResult.ok && analystResult.canceled) {
logger.log('Analyst task was canceled mid-execution', {
runId: analystHandle.id,
});
try {
if (progressMessageTs) {
await messagingService.deleteMessage(
accessToken,
chatDetails.slackChannelId,
progressMessageTs
);
}
await removeReaction({
accessToken,
channelId: chatDetails.slackChannelId,
messageTs: mentionMessageTs,
emoji: 'hourglass_flowing_sand',
});
} catch (error) {
logger.warn(
'Error cleaning up initial message and reaction after canceled analyst task',
{
messageTs: progressMessageTs,
error: error instanceof Error ? error.message : 'Unknown error',
}
);
}
try {
const canceledMessage =
'Your request was canceled mid-execution. If this was a mistake, please ask your question again.';
const messageBlocks = [
{
type: 'section' as const,
text: {
type: 'plain_text' as const,
text: canceledMessage,
},
},
{
type: 'actions' as const,
elements: [
{
type: 'button' as const,
text: {
type: 'plain_text' as const,
text: 'Open in Buster',
emoji: false,
},
url: `${busterUrl}/app/chats/${payload.chatId}`,
},
],
},
];
const completeCanceledMessage = {
text: canceledMessage,
thread_ts: chatDetails.slackThreadTs,
blocks: messageBlocks,
};
await messagingService.sendMessage(
accessToken,
chatDetails.slackChannelId,
completeCanceledMessage
);
} catch (error) {
logger.warn('Failed to send canceled message to Slack', {
error: error instanceof Error ? error.message : 'Unknown error',
});
}
return {
success: false,
messageId: '',
triggerRunId: '',
};
}
// Handle analyst task result // Handle analyst task result
if (!analystResult || !analystResult.ok) { if (!analystResult || !analystResult.ok) {
// If analyst task failed, notify the user // If analyst task failed, notify the user