作者 202304001

增加重写功能提示词

1 export const maxWord = 5000; 1 export const maxWord = 5000;
2 export const minWord = 200; 2 export const minWord = 200;
3 3
4 -export const rewriteItems = [  
5 - "全文润色",  
6 - "全文缩写",  
7 - "全文扩写",  
8 - "语气更专业",  
9 - "语气更口语化",  
10 - "语气更易读",  
11 - "语气更含蓄",  
12 - "语气更学术化",  
13 - "语气更有文采",  
14 - "语气更有网感",  
15 -]; 4 +export type writePromptParam = {
  5 + writingPurposeName: string;
  6 + writingStyleName: string;
  7 + writingLanguageName: string;
  8 + prompt: string;
  9 + writingTypeName: string;
  10 + isImgName: string;
  11 + writingCount: string;
  12 + fileData: string;
  13 +};
  14 +
  15 +export interface WritePanelProps {
  16 + htmlCode: string;
  17 + setHtmlCode: React.Dispatch<React.SetStateAction<string>>;
  18 + loading: boolean;
  19 + setLoading: React.Dispatch<React.SetStateAction<boolean>>;
  20 + setWidth: React.Dispatch<React.SetStateAction<string>>;
  21 + setHtmlheader: React.Dispatch<React.SetStateAction<string>>;
  22 +}
  23 +
  24 +export const rewriteItemsMap: Record<string, string> = {
  25 + 全文润色: "请重新写,全文润色",
  26 + 全文缩写: "请重新写,全文缩写",
  27 + 全文扩写: "请重新写,全文扩写",
  28 + 语气更专业: "请重新写,语气更专业",
  29 + 语气更口语化: "请重新写,语气更口语化",
  30 + 语气更易读: "请重新写,语气更易读",
  31 + 语气更含蓄: "请重新写,语气更含蓄",
  32 + 语气更学术化: "请重新写,语气更学术化",
  33 + 语气更有文采: "请重新写,语气更有文采",
  34 + 语气更有网感: "请重新写,语气更有网感",
  35 +};
