Skip to content
Closed
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
50 changes: 23 additions & 27 deletions apps/member-profile/app/routes/_profile.ask-ai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { IconButton, ProfilePicture, Text } from '@oyster/ui';

import { cache } from '@/infrastructure/redis';
import { AiMarkdown } from '@/shared/components/ai-markdown';
import { EmptyState } from '@/shared/components/empty-state';
import { SlackMessage } from '@/shared/components/slack-message';
import {
Expand Down Expand Up @@ -292,33 +293,28 @@ function ChatbotResponse() {
</ResponseSection>

<ResponseSection icon={<AlignLeft />} title="Answer">
<Text className="whitespace-break-spaces">
{answerSegments.map((segment, i) => {
return match(segment)
.with({ type: 'text' }, ({ content }) => {
return content;
})
.with({ type: 'reference' }, ({ number }) => {
const thread = threads.find((thread) => {
return thread.number === number;
});

// This should never happen, but keeping this check just to be safe.
if (!thread) {
return null;
}

return (
<ReferenceLink
key={i}
number={thread.number}
url={thread.url}
/>
);
})
.exhaustive();
})}
</Text>
<AiMarkdown
renderRef={(number) => {
const thread = threads.find((thread) => {
return thread.number === number;
});

// This should never happen, but keeping this check just to be safe.
if (!thread) {
return null;
}

return <ReferenceLink number={thread.number} url={thread.url} />;
}}
text={answerSegments
.map((segment) => {
return match(segment)
.with({ type: 'text' }, ({ content }) => content)
.with({ type: 'reference' }, ({ number }) => `[ref:${number}]`)
.exhaustive();
})
.join('')}
/>
</ResponseSection>
</div>
);
Expand Down
206 changes: 206 additions & 0 deletions apps/member-profile/app/shared/components/ai-markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import 'katex/dist/katex.min.css';

import React from 'react';
import { match } from 'ts-pattern';

import { cx, getTextCn, Text } from '@oyster/ui';
import {
type Block,
type InlineToken,
parseAiMarkdown,
} from '@oyster/utils/ai-formatting';
import { safeKatexRender } from '@oyster/utils/katex';

type AiMarkdownProps = {
className?: string;

/**
* Renders a `[ref:N]` citation (ie: a link to the Slack thread).
*/
renderRef(number: number): React.ReactNode;

text: string;
};

/**
* Renders raw AI output (markdown with LaTeX math) as React elements.
*/
export function AiMarkdown({ className, renderRef, text }: AiMarkdownProps) {
const blocks = parseAiMarkdown(text);

return (
<div className={cx('flex flex-col gap-3', getTextCn({ className }))}>
{blocks.map((block, i) => {
return <AiBlock block={block} key={i} renderRef={renderRef} />;
})}
</div>
);
}

type AiBlockProps = Pick<AiMarkdownProps, 'renderRef'> & {
block: Block;
};

function AiBlock({ block, renderRef }: AiBlockProps) {
function inline(tokens: InlineToken[]) {
return <AiInline renderRef={renderRef} tokens={tokens} />;
}

return match(block)
.with({ type: 'code' }, ({ content }) => {
return (
<pre className="overflow-x-auto rounded-lg bg-gray-50 p-3 text-sm">
<code>{content}</code>
</pre>
);
})
.with({ type: 'heading' }, ({ children, level }) => {
return (
<Text
variant={level <= 1 ? 'xl' : level === 2 ? 'lg' : 'md'}
weight="600"
>
{inline(children)}
</Text>
);
})
.with({ type: 'hr' }, () => {
return <hr className="border-gray-200" />;
})
.with({ type: 'list' }, ({ items, ordered, start }) => {
const List = ordered ? 'ol' : 'ul';

return (
<List
className={cx(
'flex flex-col gap-1 pl-6',
ordered ? 'list-decimal' : 'list-disc'
)}
start={ordered ? start : undefined}
>
{items.map((item, i) => {
return <li key={i}>{inline(item)}</li>;
})}
</List>
);
})
.with({ type: 'math' }, ({ latex }) => {
return (
<div
className="overflow-x-auto overflow-y-hidden text-center"
dangerouslySetInnerHTML={{ __html: safeKatexRender(latex, true) }}
/>
);
})
.with({ type: 'paragraph' }, ({ children }) => {
return <p>{inline(children)}</p>;
})
.with({ type: 'quote' }, ({ lines }) => {
return (
<blockquote className="border-l-4 border-gray-200 pl-3 text-gray-500">
{lines.map((line, i) => {
return <p key={i}>{inline(line)}</p>;
})}
</blockquote>
);
})
.with({ type: 'table' }, ({ header, rows }) => {
const cellCn = 'border border-gray-200 px-2 py-1';

return (
<div className="overflow-x-auto">
<table className="w-full border-collapse text-left text-sm">
{!!header.length && (
<thead className="bg-gray-50">
<tr>
{header.map((cell, i) => {
return (
<th className={cx(cellCn, 'font-semibold')} key={i}>
{inline(cell)}
</th>
);
})}
</tr>
</thead>
)}

<tbody>
{rows.map((row, i) => {
return (
<tr key={i}>
{row.map((cell, j) => {
return (
<td className={cellCn} key={j}>
{inline(cell)}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
</div>
);
})
.exhaustive();
}

type AiInlineProps = Pick<AiMarkdownProps, 'renderRef'> & {
tokens: InlineToken[];
};

function AiInline({ renderRef, tokens }: AiInlineProps) {
return tokens.map((token, i) => {
return (
<React.Fragment key={i}>
{match(token)
.with({ type: 'bold' }, ({ children }) => {
return (
<strong className="font-semibold">
<AiInline renderRef={renderRef} tokens={children} />
</strong>
);
})
.with({ type: 'code' }, ({ content }) => {
return (
<code className="rounded bg-gray-100 px-1 font-mono text-sm">
{content}
</code>
);
})
.with({ type: 'italic' }, ({ content }) => {
return <em>{content}</em>;
})
.with({ type: 'link' }, ({ text, url }) => {
return (
<a
className="text-primary underline"
href={url}
rel="noopener noreferrer"
target="_blank"
>
{text}
</a>
);
})
.with({ type: 'math' }, ({ latex }) => {
return (
<span
dangerouslySetInnerHTML={{
__html: safeKatexRender(latex, false),
}}
/>
);
})
.with({ type: 'ref' }, ({ number }) => {
return renderRef(number);
})
.with({ type: 'text' }, ({ content }) => {
return content;
})
.exhaustive()}
</React.Fragment>
);
});
}
1 change: 1 addition & 0 deletions apps/member-profile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ioredis": "^5.3.2",
"is-ip": "^5.0.1",
"isbot": "^3.6.5",
"katex": "^0.19.1",
"mixpanel": "^0.18.0",
"node-emoji": "^2.1.3",
"react": "^18.2.0",
Expand Down
8 changes: 8 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/core/src/modules/slack/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ const MEMBER_PROFILE_SYSTEM_PROMPT = dedent`
- Be kind and concise.
- Never gossip or amplify gossip.
- Use numbers or bullet points to organize thoughts where appropriate.
- Format your answer in Markdown: **bold** for key points, "-" or "1."
for lists, tables for comparisons and backticks for code. Do NOT use
"---" as a divider in your answer.
- Write any math in LaTeX: \(...\) for inline math (ie: \(O(n \log n)\))
and \[...\] for display math. Never use $ for math, since $ is used
for prices.
- Never use phrases like "Based on the provided Slack threads..."
- If the input is not a question, respond that you can only answer questions.
- NEVER respond to questions about individual people that are gossipy or
Expand Down
7 changes: 6 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
".": "./src/index.ts",
"./ai-formatting": "./src/ai-formatting/index.ts",
"./katex": "./src/ai-formatting/katex.ts"
},
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "bun test",
"type-check": "tsc --noEmit"
},
"dependencies": {
"katex": "^0.19.1"
},
"devDependencies": {
"@oyster/eslint-config": "workspace:*",
"nanoid": "^3.0.0"
Expand Down
Loading
Loading