document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('article-form'); const outlineContainer = document.getElementById('outline-container'); const outlineElement = document.getElementById('outline'); const expandOutlineElement = document.getElementById('expand-outline'); const generateContentButton = document.getElementById('generate-content'); const contentContainer = document.getElementById('content-container'); const contentElement = document.getElementById('content'); const docType = document.getElementById('doc-type').value; // 获取文档类型 form.addEventListener('submit', function(event) { event.preventDefault(); const formData = new FormData(form); const requestData = { outline_model: formData.get('outline_model'), content_model: formData.get('content_model'), proxy_url: formData.get('proxy_url'), api_key: formData.get('api_key'), title: formData.get('title'), doc_type: formData.get('doc_type'), notice: formData.get('notice'), //doc_type: formData.get('doc_type') || docType }; outlineElement.value = 'Generating outline...'; outlineContainer.style.display = 'block'; fetch('/generate_outline', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(requestData) }) .then(response => response.json()) .then(data => { outlineElement.value = data.outline; }) .catch(error => { console.error('Error:', error); outlineElement.value = 'Failed to generate outline. Please check the console for more details.'; }); }); generateContentButton.addEventListener('click', function() { const expandOutline = expandOutlineElement.value; const outline = outlineElement.value; const title = document.getElementById('title').value; const contentModel = document.getElementById('content-model').value; const apiKey = document.getElementById('api-key').value; const proxyUrl = document.getElementById('proxy-url').value; const requestData = { expand_outline: expandOutline, outline: outline, title: title, content_model: contentModel, api_key: apiKey, proxy_url: proxyUrl, doc_type: docType }; contentElement.innerText = 'Generating content...'; contentContainer.style.display = 'block'; fetch('/generate_content', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(requestData) }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.body; }) .then(body => { const reader = body.getReader(); const decoder = new TextDecoder('utf-8'); return new ReadableStream({ start(controller) { function push() { reader.read().then(({ done, value }) => { if (done) { controller.close(); return; } controller.enqueue(decoder.decode(value)); push(); }); } push(); } }); }) .then(stream => { const reader = stream.getReader(); let fullContent = ''; function read() { reader.read().then(({ done, value }) => { if (done) { // 内容生成完成后,将其发送到后端进行文件写入 const title = document.getElementById('title').value; fetch('/write_to_file', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: title, content: fullContent }) }) .then(response => response.json()) .then(data => { console.log(data.message); }) .catch(error => { console.error('Error:', error); }); return; } fullContent += value; contentElement.innerText += value; read(); }); } read(); }) .catch(error => { console.error('Error:', error); contentElement.innerText = 'Failed to generate content. Please check the console for more details.'; }); }); });