Skip to content

The open alternative to Salesforce, designed for AI.

feat(slack): branch the assistant failure reply on the failure kind (#25484)

Abdul Rahmanac412ddda1b9
diff --git a/packages/twenty-apps/public/slack/src/__tests__/slack-assistant-worker.integration-test.ts b/packages/twenty-apps/public/slack/src/__tests__/slack-assistant-worker.integration-test.ts
index e6c1fdf8446..035766a7b41 100644
--- a/packages/twenty-apps/public/slack/src/__tests__/slack-assistant-worker.integration-test.ts
+++ b/packages/twenty-apps/public/slack/src/__tests__/slack-assistant-worker.integration-test.ts
@@ -3,6 +3,10 @@ import { afterEach, describe, expect, it } from 'vitest';
import { createSlackMessageTimestampSequence } from 'src/__tests__/utils/create-slack-message-timestamp-sequence.util';
import { requireDefinedOrThrow } from 'src/__tests__/utils/require-defined-or-throw.util';
import { setupSlackIntegrationTest } from 'src/__tests__/utils/setup-slack-integration-test.util';
+import { SLACK_ASSISTANT_DEADLINE_ERROR } from 'src/logic-functions/constants/slack-assistant-deadline-error';
+import { SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-deadline-failure-text';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR } from 'src/logic-functions/constants/slack-assistant-empty-response-error';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-empty-response-failure-text';
import { SLACK_ASSISTANT_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-failure-text';
import { SLACK_ASSISTANT_FEEDBACK_ACTION_ID } from 'src/logic-functions/constants/slack-assistant-feedback-action-id';
import { SLACK_ASSISTANT_REQUEST_STATUS } from 'src/logic-functions/constants/slack-assistant-request-status';
@@ -336,6 +340,92 @@ describe('Slack assistant worker', () => {
);
});
+ it('should tell the member to narrow the request when the answer deadline passes', async () => {
+ slack.addChannel({ id: CHANNEL_ID, name: 'sales' });
+ const slackMessageTimestamp = nextMessageTimestamp();
+
+ appRuntime.setAgentResult({
+ success: false,
+ result: null,
+ error: SLACK_ASSISTANT_DEADLINE_ERROR,
+ });
+
+ const request = await createRequestRecord({
+ slackChannelId: CHANNEL_ID,
+ slackMessageTimestamp,
+ requestText: 'summarize every opportunity we opened this year',
+ });
+
+ const result = await slackAssistantWorkerHandler(
+ buildRequestCreatedEvent({
+ ...request,
+ slackChannelId: CHANNEL_ID,
+ slackMessageTimestamp,
+ requestText: 'summarize every opportunity we opened this year',
+ }),
+ );
+
+ expect(result).toEqual({
+ failed: true,
+ reason: SLACK_ASSISTANT_DEADLINE_ERROR,
+ });
+ expect(slack.messagesIn(CHANNEL_ID)).toEqual([
+ expect.objectContaining({
+ text: SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT,
+ threadTimestamp: slackMessageTimestamp,
+ }),
+ ]);
+ await expect(readRequest(request.id)).resolves.toEqual(
+ expect.objectContaining({
+ status: SLACK_ASSISTANT_REQUEST_STATUS.FAILED,
+ errorMessage: SLACK_ASSISTANT_DEADLINE_ERROR,
+ }),
+ );
+ });
+
+ it('should invite the member to ask again when the agent answers with nothing', async () => {
+ slack.addChannel({ id: CHANNEL_ID, name: 'sales' });
+ const slackMessageTimestamp = nextMessageTimestamp();
+
+ appRuntime.setAgentResult({
+ success: true,
+ result: null,
+ error: null,
+ });
+
+ const request = await createRequestRecord({
+ slackChannelId: CHANNEL_ID,
+ slackMessageTimestamp,
+ requestText: 'what changed on Acme this week?',
+ });
+
+ const result = await slackAssistantWorkerHandler(
+ buildRequestCreatedEvent({
+ ...request,
+ slackChannelId: CHANNEL_ID,
+ slackMessageTimestamp,
+ requestText: 'what changed on Acme this week?',
+ }),
+ );
+
+ expect(result).toEqual({
+ failed: true,
+ reason: SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR,
+ });
+ expect(slack.messagesIn(CHANNEL_ID)).toEqual([
+ expect.objectContaining({
+ text: SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT,
+ threadTimestamp: slackMessageTimestamp,
+ }),
+ ]);
+ await expect(readRequest(request.id)).resolves.toEqual(
+ expect.objectContaining({
+ status: SLACK_ASSISTANT_REQUEST_STATUS.FAILED,
+ errorMessage: SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR,
+ }),
+ );
+ });
+
it('should fail the request when Slack refuses to deliver the answer', async () => {
slack.addChannel({ id: CHANNEL_ID, name: 'sales' });
slack.failNextCall('chat.postMessage', 'channel_not_found');
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-deadline-failure-text.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-deadline-failure-text.ts
new file mode 100644
index 00000000000..7a3e5431454
--- /dev/null
+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-deadline-failure-text.ts
@@ -0,0 +1,2 @@
+export const SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT =
+ 'That request took longer than I have to answer it. Narrowing it usually works: fewer records, or one question at a time.';
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-error.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-error.ts
new file mode 100644
index 00000000000..8b98f2f83dc
--- /dev/null
+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-error.ts
@@ -0,0 +1,2 @@
+export const SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR =
+ 'Agent returned an empty response';
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-failure-text.ts b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-failure-text.ts
new file mode 100644
index 00000000000..4a8dd0c34c0
--- /dev/null
+++ b/packages/twenty-apps/public/slack/src/logic-functions/constants/slack-assistant-empty-response-failure-text.ts
@@ -0,0 +1,2 @@
+export const SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT =
+ "I finished without an answer to that one. Ask me again, or rephrase it and I'll have another go.";
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/slack-assistant-worker.ts b/packages/twenty-apps/public/slack/src/logic-functions/slack-assistant-worker.ts
index 35a7f5bb364..def15fd652a 100644
--- a/packages/twenty-apps/public/slack/src/logic-functions/slack-assistant-worker.ts
+++ b/packages/twenty-apps/public/slack/src/logic-functions/slack-assistant-worker.ts
@@ -11,6 +11,7 @@ import {
SLACK_ASSISTANT_WORKER_UNIVERSAL_IDENTIFIER,
} from 'src/constants/universal-identifiers';
import { SLACK_ASSISTANT_AGENT_BUDGET_SECONDS } from 'src/logic-functions/constants/slack-assistant-agent-budget-seconds';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR } from 'src/logic-functions/constants/slack-assistant-empty-response-error';
import { SLACK_ASSISTANT_REQUEST_STATUS } from 'src/logic-functions/constants/slack-assistant-request-status';
import { SLACK_ASSISTANT_WORKER_TIMEOUT_SECONDS } from 'src/logic-functions/constants/slack-assistant-worker-timeout-seconds';
import { SLACK_MARKDOWN_BLOCK_MAX_LENGTH } from 'src/logic-functions/constants/slack-markdown-block-max-length';
@@ -156,7 +157,7 @@ export const slackAssistantWorkerHandler = async (
if (responseText === undefined) {
return await finishSlackAssistantRequestWithFailure({
...failureContext,
- errorMessage: 'Agent returned an empty response',
+ errorMessage: SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR,
});
}
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/get-slack-assistant-failure-text.test.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/get-slack-assistant-failure-text.test.ts
new file mode 100644
index 00000000000..9949ef845a3
--- /dev/null
+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/__tests__/get-slack-assistant-failure-text.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from 'vitest';
+
+import { SLACK_ASSISTANT_DEADLINE_ERROR } from 'src/logic-functions/constants/slack-assistant-deadline-error';
+import { SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-deadline-failure-text';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR } from 'src/logic-functions/constants/slack-assistant-empty-response-error';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-empty-response-failure-text';
+import { SLACK_ASSISTANT_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-failure-text';
+import { getSlackAssistantFailureText } from 'src/logic-functions/utils/get-slack-assistant-failure-text';
+
+describe('getSlackAssistantFailureText', () => {
+ it('should tell the member to narrow the request when the answer deadline passed', () => {
+ expect(getSlackAssistantFailureText(SLACK_ASSISTANT_DEADLINE_ERROR)).toBe(
+ SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT,
+ );
+ });
+
+ it('should invite the member to ask again when the agent answered with nothing', () => {
+ expect(
+ getSlackAssistantFailureText(SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR),
+ ).toBe(SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT);
+ });
+
+ it('should point at an admin for a failure the member cannot act on', () => {
+ expect(getSlackAssistantFailureText('Agent is not available')).toBe(
+ SLACK_ASSISTANT_FAILURE_TEXT,
+ );
+ });
+
+ it('should never surface the underlying error to the member', () => {
+ const errorMessage =
+ 'Could not deliver Slack answer: channel_not_found at postMessage()';
+
+ expect(getSlackAssistantFailureText(errorMessage)).toBe(
+ SLACK_ASSISTANT_FAILURE_TEXT,
+ );
+ });
+});
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/finish-slack-assistant-request-with-failure.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/finish-slack-assistant-request-with-failure.ts
index b721d594385..9abf9801f42 100644
--- a/packages/twenty-apps/public/slack/src/logic-functions/utils/finish-slack-assistant-request-with-failure.ts
+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/finish-slack-assistant-request-with-failure.ts
@@ -1,9 +1,9 @@
import { type CoreApiClient } from 'twenty-client-sdk/core';
-import { SLACK_ASSISTANT_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-failure-text';
import { SLACK_ASSISTANT_REQUEST_STATUS } from 'src/logic-functions/constants/slack-assistant-request-status';
import { updateSlackAssistantRequest } from 'src/logic-functions/data/update-slack-assistant-request';
import { slackPostMessageHandler } from 'src/logic-functions/handlers/slack-post-message-handler';
+import { getSlackAssistantFailureText } from 'src/logic-functions/utils/get-slack-assistant-failure-text';
type SlackAssistantRequestFailureResult = {
failed: true;
@@ -25,7 +25,7 @@ export const finishSlackAssistantRequestWithFailure = async ({
}): Promise<SlackAssistantRequestFailureResult> => {
await slackPostMessageHandler({
slackChannelId,
- messageText: SLACK_ASSISTANT_FAILURE_TEXT,
+ messageText: getSlackAssistantFailureText(errorMessage),
parentMessageTimestamp,
});
diff --git a/packages/twenty-apps/public/slack/src/logic-functions/utils/get-slack-assistant-failure-text.ts b/packages/twenty-apps/public/slack/src/logic-functions/utils/get-slack-assistant-failure-text.ts
new file mode 100644
index 00000000000..9ba1abe76fc
--- /dev/null
+++ b/packages/twenty-apps/public/slack/src/logic-functions/utils/get-slack-assistant-failure-text.ts
@@ -0,0 +1,18 @@
+import { SLACK_ASSISTANT_DEADLINE_ERROR } from 'src/logic-functions/constants/slack-assistant-deadline-error';
+import { SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-deadline-failure-text';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR } from 'src/logic-functions/constants/slack-assistant-empty-response-error';
+import { SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-empty-response-failure-text';
+import { SLACK_ASSISTANT_FAILURE_TEXT } from 'src/logic-functions/constants/slack-assistant-failure-text';
+
+// Unmapped failures stay generic so no internal error text reaches Slack.
+export const getSlackAssistantFailureText = (errorMessage: string): string => {
+ if (errorMessage === SLACK_ASSISTANT_DEADLINE_ERROR) {
+ return SLACK_ASSISTANT_DEADLINE_FAILURE_TEXT;
+ }
+
+ if (errorMessage === SLACK_ASSISTANT_EMPTY_RESPONSE_ERROR) {
+ return SLACK_ASSISTANT_EMPTY_RESPONSE_FAILURE_TEXT;
+ }
+
+ return SLACK_ASSISTANT_FAILURE_TEXT;
+};