feat: 初始化AI写作工坊项目
- 实现基于Vue 3 + Vite的模块化架构 - 集成DeepSeek API进行内容生成 - 支持写作范式分析功能 - 添加环境变量配置支持 - 完整的项目结构和文档
This commit is contained in:
230
src/components/WriterPanel.vue
Normal file
230
src/components/WriterPanel.vue
Normal file
@@ -0,0 +1,230 @@
|
||||
<template>
|
||||
<aside class="w-[400px] flex flex-col border-r border-slate-700 bg-slate-800 shrink-0">
|
||||
<!-- 头部 -->
|
||||
<header class="p-4 border-b border-slate-700 flex items-center justify-between">
|
||||
<div class="flex items-center gap-4">
|
||||
<h1 class="font-bold text-lg text-white flex items-center gap-2">
|
||||
<span class="text-2xl">✍️</span> AI 写作工坊
|
||||
</h1>
|
||||
</div>
|
||||
<span class="text-xs px-2 py-1 rounded bg-blue-900 text-blue-300 border border-blue-700">Pro版</span>
|
||||
</header>
|
||||
|
||||
<!-- 内容区 -->
|
||||
<div class="flex-1 overflow-y-auto p-4 space-y-6">
|
||||
<!-- 写作任务 -->
|
||||
<section>
|
||||
<label class="block text-sm font-medium text-slate-400 mb-2 flex justify-between">
|
||||
1. 写作任务 (User Input)
|
||||
<span class="text-xs text-slate-500">{{ inputTask.length }} 字</span>
|
||||
</label>
|
||||
<textarea
|
||||
v-model="inputTask"
|
||||
class="w-full h-32 bg-slate-900 border border-slate-700 rounded-lg p-3 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none transition placeholder-slate-600 resize-none"
|
||||
placeholder="请输入具体的写作要求、主题、核心观点..."
|
||||
></textarea>
|
||||
</section>
|
||||
|
||||
<!-- 参考案例 -->
|
||||
<section>
|
||||
<div class="flex justify-between items-center mb-2">
|
||||
<label class="text-sm font-medium text-slate-400">2. 参考案例 (Style Ref)</label>
|
||||
<button @click="showRefInput = !showRefInput" class="text-xs text-blue-400 hover:text-blue-300">
|
||||
{{ showRefInput ? '取消' : '+ 添加案例' }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-if="showRefInput" class="mb-3 p-3 bg-slate-900 rounded-lg border border-blue-500/30">
|
||||
<input v-model="newRefTitle" placeholder="案例标题" class="w-full mb-2 bg-slate-800 border border-slate-700 rounded px-2 py-1 text-xs outline-none">
|
||||
<textarea v-model="newRefContent" placeholder="粘贴优秀的参考文本..." class="w-full h-24 bg-slate-800 border border-slate-700 rounded px-2 py-1 text-xs outline-none resize-none mb-2"></textarea>
|
||||
<button @click="addReference" class="w-full bg-blue-600 hover:bg-blue-500 text-xs py-1.5 rounded text-white">确认添加</button>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2">
|
||||
<div v-for="(ref, index) in references" :key="index" class="group flex items-center justify-between bg-slate-700/50 p-2 rounded border border-slate-700 hover:border-slate-600">
|
||||
<div class="flex items-center gap-2 overflow-hidden">
|
||||
<span class="text-lg">📄</span>
|
||||
<div class="flex flex-col min-w-0">
|
||||
<span class="text-xs font-medium text-slate-200 truncate">{{ ref.title }}</span>
|
||||
<span class="text-[10px] text-slate-500 truncate">{{ ref.content.substring(0, 20) }}...</span>
|
||||
</div>
|
||||
</div>
|
||||
<button @click="removeReference(index)" class="text-slate-500 hover:text-red-400 opacity-0 group-hover:opacity-100 transition px-2">×</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- 输出规范 -->
|
||||
<section>
|
||||
<label class="block text-sm font-medium text-slate-400 mb-2">3. 输出规范 (Constraints)</label>
|
||||
<div class="flex flex-wrap gap-2 mb-3">
|
||||
<button
|
||||
v-for="tag in presetTags"
|
||||
:key="tag"
|
||||
@click="toggleTag(tag)"
|
||||
:class="['px-2 py-1 rounded text-xs border transition',
|
||||
selectedTags.includes(tag)
|
||||
? 'bg-blue-600/20 border-blue-500 text-blue-300'
|
||||
: 'bg-slate-900 border-slate-700 text-slate-500 hover:border-slate-500']"
|
||||
>
|
||||
{{ tag }}
|
||||
</button>
|
||||
</div>
|
||||
<input
|
||||
v-model="customConstraint"
|
||||
type="text"
|
||||
class="w-full bg-slate-900 border border-slate-700 rounded px-3 py-2 text-xs focus:border-blue-500 outline-none"
|
||||
placeholder="补充其他要求"
|
||||
>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<!-- 底部操作区 -->
|
||||
<footer class="p-4 bg-slate-800 border-t border-slate-700 space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<label class="flex items-center gap-2 cursor-pointer">
|
||||
<input type="checkbox" v-model="showPromptDebug" class="hidden">
|
||||
<div class="w-8 h-4 bg-slate-900 rounded-full border border-slate-600 relative transition-colors" :class="{'bg-blue-900 border-blue-500': showPromptDebug}">
|
||||
<div class="w-2 h-2 bg-slate-400 rounded-full absolute top-1 left-1 transition-transform" :class="{'translate-x-4 bg-blue-400': showPromptDebug}"></div>
|
||||
</div>
|
||||
<span class="text-xs text-slate-500 select-none">预览构建的 Prompt</span>
|
||||
</label>
|
||||
<span class="text-xs text-slate-600">deepseek</span>
|
||||
</div>
|
||||
|
||||
<!-- API配置 -->
|
||||
<div class="space-y-2">
|
||||
<input
|
||||
v-model="apiUrl"
|
||||
type="text"
|
||||
class="w-full bg-slate-900 border border-slate-700 rounded px-3 py-2 text-xs focus:border-blue-500 outline-none"
|
||||
placeholder="API 地址"
|
||||
>
|
||||
<input
|
||||
v-model="apiKey"
|
||||
type="password"
|
||||
class="w-full bg-slate-900 border border-slate-700 rounded px-3 py-2 text-xs focus:border-blue-500 outline-none"
|
||||
placeholder="API Key"
|
||||
>
|
||||
</div>
|
||||
|
||||
<button
|
||||
@click="generateContent"
|
||||
:disabled="isGenerating || !inputTask"
|
||||
class="w-full py-3 rounded-lg font-bold text-white shadow-lg shadow-blue-900/20 flex items-center justify-center gap-2 transition-all transform active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
:class="isGenerating ? 'bg-slate-700' : 'bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500'"
|
||||
>
|
||||
<span v-if="isGenerating" class="animate-spin text-lg">↻</span>
|
||||
{{ isGenerating ? '正在思考与撰写...' : '开始生成文稿' }}
|
||||
</button>
|
||||
</footer>
|
||||
</aside>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useAppStore } from '../stores/app'
|
||||
import { buildPrompt } from '../utils/promptBuilder.js'
|
||||
import DeepSeekAPI from '../api/deepseek.js'
|
||||
import { marked } from 'marked'
|
||||
|
||||
const appStore = useAppStore()
|
||||
const {
|
||||
inputTask,
|
||||
references,
|
||||
showRefInput,
|
||||
newRefTitle,
|
||||
newRefContent,
|
||||
selectedTags,
|
||||
customConstraint,
|
||||
isGenerating,
|
||||
generatedContent,
|
||||
showPromptDebug,
|
||||
apiUrl,
|
||||
apiKey
|
||||
} = storeToRefs(appStore)
|
||||
|
||||
const presetTags = ['Markdown格式', '总分总结构', '数据支撑', '语气幽默', '严禁被动语态', '引用权威来源']
|
||||
|
||||
// 添加参考案例
|
||||
const addReference = () => {
|
||||
if (!newRefTitle.value || !newRefContent.value) return
|
||||
references.value.push({
|
||||
title: newRefTitle.value,
|
||||
content: newRefContent.value
|
||||
})
|
||||
newRefTitle.value = ''
|
||||
newRefContent.value = ''
|
||||
showRefInput.value = false
|
||||
}
|
||||
|
||||
// 删除参考案例
|
||||
const removeReference = (index) => {
|
||||
references.value.splice(index, 1)
|
||||
}
|
||||
|
||||
// 切换标签
|
||||
const toggleTag = (tag) => {
|
||||
if (selectedTags.value.includes(tag)) {
|
||||
selectedTags.value = selectedTags.value.filter(t => t !== tag)
|
||||
} else {
|
||||
selectedTags.value.push(tag)
|
||||
}
|
||||
}
|
||||
|
||||
// 生成内容
|
||||
const generateContent = async () => {
|
||||
if (!apiUrl.value || !apiKey.value || apiKey.value === 'YOUR_KEY') {
|
||||
alert('请先配置 API 地址和 API Key')
|
||||
return
|
||||
}
|
||||
|
||||
isGenerating.value = true
|
||||
generatedContent.value = ''
|
||||
|
||||
try {
|
||||
const api = new DeepSeekAPI({ url: apiUrl.value, key: apiKey.value })
|
||||
const prompt = buildPrompt(inputTask.value, [...selectedTags.value, customConstraint.value].filter(Boolean), references.value)
|
||||
|
||||
const response = await api.generateContent(prompt)
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
|
||||
const chunk = decoder.decode(value)
|
||||
const content = parseStreamResponse(chunk)
|
||||
generatedContent.value += content
|
||||
}
|
||||
} catch (error) {
|
||||
generatedContent.value = `## 错误\n\n${error.message}\n\n请检查 API 配置是否正确。`
|
||||
} finally {
|
||||
isGenerating.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 解析流式响应
|
||||
const parseStreamResponse = (chunk) => {
|
||||
const lines = chunk.split('\n').filter(line => line.trim())
|
||||
const contents = []
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('data: ')) {
|
||||
const data = line.slice(6)
|
||||
if (data === '[DONE]') continue
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data)
|
||||
const content = parsed.choices?.[0]?.delta?.content || ''
|
||||
if (content) contents.push(content)
|
||||
} catch (e) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return contents.join('')
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user