feat/调整tts音色,调整封面图prompt,修复对话页输入框显示逻辑,待验证封面图生成功能
This commit is contained in:
@@ -73,7 +73,7 @@ TTS_PROVIDER=tencent
|
||||
# OPENAI_API_KEY=your_openai_api_key
|
||||
# 仅 TTS_PROVIDER=tencent 时生效,与 ASR 共用 TENCENT_SECRET_ID / TENCENT_SECRET_KEY
|
||||
# 音色 ID 见 https://cloud.tencent.com/document/product/1073/92668
|
||||
TTS_VOICE_TYPE=603004
|
||||
TTS_VOICE_TYPE=502001
|
||||
TTS_CODEC=mp3
|
||||
|
||||
# =============================================================================
|
||||
|
||||
@@ -62,7 +62,7 @@ class Settings(BaseSettings):
|
||||
# ── TTS (openai | tencent) ───────────────────────────────
|
||||
tts_provider: str = "tencent"
|
||||
openai_api_key: str = ""
|
||||
tts_voice_type: int = 603004 # Tencent 音色 ID,见 https://cloud.tencent.com/document/product/1073/92668
|
||||
tts_voice_type: int = 502001 # Tencent 音色 ID,见 https://cloud.tencent.com/document/product/1073/92668
|
||||
tts_codec: str = "mp3"
|
||||
|
||||
# ── WeChat Pay ───────────────────────────────────────────
|
||||
|
||||
@@ -165,7 +165,7 @@ class MemoirImagePromptService:
|
||||
"hero composition, evocative scene, emotionally resonant, "
|
||||
"cinematic framing, natural lighting, no text overlay."
|
||||
)
|
||||
details = (context_excerpt or "").strip()[:200]
|
||||
details = (context_excerpt or "").strip()[:500]
|
||||
if not details:
|
||||
details = "A personal life story scene with authentic emotional detail"
|
||||
return (
|
||||
|
||||
@@ -96,6 +96,18 @@ async def get_chapter(
|
||||
return await service.get_chapter(chapter_id, current_user.id)
|
||||
|
||||
|
||||
@router.post("/chapters/check-cover-generation")
|
||||
async def check_cover_generation(
|
||||
current_user: User = Depends(get_current_user),
|
||||
service: MemoirService = Depends(get_memoir_service),
|
||||
):
|
||||
"""
|
||||
检查可生成封面的章节(section 配图 > 3 且无已完成封面),
|
||||
若有则触发生成任务。已有封面的章节不再检查。
|
||||
"""
|
||||
return await service.check_and_trigger_cover_generation(current_user.id)
|
||||
|
||||
|
||||
@router.delete("/chapters/{chapter_id}")
|
||||
async def disable_chapter(
|
||||
chapter_id: str,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
"""Memoir service — 回忆录编排(章节生成、状态流转);通过 MemoryService 获取 evidence。"""
|
||||
|
||||
from app.core.logging import get_logger
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -18,7 +20,11 @@ from app.features.memoir.helpers import (
|
||||
chapter_to_dict,
|
||||
is_image_permanently_unavailable,
|
||||
)
|
||||
from app.features.memoir.models import Book, Chapter, ChapterSection
|
||||
from app.features.memoir.models import Book, Chapter, ChapterSection, MemoirImage
|
||||
from app.features.memoir.memoir_images.parser import build_initial_image_assets
|
||||
from app.features.memoir.memoir_images.serializers import image_dict_to_row_kwargs
|
||||
from app.features.memoir.memoir_images.prompting import MemoirImagePromptService
|
||||
from app.features.memoir.memoir_images.settings import MemoirImageSettings
|
||||
from app.features.memory.service import MemoryService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -210,6 +216,70 @@ class MemoirService:
|
||||
"covered_stages": state.covered_stages,
|
||||
}
|
||||
|
||||
async def check_and_trigger_cover_generation(self, user_id: str) -> dict:
|
||||
"""
|
||||
检查可生成封面的章节(section 配图 > 3 且无已完成封面),
|
||||
若有则触发生成任务。已有封面的章节不再检查。
|
||||
"""
|
||||
from app.tasks.memoir_tasks import generate_chapter_images
|
||||
|
||||
chapters = await repo.get_chapters_with_sections(
|
||||
user_id, self._db, active_only=True, is_new_only=None
|
||||
)
|
||||
triggered: List[str] = []
|
||||
for ch in chapters:
|
||||
if not ch.category or ch.status == "empty":
|
||||
continue
|
||||
sections = getattr(ch, "sections", None) or []
|
||||
section_image_count = sum(1 for s in sections if getattr(s, "image_id", None))
|
||||
images = getattr(ch, "images", None) or []
|
||||
cover_rec = next(
|
||||
(m for m in images if getattr(m, "section_id", None) is None),
|
||||
None,
|
||||
)
|
||||
if section_image_count <= 3:
|
||||
continue
|
||||
if cover_rec and (getattr(cover_rec, "status") or "").strip() == "completed":
|
||||
continue
|
||||
if cover_rec is None:
|
||||
img_settings = MemoirImageSettings.from_env()
|
||||
if img_settings.enabled:
|
||||
now_iso = datetime.now(timezone.utc).isoformat()
|
||||
cover_ph = {
|
||||
"placeholder": "{{{{{{{{IMAGE:章节封面}}}}}}}}",
|
||||
"description": "章节封面",
|
||||
"index": 0,
|
||||
}
|
||||
style = MemoirImagePromptService.CATEGORY_STYLE_MAP.get(
|
||||
ch.category or "", img_settings.default_style
|
||||
)
|
||||
cover_asset = build_initial_image_assets(
|
||||
[cover_ph],
|
||||
img_settings.provider,
|
||||
style,
|
||||
img_settings.default_size,
|
||||
now_iso,
|
||||
)[0]
|
||||
kwargs = image_dict_to_row_kwargs(cover_asset)
|
||||
cover_mi = MemoirImage(
|
||||
id=str(uuid.uuid4()).replace("-", "")[:32],
|
||||
chapter_id=ch.id,
|
||||
section_id=None,
|
||||
order_index=0,
|
||||
**kwargs,
|
||||
)
|
||||
self._db.add(cover_mi)
|
||||
await self._db.commit()
|
||||
await self._db.refresh(ch)
|
||||
logger.info("创建封面占位: chapter=%s", ch.id)
|
||||
try:
|
||||
generate_chapter_images.delay(ch.id)
|
||||
triggered.append(ch.id)
|
||||
logger.info("触发生成封面: chapter=%s", ch.id)
|
||||
except Exception as exc:
|
||||
logger.warning("封面生成任务派发失败: chapter=%s, error=%s", ch.id, exc)
|
||||
return {"triggered": triggered}
|
||||
|
||||
async def mark_memoir_read(self, user_id: str) -> dict:
|
||||
stmt = select(Chapter).where(
|
||||
Chapter.user_id == user_id, Chapter.is_new == True
|
||||
|
||||
@@ -342,7 +342,9 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
)
|
||||
db.add(sec)
|
||||
db.flush()
|
||||
if img_settings.enabled:
|
||||
# 封面:仅当 section 配图 > 3 时创建
|
||||
section_image_count = sum(1 for s in existing_sections if s.image_id)
|
||||
if img_settings.enabled and section_image_count > 3:
|
||||
stmt_cover = (
|
||||
select(MemoirImage)
|
||||
.where(
|
||||
@@ -381,7 +383,7 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
return ph
|
||||
content = (seg.get("content") or "").strip()
|
||||
desc = (content[:50] + "…") if len(content) > 50 else (content or "章节配图")
|
||||
return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc}
|
||||
return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc, "index": order_idx}
|
||||
|
||||
# 按顺序创建 section,每 3 个 section 对应 1 张配图
|
||||
for i, seg in enumerate(segments):
|
||||
@@ -416,8 +418,11 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
|
||||
sec.image_id = mi.id
|
||||
db.flush()
|
||||
|
||||
# 封面图:若无则创建 pending MemoirImage(section_id=None, order_index=0)
|
||||
if img_settings.enabled:
|
||||
# 封面图:仅当 section 配图 > 3 时创建 pending MemoirImage(section_id=None, order_index=0)
|
||||
existing_with_img = sum(1 for s in existing_sections if s.image_id)
|
||||
new_with_img = sum(1 for i in range(len(segments)) if _should_have_image(order_base + i))
|
||||
section_image_count = existing_with_img + new_with_img
|
||||
if img_settings.enabled and section_image_count > 3:
|
||||
stmt_cover = (
|
||||
select(MemoirImage)
|
||||
.where(
|
||||
@@ -1020,8 +1025,10 @@ def generate_chapter_images(self, chapter_id: str):
|
||||
db.commit()
|
||||
try:
|
||||
sections_ordered = sorted(sections, key=lambda s: getattr(s, "order_index", 0))
|
||||
first_content = (sections_ordered[0].content or "").strip() if sections_ordered else ""
|
||||
context_excerpt = " ".join(first_content.split("\n")[:5])[:200]
|
||||
full_content = "\n\n".join(
|
||||
(s.content or "").strip() for s in sections_ordered if (s.content or "").strip()
|
||||
)
|
||||
context_excerpt = full_content[:1500] if full_content else ""
|
||||
prompt_data = prompt_service.build_cover_prompt(
|
||||
chapter_title=chapter.title,
|
||||
chapter_category=chapter.category or "",
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
Animated,
|
||||
FlatList,
|
||||
InteractionManager,
|
||||
Keyboard,
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
Pressable,
|
||||
@@ -578,8 +579,20 @@ export default function ConversationScreen() {
|
||||
|
||||
const [input, setInput] = useState('');
|
||||
const [inputMode, setInputMode] = useState<InputMode>('text');
|
||||
const [isKeyboardVisible, setIsKeyboardVisible] = useState(false);
|
||||
const listRef = useRef<FlatList>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const onShow = () => setIsKeyboardVisible(true);
|
||||
const onHide = () => setIsKeyboardVisible(false);
|
||||
const subShow = Keyboard.addListener('keyboardDidShow', onShow);
|
||||
const subHide = Keyboard.addListener('keyboardDidHide', onHide);
|
||||
return () => {
|
||||
subShow.remove();
|
||||
subHide.remove();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const flattenedData = flattenMessagesForList(messages ?? []);
|
||||
|
||||
const isRecording = recorderStatus === 'recording';
|
||||
@@ -607,11 +620,12 @@ export default function ConversationScreen() {
|
||||
: t('connectionDisconnected');
|
||||
|
||||
const keyboardOffset = Platform.OS === 'ios' ? insets.top + 56 : 0;
|
||||
const kavEnabled = inputMode === 'text' && isKeyboardVisible;
|
||||
|
||||
return (
|
||||
<KeyboardAvoidingView
|
||||
style={styles.container}
|
||||
behavior="padding"
|
||||
behavior={kavEnabled ? 'padding' : undefined}
|
||||
keyboardVerticalOffset={keyboardOffset}
|
||||
>
|
||||
<View style={styles.column}>
|
||||
@@ -694,9 +708,14 @@ export default function ConversationScreen() {
|
||||
onChangeText={setInput}
|
||||
onSend={handleSend}
|
||||
inputMode={inputMode}
|
||||
onInputModeToggle={() =>
|
||||
setInputMode((m) => (m === 'text' ? 'voice' : 'text'))
|
||||
onInputModeToggle={() => {
|
||||
setInputMode((m) => {
|
||||
if (m === 'text') {
|
||||
Keyboard.dismiss();
|
||||
}
|
||||
return m === 'text' ? 'voice' : 'text';
|
||||
});
|
||||
}}
|
||||
onAddPress={() => {}}
|
||||
onStartRecording={handleStartRecording}
|
||||
onStopRecording={() => void stopRecording()}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { useFocusEffect } from '@react-navigation/native';
|
||||
import { Image } from 'expo-image';
|
||||
import { router } from 'expo-router';
|
||||
import React from 'react';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
import {
|
||||
Platform,
|
||||
Pressable,
|
||||
RefreshControl,
|
||||
ScrollView,
|
||||
View,
|
||||
useWindowDimensions,
|
||||
@@ -17,7 +19,7 @@ import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Text } from '@/components/ui/text';
|
||||
import { ScreenGutter } from '@/constants/layout';
|
||||
import { useCreateConversation } from '@/features/conversation/hooks';
|
||||
import { useChapters } from '@/features/memoir/hooks';
|
||||
import { useChapters, useCheckCoverGeneration } from '@/features/memoir/hooks';
|
||||
import type { ChapterViewModel } from '@/features/memoir/types';
|
||||
|
||||
type ChapterVariant = 'completed' | 'drafting' | 'locked-left' | 'locked-large';
|
||||
@@ -393,8 +395,26 @@ function EmptyState({
|
||||
|
||||
export default function MemoirScreen() {
|
||||
const { t } = useTranslation('memoir');
|
||||
const { viewModels: chapters, isLoading } = useChapters();
|
||||
const { viewModels: chapters, isLoading, refetch } = useChapters();
|
||||
const createConversation = useCreateConversation();
|
||||
const checkCover = useCheckCoverGeneration();
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
checkCover.mutate(undefined);
|
||||
}, [checkCover.mutate]),
|
||||
);
|
||||
|
||||
const handleRefresh = useCallback(async () => {
|
||||
setRefreshing(true);
|
||||
try {
|
||||
await checkCover.mutateAsync(undefined);
|
||||
await refetch();
|
||||
} finally {
|
||||
setRefreshing(false);
|
||||
}
|
||||
}, [checkCover.mutateAsync, refetch]);
|
||||
|
||||
const handleStartChapter = () => {
|
||||
createConversation.mutate(undefined, {
|
||||
@@ -414,6 +434,9 @@ export default function MemoirScreen() {
|
||||
<ScrollView
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
className="flex-1"
|
||||
refreshControl={
|
||||
<RefreshControl refreshing={refreshing} onRefresh={handleRefresh} />
|
||||
}
|
||||
contentContainerStyle={{
|
||||
paddingHorizontal: ScreenGutter,
|
||||
paddingTop: 24,
|
||||
|
||||
@@ -48,6 +48,12 @@ export const memoirApi = {
|
||||
);
|
||||
},
|
||||
|
||||
checkCoverGeneration() {
|
||||
return api.post<{ triggered: string[] }>(
|
||||
'/api/chapters/check-cover-generation',
|
||||
);
|
||||
},
|
||||
|
||||
regenerateChapter(chapterId: string) {
|
||||
return api.post<{ status: string; message: string }>(
|
||||
`/api/chapters/${chapterId}/regenerate`,
|
||||
|
||||
@@ -69,6 +69,19 @@ export function useDeleteChapter() {
|
||||
});
|
||||
}
|
||||
|
||||
export function useCheckCoverGeneration() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: () => memoirApi.checkCoverGeneration(),
|
||||
onSuccess: (data) => {
|
||||
if (data.triggered.length > 0) {
|
||||
queryClient.invalidateQueries({ queryKey: memoirKeys.chapters() });
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Memoir state ───
|
||||
|
||||
export function useMemoirState() {
|
||||
|
||||
Reference in New Issue
Block a user