浏览代码

修复了bug

kagg886 2 月之前
父节点
当前提交
140ed38251

+ 56 - 50
src/components/markdown/plugins/echarts.ts

@@ -1,68 +1,74 @@
-import MarkdownIt from "markdown-it";
-import type {RenderRule} from "markdown-it/lib/renderer.mjs";
-import type Token from "markdown-it/lib/token.mjs";
-import {defineMarkdownPlugin} from "../type/markdown.ts";
-import {h} from "vue";
-import VueCharts from "/@/components/markdown/plugins/impl/VueCharts.vue";
-
+import MarkdownIt from 'markdown-it'
+import type { RenderRule } from 'markdown-it/lib/renderer.mjs'
+import type Token from 'markdown-it/lib/token.mjs'
+import { defineMarkdownPlugin } from '../type/markdown.ts'
+import { h } from 'vue'
+import VueCharts from '/@/components/markdown/plugins/impl/VueCharts.vue'
 
 // 验证JSON格式
 function isValidJSON(str: string): boolean {
-    try {
-        JSON.parse(str)
-        return true
-    } catch {
-        return false
-    }
+	if (str.startsWith('[')) {
+		return false
+	}
+	try {
+		const expr = JSON.parse(str)
+
+		return expr["series"] !== undefined;
+
+	} catch {
+		return false
+	}
 }
 
 // 渲染echarts代码块
 const renderEcharts: RenderRule = (tokens: Token[], idx: number) => {
-    const token = tokens[idx]
-    const content = token.content.trim()
+	const token = tokens[idx]
+	const content = token.content.trim()
 
-    if (!content) {
-        return '<div style="padding: 16px;background-color: #fff5f5;border: 1px solid #fed7d7;border-radius: 6px;color: #c53030;margin: 16px 0;">ECharts配置不能为空</div>'
-    }
-    // 生成完整HTML
-    return `<echarts-container style="width: 100%;height: 350px;margin: 16px 0; border-radius: 6px" data="${encodeURIComponent(content)}"></echarts-container>`
+	if (!content) {
+		return '<div style="padding: 16px;background-color: #fff5f5;border: 1px solid #fed7d7;border-radius: 6px;color: #c53030;margin: 16px 0;">ECharts配置不能为空</div>'
+	}
+	// 生成完整HTML
+	return `<echarts-container style="width: 100%;height: 350px;margin: 16px 0; border-radius: 6px" data="${encodeURIComponent(
+		content
+	)}"></echarts-container>`
 }
 
-
 const EChartsPlugin = defineMarkdownPlugin({
-    tagName: 'echarts-container',
-    mdItPlugin: function (md: MarkdownIt) {
-        // 保存原始的fence渲染器
-        const defaultRender = md.renderer.rules.fence ?? function (tokens, idx, options, _env, renderer) {
-            return renderer.renderToken(tokens, idx, options)
-        }
+	tagName: 'echarts-container',
+	mdItPlugin: function (md: MarkdownIt) {
+		// 保存原始的fence渲染器
+		const defaultRender =
+			md.renderer.rules.fence ??
+			function (tokens, idx, options, _env, renderer) {
+				return renderer.renderToken(tokens, idx, options)
+			}
 
-        // if (customElements.get('echarts-container') === undefined) {
-        //   customElements.define('echarts-container', EChartsElement, { extends: 'div' })
-        // }
+		// if (customElements.get('echarts-container') === undefined) {
+		//   customElements.define('echarts-container', EChartsElement, { extends: 'div' })
+		// }
 
-        // 重写fence渲染器
-        md.renderer.rules.fence = function (tokens, idx, options, env, renderer) {
-            const token = tokens[idx]
-            const info = token.info ? token.info.trim() : ''
+		// 重写fence渲染器
+		md.renderer.rules.fence = function (tokens, idx, options, env, renderer) {
+			const token = tokens[idx]
+			const info = token.info ? token.info.trim() : ''
 
-            // 检查是否是echarts代码块
-            if ((info === 'echarts' || info == 'json') && isValidJSON(token.content.trim())) {
-                return renderEcharts(tokens, idx, options, env, renderer)
-            }
+			// 检查是否是echarts代码块
+			if ((info === 'echarts' || info == 'json') && isValidJSON(token.content.trim())) {
+				return renderEcharts(tokens, idx, options, env, renderer)
+			}
 
-            // 其他代码块使用默认渲染器
-            return defaultRender(tokens, idx, options, env, renderer)
-        }
-    },
-    renderer: (node: {attribs: Record<string, string>}) => {
-        return h(VueCharts, {
-                data: node.attribs.data,
-                charts: node.attribs.id,
-                style: 'width: 100%;height: 350px;margin: 16px 0; border-radius: 6px'
-            }
-        )
-    }
+			// 其他代码块使用默认渲染器
+			return defaultRender(tokens, idx, options, env, renderer)
+		}
+	},
+	renderer: (node: { attribs: Record<string, string> }) => {
+		return h(VueCharts, {
+			data: node.attribs.data,
+			charts: node.attribs.id,
+			style: 'width: 100%;height: 350px;margin: 16px 0; border-radius: 6px',
+		})
+	},
 })
 
 export default EChartsPlugin

