Skip to content

The open alternative to Salesforce, designed for AI.

Give Ask AI the calling application in its tool context (#28678)

Paul Rastoin2fb33f17eeb1
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
index f885b10e667..d641b6f1d24 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
@@ -33,6 +33,8 @@ import {
} from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-turn.service';
import { guideUncallableToolCallsToMetaTool } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/guide-uncallable-tool-calls-to-meta-tool.util';
import { replaceUnsupportedFileParts } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/replace-unsupported-file-parts.util';
+import { fromWorkspaceEntityToFlat } from 'src/engine/core-modules/workspace/utils/from-workspace-entity-to-flat.util';
+import { resolveAgentRunCallingApplication } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/resolve-agent-run-calling-application.util';
import { type BrowsingContextType } from 'src/engine/metadata-modules/ai/ai-agent/types/browsing-context.type';
import { AGENT_CHAT_KEEPALIVE_INTERVAL_MS } from 'src/engine/metadata-modules/ai/ai-chat/constants/agent-chat-keepalive-interval-ms.constant';
import { AI_CHAT_EXCLUDED_TOOL_NAMES } from 'src/engine/metadata-modules/ai/ai-chat/constants/ai-chat-excluded-tool-names.const';
@@ -168,6 +170,11 @@ export class ChatExecutionService {
locale,
onCodeExecutionUpdate,
...authorization,
+ // The chat auth context only carries the workspace id, which can't tell the custom application apart
+ application: resolveAgentRunCallingApplication({
+ ...authorization.authContext,
+ workspace: fromWorkspaceEntityToFlat(workspace),
+ }),
resolveExecutionContext,
};
diff --git a/packages/twenty-server/test/integration/ai/suites/ask-ai-application-token-tools.integration-spec.ts b/packages/twenty-server/test/integration/ai/suites/ask-ai-application-token-tools.integration-spec.ts
new file mode 100644
index 00000000000..cf7fa14e46b
--- /dev/null
+++ b/packages/twenty-server/test/integration/ai/suites/ask-ai-application-token-tools.integration-spec.ts
@@ -0,0 +1,187 @@
+import { randomUUID } from 'node:crypto';
+
+import gql from 'graphql-tag';
+import { TEST_AI_MODEL_ID } from 'test/integration/constants/test-ai-model-ids.constants';
+import { cleanupApplicationAndAppRegistration } from 'test/integration/metadata/suites/application/utils/cleanup-application-and-app-registration.util';
+import {
+ type ApplicationWithVariable,
+ setupApplicationWithVariable,
+} from 'test/integration/metadata/suites/application/utils/setup-application-with-variable.util';
+import { makeMetadataApiRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
+import { destroyAgentChatThread } from 'test/integration/utils/destroy-agent-chat-thread.util';
+import { expectEventually } from 'test/integration/utils/expect-eventually.util';
+import { generateAppleAdminApplicationTokenPair } from 'test/integration/utils/generate-apple-admin-application-token-pair.util';
+import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';
+import { createScriptedLanguageModel } from 'test/utils/create-scripted-language-model.util';
+import { SystemPermissionFlag } from 'twenty-shared/constants';
+
+import {
+ type AiModelRegistryService,
+ type RegisteredAiModel,
+} from 'src/engine/metadata-modules/ai/ai-models/services/ai-model-registry.service';
+
+const WORKFLOW_TOOL_NAME = 'list_workflows';
+const CHAT_TURN_TIMEOUT_MS = 60_000;
+
+const USAGE = {
+ inputTokens: {
+ total: 1,
+ noCache: 1,
+ cacheRead: undefined,
+ cacheWrite: undefined,
+ },
+ outputTokens: { total: 1, text: 1, reasoning: undefined },
+};
+
+// The model answers at once, so the test sees what the turn offers it before any tool runs
+const mockAnsweringModel = (): string[] => {
+ const offeredToolContexts: string[] = [];
+ const model = createScriptedLanguageModel((options) => {
+ offeredToolContexts.push(
+ [
+ ...(options.tools ?? []).map((tool) => tool.name),
+ ...options.prompt.flatMap((message) =>
+ message.role === 'system' ? [message.content] : [],
+ ),
+ ].join('\n'),
+ );
+
+ return {
+ content: [{ type: 'text', text: 'Done' }],
+ finishReason: { unified: 'stop', raw: undefined },
+ usage: USAGE,
+ };
+ });
+ const aiModelRegistryService =
+ getAppProviderByClassName<AiModelRegistryService>('AiModelRegistryService');
+
+ jest.spyOn(aiModelRegistryService, 'resolveModelForAgent').mockReturnValue({
+ modelId: TEST_AI_MODEL_ID,
+ sdkPackage: '@ai-sdk/openai',
+ model,
+ } as unknown as RegisteredAiModel);
+ jest
+ .spyOn(aiModelRegistryService, 'getAvailableModels')
+ .mockReturnValue([{ modelId: TEST_AI_MODEL_ID }] as never);
+
+ return offeredToolContexts;
+};
+
+const sendChatMessageAndWaitForAnswer = async (token: string) => {
+ const threadId = randomUUID();
+ const response = await makeMetadataApiRequest(
+ {
+ query: gql`
+ mutation SendChatMessage(
+ $threadId: UUID!
+ $text: String!
+ $messageId: UUID!
+ ) {
+ sendChatMessage(
+ threadId: $threadId
+ text: $text
+ messageId: $messageId
+ ) {
+ queued
+ }
+ }
+ `,
+ variables: { threadId, text: 'Hello', messageId: randomUUID() },
+ },
+ token,
+ );
+
+ expect(response.body.errors).toBeUndefined();
+
+ await expectEventually(
+ async () => {
+ const messagesResponse = await makeMetadataApiRequest(
+ {
+ query: gql`
+ query ChatMessages($threadId: UUID!) {
+ chatMessages(threadId: $threadId) {
+ role
+ }
+ }
+ `,
+ variables: { threadId },
+ },
+ token,
+ );
+
+ expect(messagesResponse.body.errors).toBeUndefined();
+ expect(messagesResponse.body.data.chatMessages).toContainEqual({
+ role: 'assistant',
+ });
+ },
+ { timeoutMs: CHAT_TURN_TIMEOUT_MS },
+ );
+
+ return threadId;
+};
+
+describe('Ask AI tools for an installed application acting for a user', () => {
+ let application: ApplicationWithVariable;
+ let applicationToken: string;
+ const threadIds: string[] = [];
+
+ beforeAll(async () => {
+ application = await setupApplicationWithVariable({
+ name: 'Ask AI Probe',
+ variableKey: 'ASK_AI_PROBE',
+ permissionFlagUniversalIdentifiers: [
+ SystemPermissionFlag.AI,
+ SystemPermissionFlag.WORKFLOWS,
+ ],
+ });
+
+ const { applicationAccessToken } =
+ await generateAppleAdminApplicationTokenPair({
+ applicationId: application.id,
+ });
+
+ applicationToken = applicationAccessToken.token;
+ });
+
+ beforeEach(() => jest.useRealTimers());
+
+ afterEach(() => jest.restoreAllMocks());
+
+ afterAll(async () => {
+ for (const threadId of threadIds) {
+ await destroyAgentChatThread({ threadId });
+ }
+
+ await cleanupApplicationAndAppRegistration({
+ applicationUniversalIdentifier: application.universalIdentifier,
+ });
+ });
+
+ it(
+ 'still offers a member their workflow tools',
+ async () => {
+ const offeredToolContexts = mockAnsweringModel();
+
+ threadIds.push(
+ await sendChatMessageAndWaitForAnswer(APPLE_JANE_ADMIN_ACCESS_TOKEN),
+ );
+
+ expect(offeredToolContexts).toHaveLength(1);
+ expect(offeredToolContexts[0]).toContain(WORKFLOW_TOOL_NAME);
+ },
+ CHAT_TURN_TIMEOUT_MS,
+ );
+
+ it(
+ 'does not offer the workflow tools to the application',
+ async () => {
+ const offeredToolContexts = mockAnsweringModel();
+
+ threadIds.push(await sendChatMessageAndWaitForAnswer(applicationToken));
+
+ expect(offeredToolContexts).toHaveLength(1);
+ expect(offeredToolContexts[0]).not.toContain(WORKFLOW_TOOL_NAME);
+ },
+ CHAT_TURN_TIMEOUT_MS,
+ );
+});