refactor(api,expo): 多智能体与会话收敛、回忆录兼容层移除、后端测试集大幅删减

- 对齐「多智能体收敛」与「回忆录 stories-first / markdown-first」方向:收紧运行时契约、
  删除过渡兼容路径与双轨逻辑,并同步更新客户端与文档。

- Chat:以 ChatOrchestrator 为实时编排入口;删除独立 conversation_agent,精简 prompts。
- Memoir:删除 memory_agent;MemoirOrchestrator、classification / story_route 与 prompts 收敛到
  prepare_batches + run_story_pipeline_for_category_batch 主链路。
- 将 agents 侧 processor 迁入 feature 层为 background_runner,并移除 features 下重复/过时
  processor 封装。

- 新增 history_store,强化「conversation_messages 为 DB 真源、Redis 为缓存」模型。
- 调整 models、repo、service、session_history;精简 WS message_types,重构 pipeline 与 router。

- 移除章节占位、整章再生等旧路径;章节列表与封面逻辑要求 story 关联;收紧 cover 资格与
  enqueue。
- helpers、repo、service、router、reading_segment_materialize、story_pipeline_sync、pdf_service
  等按 canonical markdown / cover_asset_id 收缩;删除 memoir_images/provider 等冗余。
- tasks:memoir_tasks、chapter_cover_tasks 等大幅瘦身;story_image_tasks 等与当前图片任务对齐。

- core:config、logging、redis、task_tracker 小幅调整。
- auth / user / payment / quota:路由或服务侧删减过时接口或逻辑(如 payment router 行数减少)。

- pyproject.toml、development.sh、.env.example / .env.production、README 等同步说明或变量。

- Alembic 0001_initial_schema 微调(与当前 schema 叙事一致的小改动)。

- 回忆录:types / mappers / api、章节页与 memoir 页与后端契约对齐;markdown-renderer 调整。
- 语音:删除 voice/player,voice-segment-store 相应精简。

- api/tests:删除 conftest 及绝大部分既有测试文件(websocket_baseline、conversation、memoir
  图片、PDF、SMS 等),属有意收缩/待按 backend-test-system 重建的信号。
- docs:新增多智能体收敛与移除兼容层计划摘要;更新 story-first 设计、backend-test-system、
  multi-agent-refactor-plan、实施总结等。

BREAKING CHANGE: 后端对外契约、回忆录章节字段与若干路由/任务行为已变更;大量 API 测试被移除,
  CI 若依赖这些用例需按新策略补测或调整流水线。
This commit is contained in:
Kevin
2026-03-22 16:45:57 +08:00
parent 70070216c4
commit 786ebf8ae6
122 changed files with 2802 additions and 7941 deletions

View File

@@ -7,6 +7,7 @@ import {
View,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Link } from 'expo-router';
import { useTranslation } from 'react-i18next';
import { AuthError, NetworkError } from '@/core/api/types';
@@ -240,37 +241,36 @@ export default function LoginScreen() {
) : null}
</View>
{/* Terms */}
{/* Terms — min-w-0 so EN text wraps; Link for /legal */}
<View className="flex-row items-start py-2" style={{ gap: 14 }}>
<Checkbox
checked={termsAccepted}
onCheckedChange={(v) => setTermsAccepted(v === true)}
className="mt-0.5 h-8 w-8 rounded-md border-2"
className="mt-0.5 h-8 w-8 shrink-0 rounded-md border-2"
indicatorClassName="rounded-md"
/>
<Text
className="flex-1 text-muted-foreground"
style={{
fontSize: compact ? 15 : 17,
lineHeight: compact ? 22 : 26,
}}
numberOfLines={3}
>
{t('login.termsIntro')}{' '}
<View className="min-w-0 flex-1">
<Text
className="font-bold text-primary underline"
onPress={() => {}}
className="text-muted-foreground"
style={{
fontSize: compact ? 15 : 17,
lineHeight: compact ? 22 : 26,
}}
>
{t('login.userAgreement')}
</Text>{' '}
{t('login.termsAnd')}{' '}
<Text
className="font-bold text-primary underline"
onPress={() => {}}
>
{t('login.privacyPolicy')}
{t('login.termsIntro')}{' '}
<Link href="/legal/terms" asChild>
<Text className="font-bold text-primary underline">
{t('login.userAgreement')}
</Text>
</Link>{' '}
{t('login.termsAnd')}{' '}
<Link href="/legal/privacy" asChild>
<Text className="font-bold text-primary underline">
{t('login.privacyPolicy')}
</Text>
</Link>
</Text>
</Text>
</View>
</View>
{/* Login button */}

