Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/core/src/modules/slack/slack.prompts.test.ts
Original file line number Diff line number Diff line change
@@ -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(
'<question>Has anyone applied to Google?</question>'
);
expect(prompt).toContain('<threads>thread A\n\nthread B</threads>');
});
});
29 changes: 29 additions & 0 deletions packages/core/src/modules/slack/slack.prompts.ts
Original file line number Diff line number Diff line change
@@ -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>${question}</question>
<threads>${formattedThreads.join('\n\n')}</threads>
`;
}
13 changes: 5 additions & 8 deletions packages/core/src/modules/slack/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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')}.
</context>

<output_format>
Expand Down Expand Up @@ -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>${question}</question>
<threads>${formattedThreads.join('\n\n')}</threads>
`;
const userPrompt = buildMemberProfileUserPrompt({
question,
formattedThreads,
});

const completionResult = await getChatCompletion({
maxTokens: 1000,
Expand Down
Loading