Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): chat asks once when you leave a card unanswered (#16394)

Hazem Adeld98eb08abe7e
diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/display-tools.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/display-tools.ts
index 1fcbcd87e13..5ee783311a8 100644
--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/display-tools.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/tools/display-tools.ts
@@ -1,7 +1,7 @@
import { isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'
import { tool, ToolExecutionOptions, ToolSet } from 'ai'
import { z } from 'zod'
-import { GateDecision, gateNoResponseMessage, normalizePieceName, questionTextSchema, questionTitleSchema, richOptionSchema } from './tool-primitives'
+import { GateDecision, gateNoResponseMessage, normalizePieceName, questionTextSchema, questionTitleSchema, richOptionSchema, stableStringify } from './tool-primitives'
export function createDisplayTools({ waitForApproval, displayToolTimeoutMs, onConnectionSelected, onConnectorReconnected, onGateOpened, accountAlreadyChosenFor, connectionChosenEarlierFor }: {
waitForApproval: (params: { gateId: string, timeoutMs?: number }) => Promise<GateDecision>
@@ -12,6 +12,8 @@ export function createDisplayTools({ waitForApproval, displayToolTimeoutMs, onCo
accountAlreadyChosenFor?: (pieceName: string) => boolean
connectionChosenEarlierFor?: (pieceName: string) => Promise<{ externalId: string, label: string } | null>
}): ToolSet {
+ const unansweredCards = new Set<string>()
+
function refuseIfAccountAlreadyChosen(input: Record<string, unknown>): { content: { type: string, text: string }[] } | undefined {
const piece = typeof input['piece'] === 'string' ? input['piece'] : ''
if (isNil(accountAlreadyChosenFor) || !accountAlreadyChosenFor(normalizePieceName(piece))) {
@@ -47,6 +49,11 @@ export function createDisplayTools({ waitForApproval, displayToolTimeoutMs, onCo
if (!isNil(refusal)) {
return refusal
}
+ const displayName = getDisplayName?.(input) ?? toolName
+ const cardKey = cardIdentity({ toolName, input })
+ if (unansweredCards.has(cardKey)) {
+ return { notShown: true, message: cardStillUnansweredMessage(displayName) }
+ }
if (onGateOpened) {
const fallbackName = typeof input['displayName'] === 'string' ? input['displayName'] : toolName
await tryCatch(() => onGateOpened({
@@ -58,7 +65,8 @@ export function createDisplayTools({ waitForApproval, displayToolTimeoutMs, onCo
}
const decision = await waitForApproval({ gateId: options.toolCallId, timeoutMs: displayToolTimeoutMs })
if (decision.outcome === 'timeout') {
- return { timedOut: true, message: gateNoResponseMessage(getDisplayName?.(input) ?? toolName) }
+ unansweredCards.add(cardKey)
+ return { timedOut: true, message: gateNoResponseMessage(displayName) }
}
if (decision.outcome !== 'approved') {
return { dismissed: true, message: typeof dismissMessage === 'function' ? dismissMessage(input) : dismissMessage }
@@ -249,3 +257,12 @@ export function createDisplayTools({ waitForApproval, displayToolTimeoutMs, onCo
}
}
+function cardIdentity({ toolName, input }: { toolName: string, input: Record<string, unknown> }): string {
+ const piece = input['piece']
+ const subject = typeof piece === 'string' ? normalizePieceName(piece) : input['connectorUuid'] ?? input['questions'] ?? null
+ return `${toolName}:${stableStringify(subject)}`
+}
+
+function cardStillUnansweredMessage(displayName: string): string {
+ return `The ${displayName} card was already shown in this turn and the user has not answered it, so it was not shown again. Do not ask for it again now. End your reply with one short line saying what you need from them, and continue when they answer.`
+}
diff --git a/packages/server/worker/test/lib/execute/jobs/ee/agent/agent-worker-tools.test.ts b/packages/server/worker/test/lib/execute/jobs/ee/agent/agent-worker-tools.test.ts
index c1c07490c59..a7a046439d0 100644
--- a/packages/server/worker/test/lib/execute/jobs/ee/agent/agent-worker-tools.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/ee/agent/agent-worker-tools.test.ts
@@ -778,6 +778,27 @@ describe('an agent does not offer to connect an account its author already chose
expect(gatesOpened).toEqual(['ap_show_connection_picker'])
})
+ it('does not show a card again in the same turn after the user left it unanswered', async () => {
+ const waitForApproval = vi.fn(async () => ({ outcome: 'timeout' as const }))
+ const tools = agentWorkerTools.createDisplayTools({ waitForApproval, displayToolTimeoutMs: 1000 })
+
+ await tools['ap_show_connection_picker'].execute({ piece: 'gmail', displayName: 'Gmail' }, { toolCallId: 'call-1' })
+ const again = await tools['ap_show_connection_picker'].execute({ piece: '@activepieces/piece-gmail', displayName: 'My Gmail' }, { toolCallId: 'call-2' })
+
+ expect(waitForApproval).toHaveBeenCalledTimes(1)
+ expect(again).toMatchObject({ notShown: true })
+ })
+
+ it('still shows a different card after another one went unanswered', async () => {
+ const waitForApproval = vi.fn(async () => ({ outcome: 'timeout' as const }))
+ const tools = agentWorkerTools.createDisplayTools({ waitForApproval, displayToolTimeoutMs: 1000 })
+
+ await tools['ap_show_connection_picker'].execute({ piece: 'gmail', displayName: 'Gmail' }, { toolCallId: 'call-1' })
+ await tools['ap_show_connection_picker'].execute({ piece: 'slack', displayName: 'Slack' }, { toolCallId: 'call-2' })
+
+ expect(waitForApproval).toHaveBeenCalledTimes(2)
+ })
+
it.each(['ap_show_connection_picker', 'ap_show_connection_required'])('%s rejects an empty piece, so no gate opens for a card that cannot render', async (toolName) => {
const { tools } = pickerTools({})
const result = await asSchema(tools[toolName].inputSchema).validate?.({ piece: '', displayName: 'Gmail' })
diff --git a/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx b/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
index 510e5b76721..9db22167f65 100644
--- a/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
+++ b/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
@@ -578,6 +578,7 @@ function DisplayToolCard({
? (parsedOutput.data as Record<string, unknown>)
: undefined;
const toolCallId = chatPartUtils.getToolCallId(part);
+ if (toolOutput?.['notShown'] === true) return null;
switch (toolName) {
case 'ap_show_connection_required':