Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(ai): exclude current chat from open context (#4801)
Eric Hayes1667d840dd9e
diff --git a/apps/web/src/lib/core/component/AI/component/input/buildRequest.ts b/apps/web/src/lib/core/component/AI/component/input/buildRequest.tsindex 35f4a84927..d3178f5b5a 100644--- a/apps/web/src/lib/core/component/AI/component/input/buildRequest.ts+++ b/apps/web/src/lib/core/component/AI/component/input/buildRequest.ts@@ -33,7 +33,7 @@ export function useSendChatMessage() {}: ChatSendInput & { chatId?: string }): Promise<SendChatMessageResult> {// Append the dynamic-UI (displayResults) JSON schema so the model knows the// shape of the tool's `any` `view` argument.- const base = additionalInstructions();+ const base = additionalInstructions(chatId);const dashboard = displayResultsInstructions();const merged = base ? `${base}\n\n${dashboard}` : dashboard;diff --git a/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.test.ts b/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.test.tsnew file mode 100644index 0000000000..94adb6b74b--- /dev/null+++ b/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.test.ts@@ -0,0 +1,48 @@+import { beforeEach, describe, expect, test, vi } from 'vitest';++const { splitManager } = vi.hoisted(() => ({+ splitManager: {+ splits: vi.fn(),+ getSplit: vi.fn(),+ },+}));++vi.mock('@app/signal/splitLayout', () => ({+ globalSplitManager: () => splitManager,+}));++import { getOpenEntitiesPrompt } from './openEntitiesPrompt';++describe('getOpenEntitiesPrompt', () => {+ beforeEach(() => {+ splitManager.splits.mockReset();+ splitManager.getSplit.mockReset();+ });++ test('excludes the current chat while preserving other open entities', () => {+ splitManager.splits.mockReturnValue([+ { id: 'split-current', content: { type: 'chat', id: 'current-chat' } },+ { id: 'split-other', content: { type: 'chat', id: 'other-chat' } },+ { id: 'split-document', content: { type: 'md', id: 'document-id' } },+ ]);+ splitManager.getSplit.mockImplementation((splitId: string) => ({+ displayName: () =>+ splitId === 'split-other' ? 'Another chat' : 'Project notes',+ }));++ expect(getOpenEntitiesPrompt('current-chat')).toBe(+ '\nThe user currently has the following items open:\n' ++ '- Another chat (chat, id: other-chat)\n' ++ '- Project notes (md, id: document-id)'+ );+ expect(splitManager.getSplit).not.toHaveBeenCalledWith('split-current');+ });++ test('returns no open-entity prompt when only the current chat is open', () => {+ splitManager.splits.mockReturnValue([+ { id: 'split-current', content: { type: 'chat', id: 'current-chat' } },+ ]);++ expect(getOpenEntitiesPrompt('current-chat')).toBeNull();+ });+});diff --git a/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.ts b/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.tsnew file mode 100644index 0000000000..ed14a4295e--- /dev/null+++ b/apps/web/src/lib/core/component/AI/constant/openEntitiesPrompt.ts@@ -0,0 +1,25 @@+import { globalSplitManager } from '@app/signal/splitLayout';++export function getOpenEntitiesPrompt(currentChatId?: string): string | null {+ const manager = globalSplitManager();+ if (!manager) return null;++ const splits = manager.splits();+ const items: string[] = [];+ for (const split of splits) {+ if (split.content.type === 'component') continue;+ if (split.content.type === 'chat' && split.content.id === currentChatId) {+ continue;+ }+ const handle = manager.getSplit(split.id);+ const name = handle?.displayName();+ const type = split.content.type;+ if (name) {+ items.push(`- ${name} (${type}, id: ${split.content.id})`);+ } else {+ items.push(`- ${type} (id: ${split.content.id})`);+ }+ }+ if (items.length === 0) return null;+ return `\nThe user currently has the following items open:\n${items.join('\n')}`;+}diff --git a/apps/web/src/lib/core/component/AI/constant/prompts.ts b/apps/web/src/lib/core/component/AI/constant/prompts.tsindex 587a2d7bc8..b9f3383d19 100644--- a/apps/web/src/lib/core/component/AI/constant/prompts.ts+++ b/apps/web/src/lib/core/component/AI/constant/prompts.ts@@ -1,8 +1,8 @@-import { globalSplitManager } from '@app/signal/splitLayout';import { useUserContext } from '@core/context/user';import { useInstructionsMdTextQuery } from '@queries/storage/instructions-md';import { authServiceClient } from '@service-auth/client';import { createResource } from 'solid-js';+import { getOpenEntitiesPrompt } from './openEntitiesPrompt';const ABOUT_MACRO = `Macro is an AI workspace with all the latest models and built-in editors for pdfs, docs, notes, images, diagrams, chats and more. Macro is like ChatGPT but you can do all your work inside it+@@ -34,7 +34,7 @@ export function useAdditionalInstructions() {const response = await authServiceClient.getUserName();return response.isOk() ? response.value : null;});- return () => {+ return (currentChatId?: string) => {let prompt = ABOUT_MACRO;const name = userName();const parts = [name?.first_name, name?.last_name].filter(Boolean);@@ -54,7 +54,7 @@ export function useAdditionalInstructions() {'\nThese are system instructions provided by the user. Follow them\n';prompt += userInstructions;}- const openEntities = getOpenEntitiesPrompt();+ const openEntities = getOpenEntitiesPrompt(currentChatId);if (openEntities) {prompt += openEntities;}@@ -62,27 +62,6 @@ export function useAdditionalInstructions() {};}-function getOpenEntitiesPrompt(): string | null {- const manager = globalSplitManager();- if (!manager) return null;-- const splits = manager.splits();- const items: string[] = [];- for (const split of splits) {- if (split.content.type === 'component') continue;- const handle = manager.getSplit(split.id);- const name = handle?.displayName();- const type = split.content.type;- if (name) {- items.push(`- ${name} (${type}, id: ${split.content.id})`);- } else {- items.push(`- ${type} (id: ${split.content.id})`);- }- }- if (items.length === 0) return null;- return `\nThe user currently has the following items open:\n${items.join('\n')}`;-}-function appendDate(prompt: string) {const now = new Date();return `${prompt}\nThe current date is: ${now}`;