diff --git a/packages/core/src/modules/slack/slack.prompts.test.ts b/packages/core/src/modules/slack/slack.prompts.test.ts new file mode 100644 index 000000000..aa8501771 --- /dev/null +++ b/packages/core/src/modules/slack/slack.prompts.test.ts @@ -0,0 +1,47 @@ +import { afterEach, describe, expect, setSystemTime, test } from 'bun:test'; + +import { buildMemberProfileUserPrompt } from './slack.prompts'; + +describe(buildMemberProfileUserPrompt.name, () => { + afterEach(() => { + setSystemTime(); + }); + + test("Should include today's date at the time of the call.", () => { + setSystemTime(new Date('2030-06-15T12:00:00')); + + const prompt = buildMemberProfileUserPrompt({ + question: 'What is Fam Friday?', + formattedThreads: [], + }); + + expect(prompt).toContain("Today's date is 2030-06-15."); + }); + + test('Should not reuse a stale date across calls.', () => { + setSystemTime(new Date('2030-06-15T12:00:00')); + buildMemberProfileUserPrompt({ question: 'Q?', formattedThreads: [] }); + + setSystemTime(new Date('2031-01-02T12:00:00')); + + const prompt = buildMemberProfileUserPrompt({ + question: 'Q?', + formattedThreads: [], + }); + + expect(prompt).toContain("Today's date is 2031-01-02."); + expect(prompt).not.toContain('2030-06-15'); + }); + + test('Should include the question and threads.', () => { + const prompt = buildMemberProfileUserPrompt({ + question: 'Has anyone applied to Google?', + formattedThreads: ['thread A', 'thread B'], + }); + + expect(prompt).toContain( + 'Has anyone applied to Google?' + ); + expect(prompt).toContain('thread A\n\nthread B'); + }); +}); diff --git a/packages/core/src/modules/slack/slack.prompts.ts b/packages/core/src/modules/slack/slack.prompts.ts new file mode 100644 index 000000000..b7f37d446 --- /dev/null +++ b/packages/core/src/modules/slack/slack.prompts.ts @@ -0,0 +1,29 @@ +import dayjs from 'dayjs'; +import dedent from 'dedent'; + +type BuildMemberProfileUserPromptInput = { + question: string; + formattedThreads: string[]; +}; + +/** + * Builds the per-request user prompt for the member profile "Ask AI" feature. + * + * Today's date lives here (and not in the system prompt) so that it's computed + * on every request. A module-level template would only be evaluated once, when + * the module is loaded, which makes the date go stale on long-running servers. + * This also keeps the cached system prompt identical across requests. + */ +export function buildMemberProfileUserPrompt({ + question, + formattedThreads, +}: BuildMemberProfileUserPromptInput): string { + return dedent` + Today's date is ${dayjs().format('YYYY-MM-DD')}. + + Please answer the following question based on the Slack context provided: + + ${question} + ${formattedThreads.join('\n\n')} + `; +} diff --git a/packages/core/src/modules/slack/slack.ts b/packages/core/src/modules/slack/slack.ts index 0dc7f988d..f3c87ed56 100644 --- a/packages/core/src/modules/slack/slack.ts +++ b/packages/core/src/modules/slack/slack.ts @@ -17,6 +17,7 @@ import { getPineconeIndex } from '@/infrastructure/pinecone'; import { cache, ONE_HOUR_IN_SECONDS } from '@/infrastructure/redis'; import { sendSlackNotification } from '@/modules/notifications/use-cases/send-slack-notification'; import { slack } from '@/modules/slack/instances'; +import { buildMemberProfileUserPrompt } from '@/modules/slack/slack.prompts'; import { IS_PRODUCTION } from '@/shared/env'; import { fail, type Result, success } from '@/shared/utils/core'; @@ -458,8 +459,6 @@ const MEMBER_PROFILE_SYSTEM_PROMPT = dedent` aspiring software engineers (and product managers/designers). We're a community of 10,000+ members across 100+ universities. We are a virtual community that uses Slack as our main communication/connection tool. - - Today's date is ${dayjs().format('YYYY-MM-DD')}. @@ -570,12 +569,10 @@ export async function answerMemberProfileQuestion({ const formattedThreads = threadsResult.data.map(formatThreadForLLM); - const userPrompt = dedent` - Please answer the following question based on the Slack context provided: - - ${question} - ${formattedThreads.join('\n\n')} - `; + const userPrompt = buildMemberProfileUserPrompt({ + question, + formattedThreads, + }); const completionResult = await getChatCompletion({ maxTokens: 1000,