Skip to content

Commit 7c26bc0

Browse files
committed
feat: show generated embedding results in messages
1 parent a052650 commit 7c26bc0

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { DatabaseZap } from 'lucide-react'
2+
import ReactMarkdown from 'react-markdown'
3+
import remarkGfm from 'remark-gfm'
4+
import {
5+
Accordion,
6+
AccordionContent,
7+
AccordionItem,
8+
AccordionTrigger,
9+
} from '~/components/ui/accordion'
10+
import { cn } from '~/lib/utils'
11+
12+
export type MarkdownAccordionProps = {
13+
title: string
14+
content: string
15+
error?: string
16+
className?: string
17+
}
18+
19+
export default function MarkdownAccordion({
20+
title,
21+
content,
22+
error,
23+
className,
24+
}: MarkdownAccordionProps) {
25+
return (
26+
<Accordion type="single" collapsible>
27+
<AccordionItem
28+
value="item-1"
29+
className={cn(
30+
'border-2 border-neutral-100 bg-neutral-50 px-3 py-2 rounded-md',
31+
error ? 'bg-destructive-300' : undefined,
32+
className
33+
)}
34+
>
35+
<AccordionTrigger className="p-0 gap-2">
36+
<div className="flex gap-2 items-center font-normal text-lighter text-sm">
37+
<DatabaseZap size={14} />
38+
{title}
39+
</div>
40+
</AccordionTrigger>
41+
<AccordionContent className="py-2 [&_>div]:pb-0 flex flex-col gap-2">
42+
<ReactMarkdown
43+
remarkPlugins={[remarkGfm]}
44+
className="prose text-xs mt-2 [&_ul>li::before]:top-2 [&_ol>li::before]:top-0"
45+
>
46+
{content}
47+
</ReactMarkdown>
48+
{error && <div className="text-destructive-600 text-xs">{error}</div>}
49+
</AccordionContent>
50+
</AccordionItem>
51+
</Accordion>
52+
)
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { codeBlock } from 'common-tags'
2+
import { ToolInvocation } from '~/lib/tools'
3+
import MarkdownAccordion from '../markdown-accordion'
4+
5+
export type GeneratedEmbeddingProps = {
6+
toolInvocation: ToolInvocation<'embed'>
7+
}
8+
9+
export default function GeneratedEmbedding({ toolInvocation }: GeneratedEmbeddingProps) {
10+
const { texts } = toolInvocation.args
11+
12+
if (!('result' in toolInvocation)) {
13+
return null
14+
}
15+
16+
if (!toolInvocation.result.success) {
17+
const content = codeBlock`
18+
${texts.map((text) => `> ${text}`).join('\n')}
19+
`
20+
21+
return (
22+
<MarkdownAccordion
23+
title="Error generating embedding"
24+
content={content}
25+
error={toolInvocation.result.error}
26+
/>
27+
)
28+
}
29+
30+
const { ids } = toolInvocation.result
31+
32+
const content = codeBlock`
33+
Results stored in \`meta.embeddings\` table:
34+
35+
${texts.map((text, i) => `- **\`id\`:** ${ids[i]}\n\n **\`content\`:** ${text}`).join('\n')}
36+
`
37+
38+
return <MarkdownAccordion title="Generated embedding" content={content} />
39+
}

apps/postgres-new/components/tools/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CsvImport from './csv-import'
55
import CsvRequest from './csv-request'
66
import ExecutedSql from './executed-sql'
77
import GeneratedChart from './generated-chart'
8+
import GeneratedEmbedding from './generated-embedding'
89

910
export type ToolUiProps = {
1011
toolInvocation: ToolInvocation
@@ -24,6 +25,8 @@ export function ToolUi({ toolInvocation }: ToolUiProps) {
2425
return <CsvExport toolInvocation={toolInvocation} />
2526
case 'renameConversation':
2627
return <ConversationRename toolInvocation={toolInvocation} />
28+
case 'embed':
29+
return <GeneratedEmbedding toolInvocation={toolInvocation} />
2730
}
2831
return null
2932
}

apps/postgres-new/lib/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function useLocalStorage<T>(key: string, initialValue: T) {
8383
const queryKey = ['local-storage', key]
8484

8585
const currentValue =
86-
typeof window !== 'undefined' ? window.localStorage.getItem(key) ?? undefined : undefined
86+
typeof window !== 'undefined' ? (window.localStorage.getItem(key) ?? undefined) : undefined
8787

8888
const { data: storedValue = currentValue ? (JSON.parse(currentValue) as T) : initialValue } =
8989
useQuery({

0 commit comments

Comments
 (0)