Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(casc): replace new Function() template with safe string builders (#14751)

LiJiand8bfc58f2253
diff --git a/src/features/ChangelogModal/index.tsx b/src/features/ChangelogModal/index.tsx
index 2741c564cce..f44c3155c7c 100644
--- a/src/features/ChangelogModal/index.tsx
+++ b/src/features/ChangelogModal/index.tsx
@@ -1,26 +1,40 @@
'use client';
-import { useTimeout } from 'ahooks';
-import { memo } from 'react';
+import { memo, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
+import { lambdaClient } from '@/libs/trpc/client';
import { useGlobalStore } from '@/store/global';
-const ChangelogModal = memo<{ currentId?: string }>(({ currentId }) => {
+const ChangelogModal = memo<{ currentId?: string }>(({ currentId: propCurrentId }) => {
const [latestChangelogId, updateSystemStatus] = useGlobalStore((s) => [
s.status.latestChangelogId,
s.updateSystemStatus,
]);
const navigate = useNavigate();
+ const [currentId, setCurrentId] = useState(propCurrentId);
- useTimeout(() => {
+ useEffect(() => {
+ if (propCurrentId) return;
+ lambdaClient.changelog.getIndex
+ .query()
+ .then((index) => {
+ if (index[0]?.id) setCurrentId(index[0].id);
+ })
+ .catch(() => {});
+ }, [propCurrentId]);
+
+ useEffect(() => {
if (!currentId) return;
- if (!latestChangelogId) {
- updateSystemStatus({ latestChangelogId: currentId });
- } else if (latestChangelogId !== currentId) {
- navigate('/changelog/modal');
- }
- }, 1000);
+ const timer = setTimeout(() => {
+ if (!latestChangelogId) {
+ updateSystemStatus({ latestChangelogId: currentId });
+ } else if (latestChangelogId !== currentId) {
+ navigate('/changelog/modal');
+ }
+ }, 1000);
+ return () => clearTimeout(timer);
+ }, [currentId, latestChangelogId, updateSystemStatus, navigate]);
return null;
});
diff --git a/src/features/Conversation/components/ShareMessageModal/ShareText/template.ts b/src/features/Conversation/components/ShareMessageModal/ShareText/template.ts
index 87f153aa504..5da96da41f5 100644
--- a/src/features/Conversation/components/ShareMessageModal/ShareText/template.ts
+++ b/src/features/Conversation/components/ShareMessageModal/ShareText/template.ts
@@ -1,32 +1,14 @@
import { type UIChatMessage } from '@lobechat/types';
-import { template } from 'es-toolkit/compat';
import { LOADING_FLAT } from '@/const/message';
import { normalizeThinkTags, processWithArtifact } from '@/features/Conversation/utils/markdown';
-const markdownTemplate = template(
- `<% messages.forEach(function(chat) { %>
-
-{{chat.content}}
-
-<% }); %>
-`,
- {
- evaluate: /<%([\s\S]+?)%>/g,
- interpolate: /\{\{([\s\S]+?)\}\}/g,
- },
-);
-
interface MarkdownParams {
messages: UIChatMessage[];
}
-export const generateMarkdown = ({ messages }: MarkdownParams) =>
- markdownTemplate({
- messages: messages
- .filter((m) => m.content !== LOADING_FLAT)
- .map((message) => ({
- ...message,
- content: normalizeThinkTags(processWithArtifact(message.content)),
- })),
- });
+export const generateMarkdown = ({ messages }: MarkdownParams): string =>
+ messages
+ .filter((m) => m.content !== LOADING_FLAT)
+ .map((message) => normalizeThinkTags(processWithArtifact(message.content)))
+ .join('\n\n');
diff --git a/src/features/ShareModal/ShareText/template.ts b/src/features/ShareModal/ShareText/template.ts
index af5b30ae263..1a4c806db2f 100644
--- a/src/features/ShareModal/ShareText/template.ts
+++ b/src/features/ShareModal/ShareText/template.ts
@@ -1,58 +1,9 @@
import { type UIChatMessage } from '@lobechat/types';
-import { template } from 'es-toolkit/compat';
import { LOADING_FLAT } from '@/const/message';
import { normalizeThinkTags, processWithArtifact } from '@/features/Conversation/utils/markdown';
import { type FieldType } from '@/features/ShareModal/ShareText/type';
-const markdownTemplate = template(
- `# {{title}}
-
-<% if (systemRole) { %>
-\`\`\`\`md
-{{systemRole}}
-\`\`\`\`
-<% } %>
-
-<% messages.forEach(function(chat) { %>
-
-<% if (withRole) { %>
-
-<% if (chat.role === 'user') { %>
-##### User:
-<% } else if (chat.role === 'assistant') { %>
-##### Assistant:
-<% } else if (chat.role === 'tool') { %>
-##### Tools Calling:
-<% } %>
-
-<% } %>
-
-<% if (chat.role === 'tool') { %>
-\`\`\`json
-{{chat.content}}
-\`\`\`
-<% } else { %>
-
-{{chat.content}}
-
-<% if (includeTool && chat.tools) { %>
-
-\`\`\`json
-{{JSON.stringify(chat.tools, null, 2)}}
-\`\`\`
-
-<% } %>
-<% } %>
-
-<% }); %>
-`,
- {
- evaluate: /<%([\s\S]+?)%>/g,
- interpolate: /\{\{([\s\S]+?)\}\}/g,
- },
-);
-
interface MarkdownParams extends FieldType {
messages: UIChatMessage[];
systemRole: string;
@@ -67,18 +18,45 @@ export const generateMarkdown = ({
withSystemRole,
withRole,
systemRole,
-}: MarkdownParams) =>
- markdownTemplate({
- includeTool,
- messages: messages
- .filter((m) => m.content !== LOADING_FLAT)
- .filter((m) => (!includeUser ? m.role !== 'user' : true))
- .filter((m) => (!includeTool ? m.role !== 'tool' : true))
- .map((message) => ({
- ...message,
- content: normalizeThinkTags(processWithArtifact(message.content)),
- })),
- systemRole: withSystemRole ? systemRole : undefined,
- title,
- withRole,
- });
+}: MarkdownParams): string => {
+ const parts: string[] = [`# ${title}`, ''];
+
+ if (withSystemRole && systemRole) {
+ parts.push('````md', systemRole, '````', '');
+ }
+
+ const filteredMessages = messages
+ .filter((m) => m.content !== LOADING_FLAT)
+ .filter((m) => (!includeUser ? m.role !== 'user' : true))
+ .filter((m) => (!includeTool ? m.role !== 'tool' : true))
+ .map((message) => ({
+ ...message,
+ content: normalizeThinkTags(processWithArtifact(message.content)),
+ }));
+
+ for (const chat of filteredMessages) {
+ parts.push('');
+
+ if (withRole) {
+ if (chat.role === 'user') {
+ parts.push('##### User:', '');
+ } else if (chat.role === 'assistant') {
+ parts.push('##### Assistant:', '');
+ } else if (chat.role === 'tool') {
+ parts.push('##### Tools Calling:', '');
+ }
+ }
+
+ if (chat.role === 'tool') {
+ parts.push('```json', String(chat.content), '```');
+ } else {
+ parts.push(String(chat.content));
+
+ if (includeTool && chat.tools && chat.tools.length > 0) {
+ parts.push('', '```json', JSON.stringify(chat.tools, null, 2), '```');
+ }
+ }
+ }
+
+ return parts.join('\n');
+};
diff --git a/src/helpers/parserPlaceholder/index.ts b/src/helpers/parserPlaceholder/index.ts
index 539da8cd3d0..e524acfc2f9 100644
--- a/src/helpers/parserPlaceholder/index.ts
+++ b/src/helpers/parserPlaceholder/index.ts
@@ -1,6 +1,5 @@
import { isDesktop } from '@lobechat/const';
import { uuid } from '@lobechat/utils';
-import { template } from 'es-toolkit/compat';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
@@ -195,7 +194,10 @@ export const parsePlaceholderVariables = (text: string, depth = 2): string => {
.filter(([, value]) => value !== undefined),
);
- const replaced = template(result, { interpolate: placeholderVariablesRegex })(variables);
+ const replaced = result.replaceAll(
+ placeholderVariablesRegex,
+ (match, key) => variables[key.trim()] ?? match,
+ );
if (replaced === result) break;
result = replaced;
diff --git a/src/routes/(main)/agent/features/ChangelogModal.tsx b/src/routes/(main)/agent/features/ChangelogModal.tsx
index b4c63dffd08..9dae9b5b9b9 100644
--- a/src/routes/(main)/agent/features/ChangelogModal.tsx
+++ b/src/routes/(main)/agent/features/ChangelogModal.tsx
@@ -1,11 +1,7 @@
import ChangelogModal from '@/features/ChangelogModal';
-import { ChangelogService } from '@/server/services/changelog';
-const Changelog = async () => {
- const service = new ChangelogService();
- const id = await service.getLatestChangelogId();
-
- return <ChangelogModal currentId={id} />;
+const Changelog = () => {
+ return <ChangelogModal />;
};
export default Changelog;