refactor: AI session managed server-side, sessionId returned from API
This commit is contained in:
@@ -31,12 +31,12 @@ async function send() {
|
||||
return
|
||||
}
|
||||
|
||||
store.ensureAISession()
|
||||
messages.value.push({ role: 'user', content: msg })
|
||||
loading.value = true
|
||||
|
||||
try {
|
||||
const { result } = await sendAIRequest(store.aiSessionId, msg, mode.value, store.deepseekKey)
|
||||
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') {
|
||||
@@ -58,7 +58,7 @@ function onKeydown(e: KeyboardEvent) {
|
||||
}
|
||||
|
||||
function newSession() {
|
||||
store.newAISession()
|
||||
store.clearAISession()
|
||||
messages.value = []
|
||||
errorMsg.value = ''
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export async function sendAIRequest(sessionId: string, userMessage: string, mode: string, apiKey: string): Promise<{ result: string }> {
|
||||
export async function sendAIRequest(userMessage: string, mode: string, apiKey: string, sessionId?: string): Promise<{ result: string; sessionId: string }> {
|
||||
const resp = await fetch('/api/ai', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ sessionId, userMessage, apiKey, mode }),
|
||||
body: JSON.stringify({ userMessage, apiKey, mode, ...(sessionId ? { sessionId } : {}) }),
|
||||
})
|
||||
if (!resp.ok) {
|
||||
const err = await resp.json().catch(() => ({ error: 'request failed' }))
|
||||
|
||||
@@ -11,7 +11,7 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
const deepseekKey = ref(localStorage.getItem('deepseek_key') || '')
|
||||
const showAIPanel = ref(false)
|
||||
const aiResult = ref('')
|
||||
const aiSessionId = ref(localStorage.getItem('editor_ai_session') || '')
|
||||
const aiSessionId = ref('')
|
||||
|
||||
const selectedScene = computed(() => {
|
||||
if (!selectedNodeId.value) return null
|
||||
@@ -138,17 +138,9 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
|
||||
function setDeepseekKey(k: string) { deepseekKey.value = k; localStorage.setItem('deepseek_key', k) }
|
||||
|
||||
function ensureAISession() {
|
||||
if (!aiSessionId.value) {
|
||||
aiSessionId.value = crypto.randomUUID()
|
||||
localStorage.setItem('editor_ai_session', aiSessionId.value)
|
||||
}
|
||||
}
|
||||
function setAISessionId(id: string) { aiSessionId.value = id; localStorage.setItem('editor_ai_session', id) }
|
||||
|
||||
function newAISession() {
|
||||
aiSessionId.value = crypto.randomUUID()
|
||||
localStorage.setItem('editor_ai_session', aiSessionId.value)
|
||||
}
|
||||
function clearAISession() { aiSessionId.value = ''; localStorage.removeItem('editor_ai_session') }
|
||||
|
||||
function setAIResult(r: string) { aiResult.value = r }
|
||||
|
||||
@@ -167,6 +159,6 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
deepseekKey, showAIPanel, aiResult, aiSessionId,
|
||||
markDirty, loadJSON, exportJSON, addScene, deleteScene,
|
||||
updateScene, addChoice, updateChoice, deleteChoice, generateId,
|
||||
setSourcePath, setDeepseekKey, ensureAISession, newAISession, setAIResult, autoSave,
|
||||
setSourcePath, setDeepseekKey, setAISessionId, clearAISession, setAIResult, autoSave,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -40,9 +40,9 @@ function apiSavePlugin() {
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const { sessionId, userMessage, apiKey, mode } = JSON.parse(body)
|
||||
if (!userMessage || !sessionId) { res.writeHead(400); res.end(JSON.stringify({ error: 'missing fields' })); return }
|
||||
if (!userMessage) { res.writeHead(400); res.end(JSON.stringify({ error: 'missing fields' })); return }
|
||||
|
||||
const dedupKey = `${mode}_${userMessage}`
|
||||
const dedupKey = `${mode}_${sessionId || ''}_${userMessage}`
|
||||
const last = dedupMap.get(dedupKey) || 0
|
||||
if (Date.now() - last < 3000) {
|
||||
res.writeHead(429)
|
||||
@@ -56,8 +56,12 @@ function apiSavePlugin() {
|
||||
: 'JSON模式:只返回修改后的 JSON 文本,不要写任何文件。需求:'
|
||||
const fullMessage = modePrefix + userMessage
|
||||
|
||||
const args = ['run', '--model', 'deepseek/deepseek-v4-pro', '--format', 'json']
|
||||
if (sessionId) args.push('--session', sessionId)
|
||||
args.push(fullMessage)
|
||||
|
||||
const opencodeBin = resolve(__dirname, 'node_modules', '.bin', 'opencode')
|
||||
const child = spawn(opencodeBin, ['run', '--session', sessionId, '--model', 'deepseek', '--format', 'json', fullMessage], {
|
||||
const child = spawn(opencodeBin, args, {
|
||||
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
|
||||
timeout: 15000,
|
||||
})
|
||||
@@ -67,14 +71,35 @@ function apiSavePlugin() {
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.stderr.on('data', (d: Buffer) => stderr += d.toString())
|
||||
|
||||
child.on('close', (code) => {
|
||||
child.on('close', async (code) => {
|
||||
if (code !== 0) {
|
||||
res.writeHead(500)
|
||||
res.end(JSON.stringify({ error: 'opencode exited with code ' + code, stderr }))
|
||||
return
|
||||
}
|
||||
|
||||
let resolvedSessionId = sessionId
|
||||
if (!resolvedSessionId) {
|
||||
try {
|
||||
const listChild = spawn(opencodeBin, ['session', 'list', '--format', 'json', '--max-count', '1'], {
|
||||
timeout: 5000,
|
||||
env: { ...process.env, DEEPSEEK_API_KEY: apiKey || process.env.DEEPSEEK_API_KEY || '' },
|
||||
})
|
||||
let listOut = ''
|
||||
listChild.stdout.on('data', (d: Buffer) => listOut += d.toString())
|
||||
await new Promise<void>((resolveList) => listChild.on('close', () => {
|
||||
try {
|
||||
const sessions = JSON.parse(listOut)
|
||||
if (Array.isArray(sessions) && sessions.length > 0) {
|
||||
resolvedSessionId = sessions[0].id
|
||||
}
|
||||
} catch {}
|
||||
resolveList()
|
||||
}))
|
||||
} catch {}
|
||||
}
|
||||
|
||||
if (mode === 'json') {
|
||||
// try to extract JSON block from response
|
||||
const match = stdout.match(/```json\n?([\s\S]*?)\n?```|\{[\s\S]*\}/)
|
||||
const jsonStr = match ? (match[1] || match[0]) : stdout
|
||||
try { JSON.parse(jsonStr) } catch {
|
||||
@@ -83,10 +108,10 @@ function apiSavePlugin() {
|
||||
return
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ result: jsonStr }))
|
||||
res.end(JSON.stringify({ result: jsonStr, sessionId: resolvedSessionId || '' }))
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ result: stdout || 'done' }))
|
||||
res.end(JSON.stringify({ result: stdout || 'done', sessionId: resolvedSessionId || '' }))
|
||||
}
|
||||
})
|
||||
} catch (e: any) {
|
||||
@@ -99,7 +124,7 @@ function apiSavePlugin() {
|
||||
server.middlewares.use('/api/ai/sessions', (req: any, res: any) => {
|
||||
try {
|
||||
const opencodeBin = resolve(__dirname, 'node_modules', '.bin', 'opencode')
|
||||
const child = spawn(opencodeBin, ['session', 'list'], { timeout: 5000 })
|
||||
const child = spawn(opencodeBin, ['session', 'list', '--format', 'json'], { timeout: 5000 })
|
||||
let stdout = ''
|
||||
child.stdout.on('data', (d: Buffer) => stdout += d.toString())
|
||||
child.on('close', () => {
|
||||
|
||||
Reference in New Issue
Block a user