From 395c55b6b09aa0ba6207120b02dda7212fedfec6 Mon Sep 17 00:00:00 2001 From: cocos02 Date: Mon, 15 Jun 2026 14:31:23 +0800 Subject: [PATCH] feat: enrich AI requests with scene context and project root path --- editor/components/AIPanel.vue | 24 ++++++++++++++++++++---- vite.config.ts | 4 ++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/editor/components/AIPanel.vue b/editor/components/AIPanel.vue index 7ec2fc4..6344ef0 100644 --- a/editor/components/AIPanel.vue +++ b/editor/components/AIPanel.vue @@ -16,9 +16,24 @@ function toggleMode() { mode.value = mode.value === 'json' ? 'code' : 'json' } +function buildMessage(userInput: string): string { + if (mode.value !== 'json') return `需求: ${userInput}` + + let ctx = `当前编辑文件: ${store.sourcePath}\n` + if (store.selectedScene) { + ctx += `选中场景: ${store.selectedScene.id}\n` + ctx += `当前场景 JSON:\n${JSON.stringify(store.selectedScene, null, 2)}\n\n` + } else if (Object.keys(store.gameData.scenes).length > 0) { + const { scenes, ...rest } = store.gameData + ctx += `全局配置:\n${JSON.stringify(rest, null, 2)}\n\n` + } + ctx += `需求: ${userInput}` + return ctx +} + async function send() { - const msg = inputText.value.trim() - if (!msg || loading.value) return + const userInput = inputText.value.trim() + if (!userInput || loading.value) return inputText.value = '' errorMsg.value = '' @@ -27,11 +42,12 @@ async function send() { return } - messages.value.push({ role: 'user', content: msg }) + const fullMessage = buildMessage(userInput) + messages.value.push({ role: 'user', content: userInput }) loading.value = true try { - const { result, sessionId: newSid } = await sendAIRequest(msg, mode.value, store.deepseekKey, store.aiSessionId || undefined) + const { result, sessionId: newSid } = await sendAIRequest(fullMessage, mode.value, store.deepseekKey, store.aiSessionId || undefined) if (newSid) store.setAISessionId(newSid) if (mode.value === 'json') { diff --git a/vite.config.ts b/vite.config.ts index 592408e..c0b0d11 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -79,8 +79,8 @@ function apiSavePlugin() { dedupMap.set(dedupKey, Date.now()) const modePrefix = mode === 'code' - ? '代码模式:直接修改 src/ 下的源码文件并保存。需求:' - : 'JSON模式:只返回修改后的 JSON 文本,不要写任何文件。需求:' + ? `当前项目根目录: ${__dirname}\n代码模式:直接修改 src/ 下的源码文件并保存。需求:` + : `当前项目根目录: ${__dirname}\nJSON模式:只返回修改后的 JSON 文本,不要写任何文件。需求:` const fullMessage = modePrefix + userMessage const args = ['run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']