|
@@ -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(() => {
|