fix: validate JSON before applying AI result, show raw text on parse failure
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, nextTick } from 'vue'
|
||||
import { ref, nextTick } from 'vue'
|
||||
import { useEditorStore } from '../stores/editorStore'
|
||||
import { sendAIRequest } from '../composables/useAI'
|
||||
|
||||
@@ -12,10 +12,6 @@ const chatRef = ref<HTMLDivElement | null>(null)
|
||||
|
||||
const mode = ref<'json' | 'code'>('json')
|
||||
|
||||
watch(() => store.selectedNodeId, (id) => {
|
||||
mode.value = id ? 'json' : 'code'
|
||||
}, { immediate: true })
|
||||
|
||||
function toggleMode() {
|
||||
mode.value = mode.value === 'json' ? 'code' : 'json'
|
||||
}
|
||||
@@ -37,12 +33,18 @@ async function send() {
|
||||
try {
|
||||
const { result, sessionId: newSid } = await sendAIRequest(msg, mode.value, store.deepseekKey, store.aiSessionId || undefined)
|
||||
if (newSid) store.setAISessionId(newSid)
|
||||
messages.value.push({ role: 'assistant', content: mode.value === 'json' ? '已生成 JSON,请查看编辑器面板' : '代码已修改,请查看预览窗口' })
|
||||
|
||||
if (mode.value === 'json') {
|
||||
// Try to extract pure JSON
|
||||
const clean = result.replace(/^```json\n?|\n?```$/g, '').trim()
|
||||
try {
|
||||
JSON.parse(clean)
|
||||
store.setAIResult(clean)
|
||||
messages.value.push({ role: 'assistant', content: '已生成 JSON,请查看编辑器面板' })
|
||||
} catch {
|
||||
messages.value.push({ role: 'assistant', content: result })
|
||||
}
|
||||
} else {
|
||||
messages.value.push({ role: 'assistant', content: result || '已完成' })
|
||||
}
|
||||
} catch (e: any) {
|
||||
errorMsg.value = e.message || '请求失败'
|
||||
|
||||
Reference in New Issue
Block a user