...
|
...
|
@@ -44,9 +44,9 @@ import { HTMLPreview } from "../artifacts"; |
|
|
import { getMindPrompt, getWrtingPrompt } from "@/app/utils/prompt";
|
|
|
import { htmlToPdf2 } from "@/app/utils/fileExport/toPdf";
|
|
|
import { hasTable, htmlToExcel } from "@/app/utils/fileExport/export2Excel";
|
|
|
import { writePromptParam } from "@/app/types/prompt";
|
|
|
import dynamic from "next/dynamic";
|
|
|
import { rewriteItems, mergedData } from "./menuData";
|
|
|
import { getNavgParam, rewriteItemsMap } from "./menuData";
|
|
|
import { RequestMessage } from "@/app/typing";
|
|
|
|
|
|
const EditorComponent = dynamic(
|
|
|
async () => (await import("./editor")).EditorComponent,
|
...
|
...
|
@@ -75,73 +75,63 @@ export function WritingPage() { |
|
|
const query = useLocation(); //获取路由参数
|
|
|
let { msg, writeMessage } = query.state || {}; //获取路由参数
|
|
|
|
|
|
const items: MenuProps["items"] = rewriteItems.map((item, index) => ({
|
|
|
key: (index + 1).toString(),
|
|
|
label: (
|
|
|
<a
|
|
|
target="_blank"
|
|
|
rel="noopener noreferrer"
|
|
|
href="#"
|
|
|
onClick={(e) => {
|
|
|
e.preventDefault(); // 阻止默认行为
|
|
|
rewrite(item); // 调用 rewrite 函数并传递菜单项文本
|
|
|
}}
|
|
|
>
|
|
|
{item}
|
|
|
</a>
|
|
|
),
|
|
|
}));
|
|
|
const items: MenuProps["items"] = Object.entries(rewriteItemsMap).map(
|
|
|
([key, value]) => ({
|
|
|
key,
|
|
|
label: (
|
|
|
<a
|
|
|
target="_blank"
|
|
|
rel="noopener noreferrer"
|
|
|
href="#"
|
|
|
onClick={(e) => {
|
|
|
e.preventDefault(); // 阻止默认行为
|
|
|
rewrite(value); // 调用 rewrite 函数并传递对应的值
|
|
|
}}
|
|
|
>
|
|
|
{key}
|
|
|
</a>
|
|
|
),
|
|
|
}),
|
|
|
);
|
|
|
|
|
|
useEffect(() => {
|
|
|
if (!msg) {
|
|
|
return;
|
|
|
}
|
|
|
if (!writeMessage) {
|
|
|
return;
|
|
|
const requestWrite = async (content: string, messages: RequestMessage[]) => {
|
|
|
try {
|
|
|
setLoading(true);
|
|
|
messages.push({ role: "user", content: content });
|
|
|
const response = await chatStore.sendContext(messages, writeModel);
|
|
|
messages.push({ role: "assistant", content: response });
|
|
|
let cleanedContent = response.startsWith("```html")
|
|
|
? response.substring(8)
|
|
|
: response;
|
|
|
if (cleanedContent.endsWith("```")) {
|
|
|
cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
|
|
|
}
|
|
|
|
|
|
const bodyTagRegex = /<body[^>]*>/i;
|
|
|
const bodyTagMatch = cleanedContent.match(bodyTagRegex);
|
|
|
if (bodyTagMatch?.index !== undefined) {
|
|
|
setHtmlheader(
|
|
|
cleanedContent.slice(0, bodyTagMatch.index + bodyTagMatch[0].length),
|
|
|
);
|
|
|
}
|
|
|
localStorage.setItem("htmlCode", cleanedContent);
|
|
|
localStorage.setItem("aiWrite", JSON.stringify(messages));
|
|
|
setHtmlCode(cleanedContent);
|
|
|
} catch (error) {
|
|
|
msgModal.error("生成失败,请重试");
|
|
|
} finally {
|
|
|
setLoading(false);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
|
if (!msg) return;
|
|
|
if (!writeMessage) return;
|
|
|
const navigateGetData = async () => {
|
|
|
try {
|
|
|
const param: writePromptParam = {
|
|
|
writingPurposeName: mergedData[0].default,
|
|
|
writingStyleName: mergedData[2].default,
|
|
|
writingLanguageName: mergedData[3].default,
|
|
|
prompt: writeMessage,
|
|
|
writingTypeName: mergedData[4].default,
|
|
|
isImgName: mergedData[5].default,
|
|
|
writingCount: "200",
|
|
|
fileData: "",
|
|
|
};
|
|
|
const input = getWrtingPrompt(param);
|
|
|
setLoading(true);
|
|
|
console.log("------------------------" + input);
|
|
|
const response = await chatStore.directLlmInvoke(input, writeModel);
|
|
|
let cleanedContent = response.startsWith("```html")
|
|
|
? response.substring(8)
|
|
|
: response;
|
|
|
if (cleanedContent.endsWith("```")) {
|
|
|
cleanedContent = cleanedContent.substring(
|
|
|
0,
|
|
|
cleanedContent.length - 4,
|
|
|
);
|
|
|
}
|
|
|
//保存html头部
|
|
|
const bodyTagRegex = /<body[^>]*>/i;
|
|
|
const bodyTagMatch = cleanedContent.match(bodyTagRegex);
|
|
|
if (bodyTagMatch && bodyTagMatch.index !== undefined) {
|
|
|
// 截取从文档开头到 <body> 标签的起始位置
|
|
|
const contentUpToBody = cleanedContent.slice(
|
|
|
0,
|
|
|
bodyTagMatch.index + bodyTagMatch[0].length,
|
|
|
);
|
|
|
setHtmlheader(contentUpToBody); //保存html头部
|
|
|
}
|
|
|
localStorage.setItem("htmlCode", cleanedContent);
|
|
|
setHtmlCode(cleanedContent);
|
|
|
} catch (error) {
|
|
|
msgModal.error("生成失败,请重试");
|
|
|
} finally {
|
|
|
setLoading(false);
|
|
|
}
|
|
|
let messages = [];
|
|
|
const input = getWrtingPrompt(getNavgParam(writeMessage));
|
|
|
console.log("------------------------" + input);
|
|
|
requestWrite(input, []);
|
|
|
};
|
|
|
navigateGetData();
|
|
|
}, []);
|
...
|
...
|
@@ -232,17 +222,15 @@ export function WritingPage() { |
|
|
}
|
|
|
}, [wrapContentInDivWithWidth]);
|
|
|
|
|
|
async function rewrite(msg: string) {
|
|
|
async function rewrite(content: string) {
|
|
|
const messagesStr = localStorage.getItem("aiWrite");
|
|
|
if (!messagesStr) return;
|
|
|
|
|
|
let messages: any;
|
|
|
let messages: RequestMessage[];
|
|
|
try {
|
|
|
messages = JSON.parse(messagesStr);
|
|
|
} catch (error) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检查是否是数组且包含合法 message 对象
|
|
|
if (!Array.isArray(messages)) return;
|
|
|
if (
|
...
|
...
|
@@ -257,34 +245,7 @@ export function WritingPage() { |
|
|
)
|
|
|
)
|
|
|
return;
|
|
|
try {
|
|
|
setLoading(true);
|
|
|
messages.push({ role: "user", content: msg });
|
|
|
const response = await chatStore.sendContext(messages, writeModel);
|
|
|
messages.push({ role: "assistant", content: response });
|
|
|
let cleanedContent = response.startsWith("```html")
|
|
|
? response.substring(8)
|
|
|
: response;
|
|
|
if (cleanedContent.endsWith("```")) {
|
|
|
cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
|
|
|
}
|
|
|
|
|
|
const bodyTagRegex = /<body[^>]*>/i;
|
|
|
const bodyTagMatch = cleanedContent.match(bodyTagRegex);
|
|
|
if (bodyTagMatch?.index !== undefined) {
|
|
|
setHtmlheader(
|
|
|
cleanedContent.slice(0, bodyTagMatch.index + bodyTagMatch[0].length),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
localStorage.setItem("htmlCode", cleanedContent);
|
|
|
localStorage.setItem("aiWrite", JSON.stringify(messages));
|
|
|
setHtmlCode(cleanedContent);
|
|
|
} catch (error) {
|
|
|
msgModal.error("重写失败,请重试");
|
|
|
} finally {
|
|
|
setLoading(false);
|
|
|
}
|
|
|
requestWrite(content, messages);
|
|
|
}
|
|
|
|
|
|
useEffect(() => {
|
...
|
...
|
|