Prechádzať zdrojové kódy

refactor(ai-chat): 优化工具生成逻辑,修复带选页加载问题

1. 调整缩略页点击事件,避免重复触发状态更新
2. 注释掉ChildPageDisplay中自动创建工具的逻辑
3. 在AI聊天组件中新增批量处理所有幻灯片工具的生成逻辑
4. 新增createTool和setTool工具函数复用代码逻辑
lsc 2 dní pred
rodič
commit
48536f2824

+ 104 - 0
src/components/CollapsibleToolbar/componets/aiChat.vue

@@ -900,6 +900,19 @@ const generate = (message: ChatMessage) => {
           slidesStore.updateSlide({ toolsArray: item.tools }, slide.id)
         }
       }
+
+      for (let i = 0; i < slidesStore.slides.length; i++) {
+        const slide = slidesStore.slides[i]
+        if (slide.toolsArray && slide.toolsArray.length > 0) {
+          for (let j = 0; j < slide.toolsArray.length; j++) {
+            const tool = slide.toolsArray[j]
+            if (tool.url) {
+              continue
+            }
+            createTool(slide.toolsArray, tool, j, slide)
+          }
+        }
+      }
       
       const firstPageWithTools = slidesStore.slides.findIndex((slide: any) => 
         slide.toolsArray && slide.toolsArray.length > 0
@@ -931,6 +944,84 @@ const generate = (message: ChatMessage) => {
   }
 }
 