16 36
17 export const mergedData = [ 37 export const mergedData = [
18 { 38 {
@@ -84,6 +104,20 @@ export const mergedData = [ @@ -84,6 +104,20 @@ export const mergedData = [
84 }, 104 },
85 ]; 105 ];
86 106
  107 +export function getNavgParam(writeMessage: string) {
  108 + const param: writePromptParam = {
  109 + writingPurposeName: mergedData[0].default,
  110 + writingStyleName: mergedData[2].default,
  111 + writingLanguageName: mergedData[3].default,
  112 + prompt: writeMessage,
  113 + writingTypeName: mergedData[4].default,
  114 + isImgName: mergedData[5].default,
  115 + writingCount: "200",
  116 + fileData: "",
  117 + };
  118 + return param;
  119 +}
  120 +
87 // 20250408新增写作风格选项映射 121 // 20250408新增写作风格选项映射
88 export const getWritingStyleOptions = (purpose: string) => { 122 export const getWritingStyleOptions = (purpose: string) => {
89 switch (purpose) { 123 switch (purpose) {
@@ -117,17 +151,3 @@ export const getWritingStyleOptions = (purpose: string) => { @@ -117,17 +151,3 @@ export const getWritingStyleOptions = (purpose: string) => {
117 ]; 151 ];
118 } 152 }
119 }; 153 };
120 -  
121 -export interface writeMessage {  
122 - role: string;  
123 - content: string;  
124 -}  
125 -  
126 -export interface WritePanelProps {  
127 - htmlCode: string;  
128 - setHtmlCode: React.Dispatch<React.SetStateAction<string>>;  
129 - loading: boolean;  
130 - setLoading: React.Dispatch<React.SetStateAction<boolean>>;  
131 - setWidth: React.Dispatch<React.SetStateAction<string>>;  
132 - setHtmlheader: React.Dispatch<React.SetStateAction<string>>;  
133 -}  
@@ -6,7 +6,6 @@ import { IconButton } from "../button"; @@ -6,7 +6,6 @@ import { IconButton } from "../button";
6 import { message as msgModal } from "antd"; 6 import { message as msgModal } from "antd";
7 import { useChatStore } from "@/app/store"; 7 import { useChatStore } from "@/app/store";
8 import { getWrtingPrompt } from "@/app/utils/prompt"; 8 import { getWrtingPrompt } from "@/app/utils/prompt";
9 -import type { writePromptParam } from "@/app/types/prompt";  
10 import { processChatFile } from "@/app/utils/fileUtil"; 9 import { processChatFile } from "@/app/utils/fileUtil";
11 import Locale from "@/app/locales"; 10 import Locale from "@/app/locales";
12 import styles from "./wrtie-panel.module.scss"; 11 import styles from "./wrtie-panel.module.scss";
@@ -16,10 +15,11 @@ import { @@ -16,10 +15,11 @@ import {
16 maxWord, 15 maxWord,
17 mergedData, 16 mergedData,
18 minWord, 17 minWord,
19 - writeMessage,  
20 WritePanelProps, 18 WritePanelProps,
  19 + writePromptParam,
21 } from "./menuData"; 20 } from "./menuData";
22 import { writeModel } from "@/app/constant"; 21 import { writeModel } from "@/app/constant";
  22 +import { RequestMessage } from "@/app/typing";
23 23
24 export function WritingPanel(props: WritePanelProps) { 24 export function WritingPanel(props: WritePanelProps) {
25 const { 25 const {
@@ -44,7 +44,7 @@ export function WritingPanel(props: WritePanelProps) { @@ -44,7 +44,7 @@ export function WritingPanel(props: WritePanelProps) {
44 44
45 const [fileData, setFileData] = useState(""); 45 const [fileData, setFileData] = useState("");
46 const [fileName, setFileName] = useState(""); 46 const [fileName, setFileName] = useState("");
47 - const messages: writeMessage[] = []; 47 + const messages: RequestMessage[] = [];
48 48
49 // 生成动态数据 49 // 生成动态数据
50 const dynamicMergedData = mergedData.map((item) => { 50 const dynamicMergedData = mergedData.map((item) => {
@@ -44,9 +44,9 @@ import { HTMLPreview } from "../artifacts"; @@ -44,9 +44,9 @@ import { HTMLPreview } from "../artifacts";
44 import { getMindPrompt, getWrtingPrompt } from "@/app/utils/prompt"; 44 import { getMindPrompt, getWrtingPrompt } from "@/app/utils/prompt";
45 import { htmlToPdf2 } from "@/app/utils/fileExport/toPdf"; 45 import { htmlToPdf2 } from "@/app/utils/fileExport/toPdf";
46 import { hasTable, htmlToExcel } from "@/app/utils/fileExport/export2Excel"; 46 import { hasTable, htmlToExcel } from "@/app/utils/fileExport/export2Excel";
47 -import { writePromptParam } from "@/app/types/prompt";  
48 import dynamic from "next/dynamic"; 47 import dynamic from "next/dynamic";
49 -import { rewriteItems, mergedData } from "./menuData"; 48 +import { getNavgParam, rewriteItemsMap } from "./menuData";
  49 +import { RequestMessage } from "@/app/typing";
50 50
51 const EditorComponent = dynamic( 51 const EditorComponent = dynamic(
52 async () => (await import("./editor")).EditorComponent, 52 async () => (await import("./editor")).EditorComponent,
@@ -75,73 +75,63 @@ export function WritingPage() { @@ -75,73 +75,63 @@ export function WritingPage() {
75 const query = useLocation(); //获取路由参数 75 const query = useLocation(); //获取路由参数
76 let { msg, writeMessage } = query.state || {}; //获取路由参数 76 let { msg, writeMessage } = query.state || {}; //获取路由参数
77 77
78 - const items: MenuProps["items"] = rewriteItems.map((item, index) => ({  
79 - key: (index + 1).toString(),  
80 - label: (  
81 - <a  
82 - target="_blank"  
83 - rel="noopener noreferrer"  
84 - href="#"  
85 - onClick={(e) => {  
86 - e.preventDefault(); // 阻止默认行为  
87 - rewrite(item); // 调用 rewrite 函数并传递菜单项文本  
88 - }}  
89 - >  
90 - {item}  
91 - </a>  
92 - ),  
93 - })); 78 + const items: MenuProps["items"] = Object.entries(rewriteItemsMap).map(
  79 + ([key, value]) => ({
  80 + key,
  81 + label: (
  82 + <a
  83 + target="_blank"
  84 + rel="noopener noreferrer"
  85 + href="#"
  86 + onClick={(e) => {
  87 + e.preventDefault(); // 阻止默认行为
  88 + rewrite(value); // 调用 rewrite 函数并传递对应的值
  89 + }}
  90 + >
  91 + {key}
  92 + </a>
  93 + ),
  94 + }),
  95 + );
94 96
95 - useEffect(() => {  
96 - if (!msg) {  
97 - return;  
98 - }  
99 - if (!writeMessage) {  
100 - return; 97 + const requestWrite = async (content: string, messages: RequestMessage[]) => {
  98 + try {
  99 + setLoading(true);
  100 + messages.push({ role: "user", content: content });
  101 + const response = await chatStore.sendContext(messages, writeModel);
  102 + messages.push({ role: "assistant", content: response });
  103 + let cleanedContent = response.startsWith("```html")
  104 + ? response.substring(8)
  105 + : response;
  106 + if (cleanedContent.endsWith("```")) {
  107 + cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
  108 + }
  109 +
  110 + const bodyTagRegex = /<body[^>]*>/i;
  111 + const bodyTagMatch = cleanedContent.match(bodyTagRegex);
  112 + if (bodyTagMatch?.index !== undefined) {
  113 + setHtmlheader(
  114 + cleanedContent.slice(0, bodyTagMatch.index + bodyTagMatch[0].length),
  115 + );
  116 + }
  117 + localStorage.setItem("htmlCode", cleanedContent);
  118 + localStorage.setItem("aiWrite", JSON.stringify(messages));
  119 + setHtmlCode(cleanedContent);
  120 + } catch (error) {
  121 + msgModal.error("生成失败,请重试");
  122 + } finally {
  123 + setLoading(false);
101 } 124 }
  125 + };
  126 +
  127 + useEffect(() => {
  128 + if (!msg) return;
  129 + if (!writeMessage) return;
102 const navigateGetData = async () => { 130 const navigateGetData = async () => {
103 - try {  
104 - const param: writePromptParam = {  
105 - writingPurposeName: mergedData[0].default,  
106 - writingStyleName: mergedData[2].default,  
107 - writingLanguageName: mergedData[3].default,  
108 - prompt: writeMessage,  
109 - writingTypeName: mergedData[4].default,  
110 - isImgName: mergedData[5].default,  
111 - writingCount: "200",  
112 - fileData: "",  
113 - };  
114 - const input = getWrtingPrompt(param);  
115 - setLoading(true);  
116 - console.log("------------------------" + input);  
117 - const response = await chatStore.directLlmInvoke(input, writeModel);  
118 - let cleanedContent = response.startsWith("```html")  
119 - ? response.substring(8)  
120 - : response;  
121 - if (cleanedContent.endsWith("```")) {  
122 - cleanedContent = cleanedContent.substring(  
123 - 0,  
124 - cleanedContent.length - 4,  
125 - );  
126 - }  
127 - //保存html头部  
128 - const bodyTagRegex = /<body[^>]*>/i;  
129 - const bodyTagMatch = cleanedContent.match(bodyTagRegex);  
130 - if (bodyTagMatch && bodyTagMatch.index !== undefined) {  
131 - // 截取从文档开头到 <body> 标签的起始位置  
132 - const contentUpToBody = cleanedContent.slice(  
133 - 0,  
134 - bodyTagMatch.index + bodyTagMatch[0].length,  
135 - );  
136 - setHtmlheader(contentUpToBody); //保存html头部  
137 - }  
138 - localStorage.setItem("htmlCode", cleanedContent);  
139 - setHtmlCode(cleanedContent);  
140 - } catch (error) {  
141 - msgModal.error("生成失败,请重试");  
142 - } finally {  
143 - setLoading(false);  
144 - } 131 + let messages = [];
  132 + const input = getWrtingPrompt(getNavgParam(writeMessage));
  133 + console.log("------------------------" + input);
  134 + requestWrite(input, []);
145 }; 135 };
146 navigateGetData(); 136 navigateGetData();
147 }, []); 137 }, []);
@@ -232,17 +222,15 @@ export function WritingPage() { @@ -232,17 +222,15 @@ export function WritingPage() {
232 } 222 }
233 }, [wrapContentInDivWithWidth]); 223 }, [wrapContentInDivWithWidth]);
234 224
235 - async function rewrite(msg: string) { 225 + async function rewrite(content: string) {
236 const messagesStr = localStorage.getItem("aiWrite"); 226 const messagesStr = localStorage.getItem("aiWrite");
237 if (!messagesStr) return; 227 if (!messagesStr) return;
238 -  
239 - let messages: any; 228 + let messages: RequestMessage[];
240 try { 229 try {
241 messages = JSON.parse(messagesStr); 230 messages = JSON.parse(messagesStr);
242 } catch (error) { 231 } catch (error) {
243 return; 232 return;
244 } 233 }
245 -  
246 // 检查是否是数组且包含合法 message 对象 234 // 检查是否是数组且包含合法 message 对象
247 if (!Array.isArray(messages)) return; 235 if (!Array.isArray(messages)) return;
248 if ( 236 if (
@@ -257,34 +245,7 @@ export function WritingPage() { @@ -257,34 +245,7 @@ export function WritingPage() {
257 ) 245 )
258 ) 246 )
259 return; 247 return;
260 - try {  
261 - setLoading(true);  
262 - messages.push({ role: "user", content: msg });  
263 - const response = await chatStore.sendContext(messages, writeModel);  
264 - messages.push({ role: "assistant", content: response });  
265 - let cleanedContent = response.startsWith("```html")  
266 - ? response.substring(8)  
267 - : response;  
268 - if (cleanedContent.endsWith("```")) {  
269 - cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);  
270 - }  
271 -  
272 - const bodyTagRegex = /<body[^>]*>/i;  
273 - const bodyTagMatch = cleanedContent.match(bodyTagRegex);  
274 - if (bodyTagMatch?.index !== undefined) {  
275 - setHtmlheader(  
276 - cleanedContent.slice(0, bodyTagMatch.index + bodyTagMatch[0].length),  
277 - );  
278 - }  
279 -  
280 - localStorage.setItem("htmlCode", cleanedContent);  
281 - localStorage.setItem("aiWrite", JSON.stringify(messages));  
282 - setHtmlCode(cleanedContent);  
283 - } catch (error) {  
284 - msgModal.error("重写失败,请重试");  
285 - } finally {  
286 - setLoading(false);  
287 - } 248 + requestWrite(content, messages);
288 } 249 }
289 250
290 useEffect(() => { 251 useEffect(() => {
1 -export type writePromptParam = {  
2 - writingPurposeName: string;  
3 - writingStyleName: string;  
4 - writingLanguageName: string;  
5 - prompt: string;  
6 - writingTypeName: string;  
7 - isImgName: string;  
8 - writingCount: string;  
9 - fileData: string;  
10 -};  
@@ -21,7 +21,8 @@ export function createDeepThink(content: string) { @@ -21,7 +21,8 @@ export function createDeepThink(content: string) {
21 lines[i].trim().startsWith("8") || 21 lines[i].trim().startsWith("8") ||
22 lines[i].trim().startsWith("9") || 22 lines[i].trim().startsWith("9") ||
23 lines[i].trim().startsWith("10") || 23 lines[i].trim().startsWith("10") ||
24 - lines[i].trim().startsWith("10") 24 + lines[i].trim().startsWith("-") ||
  25 + lines[i].trim().startsWith("=")
25 ) { 26 ) {
26 if (j !== 0 && lines[i].startsWith(">")) { 27 if (j !== 0 && lines[i].startsWith(">")) {
27 lines[i] = lines[i].substring(1); 28 lines[i] = lines[i].substring(1);
1 -import type { writePromptParam } from "@/app/types/prompt"; 1 +import { writePromptParam } from "../components/writing/menuData";
2 2
3 //生成文章提示词 3 //生成文章提示词
4 export function getWrtingPrompt(param: writePromptParam): string { 4 export function getWrtingPrompt(param: writePromptParam): string {