Skip to content

Commit

Permalink
Merge branch 'feat/parse-file' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangtaofeng committed Mar 4, 2024
2 parents eb0c40a + 0270eb9 commit 9837103
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
4 changes: 2 additions & 2 deletions apps/builder/src/page/AI/AIAgent/aiagent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export const AIAgent: FC = () => {
const handleSubmitSave = async (data: Agent) => {
let currentData: Agent = { ...data }
if (
currentData.model !== AI_AGENT_MODEL.GPT_4 &&
!isPremiumModel(currentData.model) &&
currentData.knowledge?.length > 0
) {
currentData = {
Expand Down Expand Up @@ -755,7 +755,7 @@ export const AIAgent: FC = () => {
/>

{CAN_EDIT_KNOWLEDGE_FILE &&
getValues("model") === AI_AGENT_MODEL.GPT_4 && (
isPremiumModel(getValues("model")) && (
<Controller
name="knowledge"
control={control}
Expand Down
4 changes: 2 additions & 2 deletions apps/builder/src/page/AI/components/MarkdownMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
tableStyle,
} from "@/page/AI/components/MarkdownMessage/style"
import Code from "./Code"
import { handleMarkdownLine } from "./utils"
import { handleParseText } from "./utils"

export const MarkdownMessage: FC<MarkdownMessageProps> = (props) => {
const { children, isOwnMessage } = props
Expand Down Expand Up @@ -100,7 +100,7 @@ export const MarkdownMessage: FC<MarkdownMessageProps> = (props) => {
code: (props) => <Code {...props} />,
}}
>
{handleMarkdownLine(children ?? "")}
{handleParseText(children ?? "", isOwnMessage)}
</ReactMarkdown>
</Typography>
</div>
Expand Down
43 changes: 43 additions & 0 deletions apps/builder/src/page/AI/components/MarkdownMessage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,46 @@ export const getTextValue = (value: unknown): string => {
export const handleMarkdownLine = (text: string) => {
return text.replace(/((^\n?---)(\n|$))/gm, "\n---\n")
}

export const handleMarkdownCode = (
text: string,
isOwnMessage?: boolean,
): string => {
let res = text
if (isOwnMessage) {
const startRex =
/(^\n?```markdown)|(^\n?```md)|(\n*```markdown) | (\n*```md)/gim
const endRex = /(```\n?$)|(```\n\n)/gm
if (startRex.test(res) && endRex.test(res)) {
res = text.replace(startRex, () => {
return `\n\n`
})
res = res.replace(endRex, () => {
return `\n\n`
})
}
} else {
const startRex =
/(^\n?```markdown)|(^\n?```md)|((\n\n)(.*)```markdown) | ((\n\n)(.*)```md)/im
const endRex = /(```\n?$)|(```\n\n)/m
if (startRex.test(res) && endRex.test(res)) {
res = res.replace(startRex, () => {
return `\n\n`
})
res = res.replace(endRex, () => {
return `\n\n`
})
} else if (startRex.test(res)) {
res = res.replace(startRex, () => {
return `\n\n`
})
}
}
return res
}
export const handleParseText = (text: string, isOwnMessage?: boolean) => {
let res = text
res = handleMarkdownLine(text)
res = handleMarkdownCode(res, isOwnMessage)
return res
}
9 changes: 3 additions & 6 deletions apps/builder/src/page/AI/components/PreviewChat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { isPremiumModel } from "@illa-public/market-agent"
import {
ILLA_MIXPANEL_EVENT_TYPE,
MixpanelTrackContext,
} from "@illa-public/mixpanel-utils"
import {
AI_AGENT_MODEL,
AI_AGENT_TYPE,
KnowledgeFile,
} from "@illa-public/public-types"
import { AI_AGENT_TYPE, KnowledgeFile } from "@illa-public/public-types"
import { getCurrentUser } from "@illa-public/user-data"
import { AnimatePresence, motion } from "framer-motion"
import {
Expand Down Expand Up @@ -101,7 +98,7 @@ export const PreviewChat: FC<PreviewChatProps> = (props) => {
const [parseKnowledgeLoading, setParseKnowledgeLoading] = useState(false)

const inputRef = useRef<HTMLInputElement>(null)
const canShowKnowledgeFiles = model === AI_AGENT_MODEL.GPT_4
const canShowKnowledgeFiles = isPremiumModel(model)

const { t } = useTranslation()

Expand Down
5 changes: 3 additions & 2 deletions apps/builder/src/page/AI/components/ws/useAgentConnect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AI_AGENT_MODEL, AI_AGENT_TYPE } from "@illa-public/public-types"
import { isPremiumModel } from "@illa-public/market-agent"
import { AI_AGENT_TYPE } from "@illa-public/public-types"
import {
CollarModalType,
handleCollaPurchaseError,
Expand Down Expand Up @@ -71,7 +72,7 @@ export function useAgentConnect(useAgentProps: UseAgentProps) {
Object.keys(encodePayload).forEach((key) => {
if (key === "prompt") {
const text = encodePayload[key]
if (payload.model === AI_AGENT_MODEL.GPT_4) {
if (isPremiumModel(payload.model)) {
encodePayload[key] = encodeURIComponent(
formatMessageString(text, messageContent?.knowledgeFiles),
)
Expand Down

0 comments on commit 9837103

Please sign in to comment.