+const createTool = async (array: any[], tool: any, index: number, slide: any) => {
+  let action = ''
+  let type = 0
+  if (tool.tool_id === 'choice') {
+    action = lang.ssAiChatQuickAction1
+    type = 45
+  }
+  else if (tool.tool_id === 'qa') {
+    action = lang.ssAiChatQuickAction2
+    type = 15
+  }
+  else if (tool.tool_id === 'poll') {
+    action = lang.ssAiChatQuickAction6
+    type = 78
+  }
+  else if (tool.tool_id === 'photo') {
+    action = lang.ssAiChatQuickAction7
+    type = 79
+  }
+  
+  const textContents = slide?.elements
+    .filter((element: any) => element.type === 'text' || (element.type === 'shape' && element.text && element.text.content))
+    .map((textElement: any) => {
+      if (textElement.type === 'shape') {
+        const tempElement = document.createElement('div')
+        tempElement.innerHTML = textElement.text.content
+        return tempElement.textContent || tempElement.innerText || ''
+      }
+      const tempElement = document.createElement('div')
+      tempElement.innerHTML = textElement.content
+      return tempElement.textContent || tempElement.innerText || ''
+    })
+    .filter(content => content.trim() !== '') || []
+  
+  console.log('textContents', textContents)
+  const prompt = `
+      #课程标题(course_title): ${props.courseTitle || ''}
+      #当前页面内容(current_page_content): ${textContents.length > 0 ? textContents.join('\n') : '无文本内容'}
+      #query: ${action}
+      `
+  const result = chat_no_stream(prompt, agentid2.value, props.userid || '', lang.lang)
+  console.log(result)
+  const content = await result.promise
+  console.log('👶 创建工具:', content)
+
+  let jsonFormat = ''
+  if (tool.tool_id === 'choice') {
+    jsonFormat = `{"testCount":1,"testTitle":"","testJson":[{"id":"7de1fdb4-bec3-4324-8986-4623f838e3d7","type":"2","teststitle":"1+1?","checkList":["1","2","3"],"timuList":[],"answer":[1],"userAnswer":[],"explanation":"解析"}]}`
+  }
+  else if (tool.tool_id === 'qa') {
+    jsonFormat = `{"answerQ":"问题","answer":"","fileList":[],"imageList":[],"evaluationCriteria":"评价标准"}`
+  }
+  else if (tool.tool_id === 'poll') {
+    jsonFormat = `{"testCount":1,"testTitle":"","testJson":[{"id":"7de1fdb4-bec3-4324-8986-4623f838e3d7","type":"2","teststitle":"1+1?","checkList":["1","2","3"],"timuList":[],"answer":[],"userAnswer":[]}]}`
+  }
+  else if (tool.tool_id === 'photo') {
+    jsonFormat = `{"answerQ":"问题","answer":"","fileList":[]}`
+  }
+  
+  const res = await chat_no_stream2([{
+    role: 'user',
+    content: `这是用户输入的内容:"${content}",根据用户输入的内容,生成json。输出一个json格式的回复,格式如下:${jsonFormat}。`,
+  }], { type: 'json_object' })
+  
+  console.log(`${tool.tool_name}`, res)
+  
+  const pageId = await setPageId(type, res)
+  const baseUrl = setUrl()
+  const url = `${baseUrl}/pbl-teacher-table/dist/workPage.html#/setWorkPage?id=${pageId}&type=${type}&userid=${props.userid || ''}`
+  array[index].url = url
+  setTool(slide, array)
+}
+
+const setTool = (slide: any, toolsArray: any[]) => {
+  slidesStore.updateSlide({ toolsArray: toolsArray }, slide.id)
+  console.log(slide)
+}
+
 const setPageId = async (tool: any, json: any) => {
   const res = await getWorkPageId({
     userid: props.userid || '',
@@ -1104,6 +1195,19 @@ onMounted(() => {
   getAgentModel(agentid3.value)
   getAgentModel(agentid4.value)
   getAgentModel(agentid5.value)
+
+  for (let i = 0; i < slidesStore.slides.length; i++) {
+    const slide = slidesStore.slides[i]
+    if (slide.toolsArray && slide.toolsArray.length > 0) {
+      for (let j = 0; j < slide.toolsArray.length; j++) {
+        const tool = slide.toolsArray[j]
+        if (tool.url) {
+          continue
+        }
+        createTool(slide.toolsArray, tool, j, slide)
+      }
+    }
+  }
 })
 </script>
 

+ 19 - 18
src/views/Editor/Canvas/ChildPageDisplay.vue

@@ -72,40 +72,41 @@ const selectTool = (index: number) => {
   console.log('👶 选中工具:', toolsArray[index])
 }
 
-onMounted(async () => {
+onMounted(() => {
   console.log('👶 带选页打开,toolsArray:', toolsArray)
   const toolsToCreate = toolsArray.filter(t => !t.url)
   if (toolsToCreate.length === 0) {
     loading.value = false
-    return
+    // return
   }
   
-  const createPromises = toolsToCreate.map((tool) => {
-    const originalIndex = toolsArray.findIndex(t => t.tool_id === tool.tool_id)
-    return createTool(toolsArray, tool, originalIndex)
-  })
+  // const createPromises = toolsToCreate.map((tool) => {
+  //   const originalIndex = toolsArray.findIndex(t => t.tool_id === tool.tool_id)
+  //   return createTool(toolsArray, tool, originalIndex)
+  // })
   
-  await Promise.all(createPromises)
-  loading.value = false
+  // await Promise.all(createPromises)
+  // loading.value = false
 })
 
-watch(childPageToolsArray, async (newVal) => {
+watch(childPageToolsArray, (newVal) => {
+  // async 
   console.log('👶 带选页 toolsArray 更新:', newVal)
   const toolsToCreate = newVal.filter(t => !t.url)
   if (toolsToCreate.length === 0) {
     loading.value = false
-    return
+    // return
   }
   
-  loading.value = true
-  const createPromises = toolsToCreate.map((tool) => {
-    const originalIndex = newVal.findIndex(t => t.tool_id === tool.tool_id)
-    return createTool(newVal, tool, originalIndex)
-  })
+  // loading.value = true
+  // const createPromises = toolsToCreate.map((tool) => {
+  //   const originalIndex = newVal.findIndex(t => t.tool_id === tool.tool_id)
+  //   return createTool(newVal, tool, originalIndex)
+  // })
   
-  await Promise.all(createPromises)
-  loading.value = false
-})
+  // await Promise.all(createPromises)
+  // loading.value = false
+}, { immediate: true, deep: true })
 
 const createTool = async (array: any[], tool: any, index: number) => {
   let action = ''

+ 3 - 2
src/views/Editor/Thumbnails/ThumbnailChildPage.vue

@@ -37,9 +37,10 @@ const changeSlideIndex = (index: number) => {
 }
 
 const handleClick = () => {
-  if (slideIndexStore.value === props.slideIndex) return
   console.log(props.slideIndex)
-  changeSlideIndex(props.slideIndex || 0)
+  if (slideIndexStore.value !== props.slideIndex) {
+    changeSlideIndex(props.slideIndex || 0)
+  }
   setChildPageState(false, [], props.slideIndex || 0)
   setTimeout(() => {
     setChildPageState(true, props.toolsArray || [], props.slideIndex || 0)