|
1
|
import clsx from "clsx" |
|
2
|
import React, { useMemo } from "react" |
|
3
|
import { CodeMdx, CodeMdxProps } from "../../CodeMdx" |
|
4
|
import { MarkdownContent } from "../../MarkdownContent" |
|
5
|
import { MDXComponents } from "../../MDXComponents" |
|
6
|
import { AiAssistantThreadItemActions } from "./Actions" |
|
7
|
import { AiAssistantThreadItem as AiAssistantThreadItemType } from "../../../providers/AiAssistant" |
|
8
|
import { useChat } from "@kapaai/react-sdk" |
|
9
|
import { AiAssistantLoading } from "../Loading" |
|
10
|
|
|
11
|
export type AiAssistantThreadItemProps = { |
|
12
|
item: AiAssistantThreadItemType |
|
13
|
} |
|
14
|
|
|
15
|
export const AiAssistantThreadItem = ({ item }: AiAssistantThreadItemProps) => { |
|
16
|
const { error } = useChat() |
|
17
|
const showLoading = useMemo(() => { |
|
18
|
if (error?.length) { |
|
19
|
return false |
|
20
|
} |
|
21
|
|
|
22
|
return !item.question_id && item.content.length === 0 |
|
23
|
}, [item, error]) |
|
24
|
if ( |
|
25
|
item.isGenerationAborted && |
|
26
|
item.type === "answer" && |
|
27
|
!item.content.length |
|
28
|
) { |
|
29
|
return null |
|
30
|
} |
|
31
|
return ( |
|
32
|
<div |
|
33
|
className={clsx( |
|
34
|
"p-docs_0.5 flex gap-docs_0.75 items-start", |
|
35
|
item.type === "question" && "justify-end", |
|
36
|
item.type === "answer" && "!pr-[20px]" |
|
37
|
)} |
|
38
|
> |
|
39
|
<div |
|
40
|
className={clsx( |
|
41
|
"txt-small text-medusa-fg-base", |
|
42
|
"flex flex-col gap-docs_0.75", |
|
43
|
item.type !== "question" && "flex-1", |
|
44
|
item.type === "answer" && "text-pretty flex-1 max-w-[calc(100%-20px)]" |
|
45
|
)} |
|
46
|
> |
|
47
|
<div |
|
48
|
className={clsx( |
|
49
|
"flex flex-col gap-docs_0.75", |
|
50
|
item.type === "question" && [ |
|
51
|
"rounded-docs_xl bg-medusa-tag-neutral-bg", |
|
52
|
"px-docs_0.75 py-docs_0.5 max-w-full md:max-w-[400px]", |
|
53
|
] |
|
54
|
)} |
|
55
|
> |
|
56
|
{item.type === "question" && ( |
|
57
|
<MarkdownContent |
|
58
|
className="[&>*:last-child]:mb-0" |
|
59
|
allowedElements={["br", "p", "code", "pre"]} |
|
60
|
unwrapDisallowed={true} |
|
61
|
components={{ |
|
62
|
...MDXComponents, |
|
63
|
code: (props: CodeMdxProps) => { |
|
64
|
return ( |
|
65
|
<CodeMdx |
|
66
|
{...props} |
|
67
|
noCopy |
|
68
|
noReport |
|
69
|
forceNoTitle |
|
70
|
noAskAi |
|
71
|
inlineCodeProps={{ |
|
72
|
...props.inlineCodeProps, |
|
73
|
className: "!text-wrap !break-words", |
|
74
|
variant: "grey-bg", |
|
75
|
}} |
|
76
|
collapsibleLines="11" |
|
77
|
codeBlockProps={{ |
|
78
|
className: clsx( |
|
79
|
"rounded-docs_lg p-[5px]", |
|
80
|
props.className |
|
81
|
), |
|
82
|
wrapperClassName: "rounded-docs_lg", |
|
83
|
innerClassName: "border rounded-docs_lg", |
|
84
|
overrideColors: { |
|
85
|
bg: "bg-medusa-contrast-bg-subtle", |
|
86
|
innerBg: "bg-medusa-contrast-bg-subtle", |
|
87
|
innerBorder: "border-medusa-contrast-border-bot", |
|
88
|
}, |
|
89
|
}} |
|
90
|
/> |
|
91
|
) |
|
92
|
}, |
|
93
|
}} |
|
94
|
> |
|
95
|
{item.content} |
|
96
|
</MarkdownContent> |
|
97
|
)} |
|
98
|
{item.type === "answer" && ( |
|
99
|
<> |
|
100
|
{showLoading && <AiAssistantLoading />} |
|
101
|
<MarkdownContent |
|
102
|
className="[&>*:last-child]:mb-0" |
|
103
|
components={{ |
|
104
|
...MDXComponents, |
|
105
|
code: (props: CodeMdxProps) => { |
|
106
|
return ( |
|
107
|
<CodeMdx |
|
108
|
{...props} |
|
109
|
noReport |
|
110
|
noAskAi |
|
111
|
wrapperClassName="mt-docs_1" |
|
112
|
/> |
|
113
|
) |
|
114
|
}, |
|
115
|
}} |
|
116
|
disallowedElements={["h1", "h2", "h3", "h4", "h5", "h6"]} |
|
117
|
> |
|
118
|
{item.content} |
|
119
|
</MarkdownContent> |
|
120
|
</> |
|
121
|
)} |
|
122
|
</div> |
|
123
|
{(item.question_id || item.type === "question") && ( |
|
124
|
<AiAssistantThreadItemActions item={item} /> |
|
125
|
)} |
|
126
|
{item.type === "error" && ( |
|
127
|
<span className="text-medusa-fg-error">{item.content}</span> |
|
128
|
)} |
|
129
|
</div> |
|
130
|
</div> |
|
131
|
) |
|
132
|
} |