View File

@@ -90,7 +90,7 @@ function StorySegmentCover({
asset,
contentWidth,
}: {
asset: NonNullable<ChapterReadingSegment['cover_image']>;
asset: NonNullable<ChapterReadingSegment['cover_asset']>;
contentWidth: number;
}) {
const url = asset?.url;
@@ -401,9 +401,9 @@ export default function ChapterScreen() {
);
}
const coverImageUrl = chapter.cover_image?.url ?? null;
const coverImageUrl = chapter.cover_asset?.url ?? null;
const canonicalMarkdown = (chapter.canonical_markdown ?? '').trim();
const renderedAssets = chapter.rendered_assets ?? chapter.images ?? [];
const renderedAssets = chapter.images ?? [];
const readingSegments = chapter.reading_segments;
const useReadingSegments =
Array.isArray(readingSegments) && readingSegments.length > 0;
@@ -528,9 +528,9 @@ export default function ChapterScreen() {
enableDropCap={i === 0}
showBottomDivider={false}
/>
{seg.cover_image ? (
{seg.cover_asset ? (
<StorySegmentCover
asset={seg.cover_image}
asset={seg.cover_asset}
contentWidth={contentWidth}
/>
) : null}

View File

@@ -1,45 +0,0 @@
import { useLocalSearchParams } from 'expo-router';
import React from 'react';
import { ActivityIndicator, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import WebView from 'react-native-webview';
import { ScreenHeader } from '@/components/screen-header';
import { useLegalDoc } from '@/features/profile/hooks';
import type { LegalDocType } from '@/features/profile/types';
const TITLES: Record<LegalDocType, string> = {
terms: '用户协议',
privacy: '隐私政策',
};
export default function LegalScreen() {
const { type } = useLocalSearchParams<{ type: string }>();
const docType = (
type === 'terms' || type === 'privacy' ? type : 'terms'
) as LegalDocType;
// TODO: 连接不上后端时可能一直 loading需加超时或展示错误态
const { data: html, isLoading } = useLegalDoc(docType);
return (
<View className="flex-1 bg-background">
<SafeAreaView className="flex-1">
<ScreenHeader title={TITLES[docType]} useSafeArea={false} />
{isLoading && (
<View className="flex-1 items-center justify-center">
<ActivityIndicator size="large" />
</View>
)}
{html && (
<WebView
source={{ html }}
style={{ flex: 1 }}
originWhitelist={['*']}
/>
)}
</SafeAreaView>
</View>
);
}

View File

@@ -1,6 +1,12 @@
import { Image } from 'expo-image';
import { router } from 'expo-router';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import {
Platform,
Pressable,
@@ -11,17 +17,18 @@ import {
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useTranslation } from 'react-i18next';
import { BookOpen, FileText } from 'lucide-react-native';
import { FileText } from 'lucide-react-native';
import { Icon } from '@/components/ui/icon';
import { Skeleton } from '@/components/ui/skeleton';
import { Text } from '@/components/ui/text';
import { ScreenGutter } from '@/constants/layout';
import { useCreateConversation } from '@/features/conversation/hooks';
import { buildFrameworkChapterPlaceholders } from '@/features/memoir/framework-chapter-keys';
import { useChapters, useCheckCoverGeneration } from '@/features/memoir/hooks';
import type { ChapterViewModel } from '@/features/memoir/types';
type ChapterVariant = 'completed' | 'drafting' | 'locked-left' | 'locked-large';
type ChapterVariant = 'completed' | 'drafting';
function getChapterVariant(vm: ChapterViewModel): ChapterVariant {
if (!vm.isEmpty) return 'completed';
@@ -29,11 +36,7 @@ function getChapterVariant(vm: ChapterViewModel): ChapterVariant {
}
function getWordCount(vm: ChapterViewModel): number {
if (vm.wordCount > 0) return vm.wordCount;
return (vm.sections ?? []).reduce(
(sum, s) => sum + (s.content?.length ?? 0),
0,
);
return vm.wordCount;
}
function formatWordCount(count: number): string {
@@ -44,33 +47,6 @@ function formatWordCount(count: number): string {
return count.toLocaleString();
}
function MemoirImagePlaceholder({
size,
muted = false,
}: {
size: number;
muted?: boolean;
}) {
return (
<View
className="items-center justify-center"
style={{
width: size,
height: size,
borderRadius: 12,
backgroundColor: muted ? 'rgba(0,0,0,0.03)' : 'rgba(128,119,166,0.08)',
opacity: muted ? 0.6 : 1,
}}
>
<Icon
as={BookOpen}
className={muted ? 'text-muted-foreground' : 'text-primary/40'}
size={size > 70 ? 32 : 24}
/>
</View>
);
}
function ChapterCardSkeleton() {
return (
<View className="overflow-hidden rounded-xl border border-border bg-card">
@@ -125,7 +101,6 @@ function ChapterCard({
if (variant === 'completed') {
const hasCoverImage = !!item.coverImageUrl;
const imageAreaHeight = hasCoverImage ? 192 : 96;
return (
<View
@@ -136,16 +111,16 @@ function ChapterCard({
...cardShadow,
}}
>
<View
style={{
height: imageAreaHeight,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'rgba(128,119,166,0.05)',
overflow: 'hidden',
}}
>
{hasCoverImage ? (
{hasCoverImage ? (
<View
style={{
height: 192,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'rgba(128,119,166,0.05)',
overflow: 'hidden',
}}
>
<Image
source={{ uri: item.coverImageUrl! }}
style={{
@@ -159,10 +134,8 @@ function ChapterCard({
}}
contentFit="cover"
/>
) : (
<MemoirImagePlaceholder size={48} muted />
)}
</View>
</View>
) : null}
<View style={{ padding: gutter, gap: 12 }}>
<View style={{ gap: 4, minHeight: 72 }}>
<Text
@@ -218,31 +191,28 @@ function ChapterCard({
...cardShadow,
}}
>
<View className="flex-row gap-4">
<MemoirImagePlaceholder size={80} muted />
<View className="flex-1">
<View>
<Text
className="text-xs font-medium text-muted-foreground"
selectable
>
{chapterLabel}
</Text>
<Text
variant="h4"
className="mt-0.5 text-foreground"
style={{ lineHeight: 28 }}
numberOfLines={2}
selectable
>
{item.title}
</Text>
</View>
<View className="mt-2 flex-row items-center gap-2">
<Text className="text-sm font-medium text-secondary" selectable>
{t('statusDrafting')}
</Text>
</View>
<View className="gap-1">
<View>
<Text
className="text-xs font-medium text-muted-foreground"
selectable
>
{chapterLabel}
</Text>
<Text
variant="h4"
className="mt-0.5 text-foreground"
style={{ lineHeight: 28 }}
numberOfLines={2}
selectable
>
{item.title}
</Text>
</View>
<View className="mt-2 flex-row items-center gap-2">
<Text className="text-sm font-medium text-secondary" selectable>
{t('statusDrafting')}
</Text>
</View>
</View>
<Pressable
@@ -257,150 +227,42 @@ function ChapterCard({
</View>
);
}
if (variant === 'locked-left') {
return (
<View
className="rounded-lg border border-border/80 bg-card/80"
style={{
padding: gutter,
width: contentWidth,
alignSelf: 'center',
opacity: 0.85,
...cardShadow,
}}
>
<View className="flex-row gap-4">
<View className="flex-1">
<Text
className="text-xs font-medium text-muted-foreground"
selectable
>
{chapterLabel}
</Text>
<Text
variant="h4"
className="mt-0.5 text-foreground/90"
style={{ lineHeight: 28 }}
numberOfLines={2}
selectable
>
{item.title}
</Text>
<View className="mt-2">
<Text
className="text-sm font-medium text-muted-foreground"
selectable
>
{t('statusLocked')}
</Text>
</View>
</View>
<MemoirImagePlaceholder size={72} muted />
</View>
<Pressable
className="mt-4 w-full items-center justify-center rounded-lg border border-border py-3"
style={{ borderCurve: 'continuous' }}
onPress={onContinuePress}
>
<Text className="text-sm font-medium text-muted-foreground">
{t('startChapter')}
</Text>
</Pressable>
</View>
);
}
// locked-large
return (
<View
className="overflow-hidden rounded-xl border border-border/80 bg-card/80"
style={{
width: contentWidth,
alignSelf: 'center',
opacity: 0.85,
...cardShadow,
}}
>
<View className="h-36 items-center justify-center bg-muted/30">
<MemoirImagePlaceholder size={64} muted />
</View>
<View style={{ padding: gutter, gap: 10 }}>
<View>
<Text
className="text-xs font-medium text-muted-foreground"
selectable
>
{chapterLabel}
</Text>
<Text
variant="h4"
className="mt-0.5 text-foreground/90"
style={{ lineHeight: 28 }}
numberOfLines={2}
selectable
>
{item.title}
</Text>
</View>
<View>
<Text
className="text-sm font-medium text-muted-foreground"
selectable
>
{t('statusPending')}
</Text>
</View>
<Pressable
className="mt-2 w-full items-center justify-center rounded-lg border border-border py-3"
style={{ borderCurve: 'continuous' }}
onPress={onContinuePress}
>
<Text className="text-sm font-medium text-muted-foreground">
{t('startChapter')}
</Text>
</Pressable>
</View>
</View>
);
return null;
}
function EmptyState({
t,
onStart,
disabled,
}: {
t: (key: string) => string;
onStart: () => void;
disabled?: boolean;
}) {
function MemoirLoadError({ onRetry }: { onRetry: () => void }) {
const { t } = useTranslation('memoir');
return (
<Pressable
className="items-center gap-6 rounded-2xl border border-dashed border-border bg-muted/20 p-10 active:opacity-90"
onPress={onStart}
disabled={disabled}
>
<Icon as={BookOpen} className="text-primary" size={36} />
<View className="items-center gap-2">
<Text variant="h3" className="text-center font-display text-foreground">
{t('emptyTitle')}
<View className="items-center gap-4 rounded-2xl border border-dashed border-border bg-muted/20 p-10">
<Text className="text-center text-base text-destructive">
{t('loadErrorMessage')}
</Text>
<Pressable
className="rounded-lg bg-primary px-6 py-3 active:opacity-90"
style={{ borderCurve: 'continuous' }}
onPress={onRetry}
>
<Text className="font-semibold text-primary-foreground">
{t('loadErrorRetry')}
</Text>
<Text className="text-center text-base font-medium text-muted-foreground">
{t('emptySubtitle')}
</Text>
</View>
</Pressable>
</Pressable>
</View>
);
}
export default function MemoirScreen() {
const { t } = useTranslation('memoir');
const { viewModels: chapters, isLoading, refetch } = useChapters();
const { viewModels: chapters, isLoading, isError, refetch } = useChapters();
const createConversation = useCreateConversation();
const checkCover = useCheckCoverGeneration();
const [refreshing, setRefreshing] = useState(false);
const didRunInitialCoverCheckRef = useRef(false);
const frameworkPlaceholders = useMemo(
() => buildFrameworkChapterPlaceholders(t as (key: string) => string),
[t],
);
useEffect(() => {
if (didRunInitialCoverCheckRef.current) return;
didRunInitialCoverCheckRef.current = true;
@@ -417,17 +279,17 @@ export default function MemoirScreen() {
}
}, [checkCover, refetch]);
const handleStartChapter = () => {
const handleStartChapter = useCallback(() => {
createConversation.mutate(undefined, {
onSuccess: (result) => {
router.push(`/(main)/conversation/${result.id}`);
},
});
};
}, [createConversation]);
const handleReadChapter = (chapterId: string) => {
const handleReadChapter = useCallback((chapterId: string) => {
router.push(`/(main)/chapter/${chapterId}`);
};
}, []);
return (
<View className="flex-1 bg-background">
@@ -443,7 +305,7 @@ export default function MemoirScreen() {
paddingTop: 24,
paddingBottom: 96,
gap: 24,
...(chapters.length === 0 && !isLoading
...(!isLoading && isError
? { flexGrow: 1, justifyContent: 'center' }
: {}),
}}
@@ -454,12 +316,19 @@ export default function MemoirScreen() {
<ChapterCardSkeleton />
<ChapterCardSkeleton />
</>
) : isError ? (
<MemoirLoadError onRetry={() => void refetch()} />
) : chapters.length === 0 ? (
<EmptyState
t={t as (key: string) => string}
onStart={handleStartChapter}
disabled={createConversation.isPending}
/>
frameworkPlaceholders.map((item) => (
<ChapterCard
key={item.id}
item={item}
variant="drafting"
t={t as (key: string) => string}
onReadPress={() => handleReadChapter(item.id)}
onContinuePress={handleStartChapter}
/>
))
) : (
chapters.map((item) => (
<ChapterCard

View File

@@ -56,6 +56,7 @@ export default function RootLayout() {
<Stack.Screen name="(tabs)" />
<Stack.Screen name="(auth)" />
<Stack.Screen name="(main)" />
<Stack.Screen name="legal" />
</Stack>
<PortalHost />
</NavigationThemeProvider>

View File

@@ -0,0 +1,79 @@
import { useLocalSearchParams, router } from 'expo-router';
import React, { useEffect, useMemo } from 'react';
import { ActivityIndicator, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import WebView from 'react-native-webview';
import { useTranslation } from 'react-i18next';
import { ScreenHeader } from '@/components/screen-header';
import { useLegalDoc } from '@/features/profile/hooks';
import type { LegalDocType } from '@/features/profile/types';
function normalizeType(raw: string | string[] | undefined): string {
if (Array.isArray(raw)) return raw[0] ?? '';
return raw ?? '';
}
export default function LegalScreen() {
const { t } = useTranslation('legal');
const { type } = useLocalSearchParams<{ type: string }>();
const rawType = useMemo(() => normalizeType(type), [type]);
const ready = rawType !== '';
const isValid = rawType === 'terms' || rawType === 'privacy';
useEffect(() => {
if (ready && !isValid) {
router.replace('/legal/terms');
}
}, [ready, isValid]);
const docType: LegalDocType = isValid ? rawType : 'terms';
const { data: html, isLoading } = useLegalDoc(docType, {
enabled: ready && isValid,
});
const title = docType === 'privacy' ? t('titlePrivacy') : t('titleTerms');
if (ready && !isValid) {
return (
<View className="flex-1 bg-background">
<SafeAreaView className="flex-1 items-center justify-center">
<ActivityIndicator size="large" />
</SafeAreaView>
</View>
);
}
if (!ready) {
return (
<View className="flex-1 bg-background">
<SafeAreaView className="flex-1 items-center justify-center">
<ActivityIndicator size="large" />
</SafeAreaView>
</View>
);
}
return (
<View className="flex-1 bg-background">
<SafeAreaView className="flex-1">
<ScreenHeader title={title} useSafeArea={false} />
{isLoading && (
<View className="flex-1 items-center justify-center">
<ActivityIndicator size="large" />
</View>
)}
{html && (
<WebView
source={{ html }}
style={{ flex: 1 }}
originWhitelist={['*']}
/>
)}
</SafeAreaView>
</View>
);
}

View File

@@ -0,0 +1,6 @@
import { Stack } from 'expo-router';
import React from 'react';
export default function LegalLayout() {
return <Stack screenOptions={{ headerShown: false }} />;
}

View File

@@ -28,14 +28,17 @@ function AlertDialogOverlay({
<FullWindowOverlay>
<AlertDialogPrimitive.Overlay
className={cn(
'absolute bottom-0 left-0 right-0 top-0 z-50 flex items-center justify-center bg-black/50 p-2',
// 不要用 items-centerRN 下子级不会在横向上拉伸,对话框宽度会随单行英文收缩导致不换行
// 见 https://medium.com/livefront/how-to-stop-text-overflow-in-react-native-8521d503e265
'absolute bottom-0 left-0 right-0 top-0 z-50 flex flex-col justify-center bg-black/50 px-4 py-2',
Platform.select({
web: 'animate-in fade-in-0 fixed',
web: 'animate-in fade-in-0 fixed items-center',
}),
className
)}
{...props}>
<NativeOnlyAnimatedView
className={Platform.OS === 'web' ? undefined : 'w-full'}
entering={FadeIn.duration(200).delay(50)}
exiting={FadeOut.duration(150)}>
<>{children}</>
@@ -58,9 +61,10 @@ function AlertDialogContent({
<AlertDialogOverlay>
<AlertDialogPrimitive.Content
className={cn(
'bg-background border-border z-50 flex w-full max-w-[calc(100%-2rem)] flex-col gap-4 rounded-lg border p-6 shadow-lg shadow-black/5 sm:max-w-lg',
'bg-background border-border z-50 flex w-full min-w-0 max-w-lg flex-col gap-4 rounded-lg border p-6 shadow-lg shadow-black/5',
Platform.select({
web: 'animate-in fade-in-0 zoom-in-95 duration-200',
native: 'max-w-full self-center',
}),
className
)}
@@ -74,7 +78,10 @@ function AlertDialogContent({
function AlertDialogHeader({ className, ...props }: ViewProps) {
return (
<TextClassContext.Provider value="text-center sm:text-left">
<View className={cn('flex flex-col gap-2', className)} {...props} />
<View
className={cn('w-full min-w-0 flex flex-col gap-2', className)}
{...props}
/>
</TextClassContext.Provider>
);
}
@@ -94,7 +101,7 @@ function AlertDialogTitle({
}: AlertDialogPrimitive.TitleProps & React.RefAttributes<AlertDialogPrimitive.TitleRef>) {
return (
<AlertDialogPrimitive.Title
className={cn('text-foreground text-lg font-semibold', className)}
className={cn('min-w-0 text-foreground text-lg font-semibold', className)}
{...props}
/>
);
@@ -102,12 +109,21 @@ function AlertDialogTitle({
function AlertDialogDescription({
className,
style,
...props
}: AlertDialogPrimitive.DescriptionProps &
React.RefAttributes<AlertDialogPrimitive.DescriptionRef>) {
return (
<AlertDialogPrimitive.Description
className={cn('text-muted-foreground text-sm', className)}
className={cn(
// shrink: RN 中 Text 在 flex 内需 flexShrink 才能按父宽换行
'w-full min-w-0 shrink text-muted-foreground text-sm',
className
)}
style={[
Platform.OS === 'web' ? undefined : { flexShrink: 1 },
style,
]}
{...props}
/>
);

View File

@@ -54,12 +54,6 @@ export const memoirApi = {
);
},
regenerateChapter(chapterId: string) {
return api.post<{ status: string; message: string }>(
`/api/chapters/${chapterId}/regenerate`,
);
},
fetchMemoirState() {
return api.get<MemoirState>('/api/memoir-state');
},

View File

@@ -0,0 +1,37 @@
import type { ChapterViewModel } from './types';
/**
* 与后端 `CHAPTER_ORDER` / `CHAPTER_CATEGORIES` 顺序一致;对应 i18n `memoir.frameworkChapters.*`。
*/
export const FRAMEWORK_CHAPTER_KEYS = [
'chapter1',
'chapter2',
'chapter3',
'chapter4',
'chapter5',
'chapter6',
'chapter7',
'chapter8',
] as const;
export type FrameworkChapterKey = (typeof FRAMEWORK_CHAPTER_KEYS)[number];
export function buildFrameworkChapterPlaceholders(
tr: (key: string) => string,
): ChapterViewModel[] {
return FRAMEWORK_CHAPTER_KEYS.map((key, orderIndex) => ({
id: `framework:${key}`,
title: tr(`frameworkChapters.${key}`),
category: '',
orderIndex,
isEmpty: true,
isNew: false,
hasImages: false,
allImagesReady: false,
pendingImageCount: 0,
failedImageCount: 0,
coverImageUrl: null,
updatedAt: null,
wordCount: 0,
}));
}

View File

@@ -6,28 +6,24 @@ function countByStatus(images: ImageAsset[], status: string): number {
export function toChapterViewModel(chapter: Chapter): ChapterViewModel {
const images = chapter.images ?? [];
const cover = chapter.cover_image ?? chapter.cover_asset ?? null;
const cover = chapter.cover_asset ?? null;
const imagesForStatus = cover ? [cover, ...images] : images;
const completedCount = countByStatus(imagesForStatus, 'completed');
const hasContent =
!!(chapter.canonical_markdown ?? '').trim() ||
!!(chapter.content ?? '').trim() ||
!!(chapter.summary ?? '').trim();
const wordCountFromSections = (chapter.sections ?? []).reduce(
(sum, s) => sum + (s.content?.length ?? 0),
0,
);
const wordCountFromMarkdown = (chapter.canonical_markdown ?? '').length;
const wordCount =
typeof chapter.word_count === 'number' && chapter.word_count >= 0
? chapter.word_count
: wordCountFromSections;
: wordCountFromMarkdown;
return {
id: chapter.id,
title: chapter.title,
category: chapter.category,
orderIndex: chapter.order_index,
isEmpty: chapter.status === 'empty' || !hasContent,
isEmpty: !hasContent,
isNew: chapter.is_new,
hasImages: imagesForStatus.length > 0,
allImagesReady:
@@ -36,7 +32,6 @@ export function toChapterViewModel(chapter: Chapter): ChapterViewModel {
countByStatus(imagesForStatus, 'pending') +
countByStatus(imagesForStatus, 'processing'),
failedImageCount: countByStatus(imagesForStatus, 'failed'),
sections: chapter.sections ?? [],
coverImageUrl: cover?.url ?? null,
updatedAt: chapter.updated_at,
wordCount,

View File

@@ -1,6 +1,6 @@
/**
* Markdown 渲染器:使用 react-native-markdown-display 渲染 canonical_markdown。
* 线上正文以 asset:// 或已解析的 https 为准;遗留 {{IMAGE:...}} 仅从展示层剥离,不作为协议。
* 线上正文以 asset:// 或已解析的 https 为准;{{IMAGE:...}} 仅从展示层剥离,不作为协议。
*/
import { Image } from 'expo-image';
@@ -25,13 +25,13 @@ function buildPlaceholderToAssetMap(
return map;
}
/** 移除遗留 IMAGE 占位符(不参与正文协议)。 */
export function stripLegacyImagePlaceholders(markdown: string): string {
/** 移除 IMAGE 占位符(不参与正文协议)。 */
export function stripImagePlaceholders(markdown: string): string {
return markdown.replace(PLACEHOLDER_RE, '').replace(/\n{3,}/g, '\n\n');
}
/**
* 预处理正文:先用 assets 替换可匹配的遗留占位符,再剥离剩余占位符。
* 预处理正文:先用 assets 替换可匹配的占位符,再剥离剩余占位符。
*/
export function replaceImagePlaceholders(
markdown: string,
@@ -47,7 +47,7 @@ export function replaceImagePlaceholders(
return `![${caption}](${asset.url})`;
});
}
return stripLegacyImagePlaceholders(out);
return stripImagePlaceholders(out);
}
/** 顶层正文段落body 直属,非列表/引用内)用于首行缩进 */

View File

@@ -44,37 +44,26 @@ export interface ImageAsset {
updated_at: string | null;
}
export interface ChapterSection {
content: string;
image: ImageAsset | null;
}
/** 章节详情:与 chapter_story_links 顺序一致,每段故事正文 + 主配图 */
export interface ChapterReadingSegment {
story_id: string;
body_markdown: string;
cover_image: ImageAsset | null;
cover_asset: ImageAsset | null;
}
export interface Chapter {
id: string;
title: string;
content: string;
order_index: number;
status: string;
category: string;
images: ImageAsset[];
cover_image: ImageAsset | null;
/** 列表接口与 cover_image 同构(资产化封面) */
cover_asset?: ImageAsset | null;
sections: ChapterSection[];
cover_asset: ImageAsset | null;
summary?: string;
/** 列表接口:与 canonical 一致的字符规模(后端 word_count */
word_count?: number;
/** 正文真源,优先用于渲染 */
canonical_markdown?: string | null;
/** 图片等资源映射,与 canonical_markdown 配合使用 */
rendered_assets?: ImageAsset[];
/** 有 story 编排时的分段阅读(正文不含故事标题,配图按故事) */
reading_segments?: ChapterReadingSegment[];
updated_at: string | null;
@@ -138,9 +127,8 @@ export interface ChapterViewModel {
allImagesReady: boolean;
pendingImageCount: number;
failedImageCount: number;
sections: ChapterSection[];
coverImageUrl: string | null;
updatedAt: string | null;
/** 优先使用列表接口的 word_count否则由 sections 推算 */
/** 优先使用列表接口的 word_count否则由 canonical_markdown 推算 */
wordCount: number;
}

View File

@@ -107,10 +107,14 @@ export function usePurgeUserData() {
// ─── Legal ───
export function useLegalDoc(type: LegalDocType) {
export function useLegalDoc(
type: LegalDocType,
options?: { enabled?: boolean },
) {
return useQuery({
queryKey: profileKeys.legal(type),
queryFn: () => profileApi.fetchLegalDoc(type),
staleTime: Infinity,
enabled: options?.enabled ?? true,
});
}

View File

@@ -1,9 +0,0 @@
/**
* Player is now fully implemented in hooks/use-player.ts as a self-contained
* React hook. expo-audio's hook-centric API (useAudioPlayer + useAudioPlayerStatus)
* makes a class-level player impractical — completion detection, source replacement,
* and lifecycle management all need React context.
*
* This file re-exports the hook for backward compatibility with any imports.
*/
export { usePlayer } from './hooks/use-player';

View File

@@ -18,21 +18,6 @@ const CREATE_TABLE_SQL = `
let initialized = false;
async function migrateLegacyVoiceMessageLocal(): Promise<void> {
const rows = await querySql<{ name: string }>(
`SELECT name FROM sqlite_master WHERE type='table' AND name='voice_message_local'`,
);
if (rows.length === 0) return;
const now = Date.now();
await executeSql(
`INSERT OR IGNORE INTO segment_outbox (conversation_id, voice_session_id, segment_index, file_uri, duration_ms, status, retry_count, created_at)
SELECT conversation_id, voice_session_id, 0, file_uri, duration_ms, 'sent', 0, ?
FROM voice_message_local`,
[now],
);
await executeSql(`DROP TABLE IF EXISTS voice_message_local`);
}
async function ensureTable(): Promise<void> {
if (initialized) return;
await executeSql(CREATE_TABLE_SQL);
@@ -40,7 +25,6 @@ async function ensureTable(): Promise<void> {
`CREATE UNIQUE INDEX IF NOT EXISTS uq_segment_outbox_voice_session_segment
ON segment_outbox(voice_session_id, segment_index)`,
);
await migrateLegacyVoiceMessageLocal();
initialized = true;
}
@@ -192,6 +176,3 @@ export const voiceSegmentStore = {
initialized = false;
},
} as const;
/** @deprecated 使用 voiceSegmentStore */
export const segmentOutbox = voiceSegmentStore;

View File

@@ -99,6 +99,10 @@ interface Resources {
};
explore: {};
home: {};
legal: {
titlePrivacy: 'Privacy Policy';
titleTerms: 'User Agreement';
};
memoir: {
chapterLabel: 'Chapter {{index}}';
chapterReading: {
@@ -125,6 +129,18 @@ interface Resources {
continueWriting: 'Continue Writing';
emptySubtitle: 'Chat with Echo to record your stories';
emptyTitle: 'No memoir yet';
frameworkChapters: {
chapter1: 'Childhood and upbringing';
chapter2: 'Education and young adulthood';
chapter3: 'Early career';
chapter4: 'Major achievements and peak moments';
chapter5: 'Setbacks, challenges, and turning points';
chapter6: 'Family and relationships';
chapter7: 'Beliefs and values';
chapter8: 'Life summary';
};
loadErrorMessage: 'Could not load chapters';
loadErrorRetry: 'Retry';
pageTitle: 'Memoir';
readMemory: 'Read Memory';
startChapter: 'Start Writing';

View File

@@ -0,0 +1,4 @@
{
"titleTerms": "User Agreement",
"titlePrivacy": "Privacy Policy"
}

View File

@@ -1,4 +1,16 @@
{
"frameworkChapters": {
"chapter1": "Childhood and upbringing",
"chapter2": "Education and young adulthood",
"chapter3": "Early career",
"chapter4": "Major achievements and peak moments",
"chapter5": "Setbacks, challenges, and turning points",
"chapter6": "Family and relationships",
"chapter7": "Beliefs and values",
"chapter8": "Life summary"
},
"loadErrorMessage": "Could not load chapters",
"loadErrorRetry": "Retry",
"chapterLabel": "Chapter {{index}}",
"chapterReading": {
"back": "Back",

View File

@@ -0,0 +1,4 @@
{
"titleTerms": "用户协议",
"titlePrivacy": "隐私政策"
}

View File

@@ -1,4 +1,16 @@
{
"frameworkChapters": {
"chapter1": "童年与成长背景",
"chapter2": "教育经历与青年时期",
"chapter3": "崭露头角",
"chapter4": "主要成就与巅峰时刻",
"chapter5": "挫折、挑战与重大转折",
"chapter6": "家庭与情感",
"chapter7": "信念与价值观",
"chapter8": "人生总结"
},
"loadErrorMessage": "无法加载章节列表",
"loadErrorRetry": "重试",
"chapterLabel": "第 {{index}} 章",
"chapterReading": {
"back": "返回",

View File

@@ -4,6 +4,7 @@ import commonEn from '../locales/en/common.json';
import conversationEn from '../locales/en/conversation.json';
import exploreEn from '../locales/en/explore.json';
import homeEn from '../locales/en/home.json';
import legalEn from '../locales/en/legal.json';
import memoirEn from '../locales/en/memoir.json';
import profileEn from '../locales/en/profile.json';
import appZh from '../locales/zh/app.json';
@@ -12,6 +13,7 @@ import commonZh from '../locales/zh/common.json';
import conversationZh from '../locales/zh/conversation.json';
import exploreZh from '../locales/zh/explore.json';
import homeZh from '../locales/zh/home.json';
import legalZh from '../locales/zh/legal.json';
import memoirZh from '../locales/zh/memoir.json';
import profileZh from '../locales/zh/profile.json';
@@ -28,6 +30,7 @@ export const namespaces = [
'conversation',
'home',
'explore',
'legal',
'memoir',
'profile',
] as const;
@@ -42,6 +45,7 @@ export const resources = {
conversation: conversationZh,
home: homeZh,
explore: exploreZh,
legal: legalZh,
memoir: memoirZh,
profile: profileZh,
},
@@ -52,6 +56,7 @@ export const resources = {
conversation: conversationEn,
home: homeEn,
explore: exploreEn,
legal: legalEn,
memoir: memoirEn,
profile: profileEn,
},