+ 7 - 1
src/components/markdown/plugins/impl/VueCharts.vue

@@ -15,6 +15,7 @@ const resizeHandler = () => {
   instance?.resize()
 }
 
+const showOrigin = ref(false)
 onMounted(()=> {
   let data: echarts.EChartsOption
   try {
@@ -24,7 +25,12 @@ onMounted(()=> {
     return
   }
   instance = echarts.init(dom.value)
-  instance.setOption(data)
+
+	try {
+		instance.setOption(data)
+	} catch (e) {
+		showOrigin.value = true
+	}
 
   window.addEventListener('resize', resizeHandler)
 })

+ 71 - 65
src/views/assistant/index.vue

@@ -28,57 +28,57 @@ watch(messages, (newVal) => {
 const inputMessage = ref('')
 const messagesContainer = ref<HTMLElement>()
 
-// 选中的工具和模型
-const selectedTool = ref([])
-// 工具选择
-const toolOptions = ref([
-	{
-		value: 'code',
-		label: '代码工具',
-		children: [
-			{
-				value: 'codebase-retrieval',
-				label: '代码库检索',
-			},
-			{
-				value: 'str-replace-editor',
-				label: '代码编辑器',
-			},
-			{
-				value: 'save-file',
-				label: '文件保存',
-			},
-		],
-	},
-	{
-		value: 'web',
-		label: '网络工具',
-		children: [
-			{
-				value: 'web-search',
-				label: '网络搜索',
-			},
-			{
-				value: 'web-fetch',
-				label: '网页获取',
-			},
-		],
-	},
-	{
-		value: 'system',
-		label: '系统工具',
-		children: [
-			{
-				value: 'execute-command',
-				label: '命令执行',
-			},
-			{
-				value: 'launch-process',
-				label: '进程启动',
-			},
-		],
-	},
-])
+// // 选中的工具和模型
+// const selectedTool = ref([])
+// // 工具选择
+// const toolOptions = ref([
+// 	{
+// 		value: 'code',
+// 		label: '代码工具',
+// 		children: [
+// 			{
+// 				value: 'codebase-retrieval',
+// 				label: '代码库检索',
+// 			},
+// 			{
+// 				value: 'str-replace-editor',
+// 				label: '代码编辑器',
+// 			},
+// 			{
+// 				value: 'save-file',
+// 				label: '文件保存',
+// 			},
+// 		],
+// 	},
+// 	{
+// 		value: 'web',
+// 		label: '网络工具',
+// 		children: [
+// 			{
+// 				value: 'web-search',
+// 				label: '网络搜索',
+// 			},
+// 			{
+// 				value: 'web-fetch',
+// 				label: '网页获取',
+// 			},
+// 		],
+// 	},
+// 	{
+// 		value: 'system',
+// 		label: '系统工具',
+// 		children: [
+// 			{
+// 				value: 'execute-command',
+// 				label: '命令执行',
+// 			},
+// 			{
+// 				value: 'launch-process',
+// 				label: '进程启动',
+// 			},
+// 		],
+// 	},
+// ])
 
 const prompt = ref<string>('')
 const openPromptDialog = ref(false)
@@ -258,6 +258,10 @@ ${resp.response.data.replace('\n', '')}
 \`\`\`
 
 `
+					} else {
+						if (rtn.render_content.at(-1) !== '\n') {
+							rtn.render_content += '\n'
+						}
 					}
 
 					messages.value.push({
@@ -303,6 +307,8 @@ ${resp.request.data.replace('\n', '')}
 > ${resp.error.split('\n').join('\n> ')}
 
 `
+					//过滤ai消息下的所有tool防止报错,鬼知道后端怎么想的
+					messages.value = messages.value.slice(0, messages.value.indexOf(rtn) + 1)
 					break
 			}
 		},
@@ -926,20 +932,20 @@ const { loading: exportConversationLoading, doLoading: exportConversation } = us
 			<div class="input-container" v-if="activeConversationId !== -1">
 				<!-- 工具和模型选择行 -->
 				<div class="selection-row">
-					<!-- 工具选择 -->
-					<div class="tool-selector">
-						<el-cascader
-							v-model="selectedTool"
-							:options="toolOptions"
-							:show-all-levels="false"
-							:props="{ multiple: true }"
-							collapse-tags
-							collapse-tags-tooltip
-							clearable
-							size="small"
-							style="width: 200px"
-						/>
-					</div>
+<!--					&lt;!&ndash; 工具选择 &ndash;&gt;-->
+<!--					<div class="tool-selector">-->
+<!--						<el-cascader-->
+<!--							v-model="selectedTool"-->
+<!--							:options="toolOptions"-->
+<!--							:show-all-levels="false"-->
+<!--							:props="{ multiple: true }"-->
+<!--							collapse-tags-->
+<!--							collapse-tags-tooltip-->
+<!--							clearable-->
+<!--							size="small"-->
+<!--							style="width: 200px"-->
+<!--						/>-->
+<!--					</div>-->
 
 					<!-- 模型选择 -->
 					<div class="model-selector">