feat(engine): merge cloudflare-migration — paradigm D engine, BYOK proxy, story persistence (#95)

Squash-merge the cloudflare-migration branch (7 commits by Kai ki) into
staging with conflict resolution, feature integration, and bug fixes.

Engine:
- Paradigm D: single-stream Writer replacing dual-phase Plan/Beats
- Delete Architect agent; story bible generated via Writer <plan> tag
- Modular prompt architecture (segments/registry/builder)
- StreamRouter for tagged stream splitting (<plan>/<story>/<choices>)

Infrastructure:
- Cloudflare Workers deployment (wrangler.jsonc, OpenNext adapter)
- D1 database schema + Drizzle ORM (scaffolded, not yet active)
- R2 storage helpers (scaffolded, not yet active)
- Story persistence API routes + client-side persistence

BYOK (Bring Your Own Key):
- /api/llm/user-proxy with SSRF-protected LLM proxy (+ requireUser auth)
- CORS-aware fetch in ai-client: auto-detect CORS failure, fallback to
  server proxy transparently via OpenAI SDK custom fetch
- BYO config support added to classify-freeform and vision routes
- SettingsModal CORS privacy notice (keys never logged/stored)

SSE streaming:
- engineClient.ts: fetchSSE helper for progressive scene events
- startSession/requestScene accept optional emit callback
- Fix SSE error event field name (error → message) in scene/start routes

i18n integration:
- Wire buildLanguageDirective into paradigm D's prompt builder
- Update corsNotice i18n keys (zh-CN/en/ja) with CORS proxy privacy text
- Preserve Session.language + LanguageSwitcher from i18n commit

Co-authored-by: Kai ki <155355644+zbf1009@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Zonghao Yuan
2026-06-18 18:05:38 +08:00
committed by GitHub
parent 05bd7e229c
commit 0e4c2ebef4
78 changed files with 7396 additions and 919 deletions
+36 -15
View File
@@ -21,6 +21,7 @@ import { SettingsModal, readStoredPlayerName, readStoredVisionClick } from "@/co
import { annotateClick } from "@/lib/annotateClient";
import { loadClientTtsConfig } from "@/lib/clientTtsConfig";
import { collectBeatAudioForExport } from "@/lib/exportAudio";
import { loadFromLocalStorage } from "@/lib/clientStoryPersistence";
import { PRESETS } from "@/lib/presets";
import {
STORY_SHARE_STORAGE_KEY,
@@ -807,10 +808,6 @@ function PlayInner() {
const replayActiveRef = useRef(false);
const exportingStoryRef = useRef(false);
const exportingGalleryRef = useRef(false);
// Audio carried in from a `.infiplot` share file, keyed by `${sceneId}:${beatId}`.
// Survives scene swaps so a player who re-exports a replayed game keeps the
// baked voices that the original creator already paid to synth — they're
// free to embed back into the new gallery / share file.
const prebakedAudioRef = useRef<Record<string, string>>({});
// Original (CDN) URL of the currently-rendered scene image. Used as the key
// to revoke its blob: URL when the scene swaps. We track the ORIGINAL URL,
@@ -1192,8 +1189,6 @@ function PlayInner() {
setVisionClickEnabled(settings.visionClickEnabled);
const nextPlayerName = settings.playerName || undefined;
setSession((prev) => prev ? { ...prev, playerName: nextPlayerName } : prev);
// Refresh the BYO TTS config so a key entered mid-session takes effect
// immediately — byoTtsRef is otherwise only read once at mount.
const cfg = settings.ttsConfigured ? loadClientTtsConfig() : null;
byoTtsRef.current = cfg;
setByoTtsConfig(cfg);
@@ -1587,10 +1582,12 @@ function PlayInner() {
// ?custom=1 → 用户自定义 promptsessionStorage 取 ws/sg
// 后走 /api/start 现场生成
// ?share=1 → 首页上传的剧情分享 JSON,从第一幕开始本地回放
// ?storyId=<uuid> → 加载已保存的剧情(从 localStorage
const cardName = params.get("card");
const presetId = params.get("preset");
const isCustom = params.get("custom") === "1";
const isShare = params.get("share") === "1";
const storyId = params.get("storyId");
if (isShare) {
(async () => {
@@ -1629,11 +1626,6 @@ function PlayInner() {
replayIndexRef.current = 0;
replayActiveRef.current = imported.history.length > 1;
visitedBeatsRef.current = [first.scene.entryBeatId];
// Stash pre-baked audio (from doc.audioByBeatId) so it survives scene
// swaps and re-exports. Keyed by `${sceneId}:${beatId}`. Also seed the
// current beatAudioMap for the first scene so audio plays right away
// — the scene-change effect normally clears the map on transition,
// and bare beat ids "b1/b2/..." would otherwise miss prebaked entries.
if (doc.audioByBeatId) {
prebakedAudioRef.current = { ...doc.audioByBeatId };
const seed: Record<string, string> = {};
@@ -1710,11 +1702,43 @@ function PlayInner() {
// be tagged onto the local Session build for /api/scene calls).
const sessionLanguage: string = locale;
if (!cardName && !livePayload) {
if (!cardName && !livePayload && !storyId) {
router.replace("/");
return;
}
// ── Load saved story path ──
if (storyId) {
// TEMPORARY: localStorage-only mode (D1 disabled until auth integration)
const loadedSession = loadFromLocalStorage(storyId);
if (!loadedSession) {
setError("找不到保存的剧情");
return;
}
const firstScene = loadedSession.history[0]?.scene;
if (!firstScene) {
setError("剧情数据损坏");
return;
}
(async () => {
try {
const blobUrl = await getOrCreateBlobUrl(firstScene.imageUrl ?? "");
lastImageOriginalUrlRef.current = firstScene.imageUrl ?? "";
setSession(loadedSession);
setCurrentScene(firstScene);
setCurrentBeatId(firstScene.entryBeatId);
setImageUrl(blobUrl);
visitedBeatsRef.current = [firstScene.entryBeatId];
setOrientation(loadedSession.orientation ?? "landscape");
setPhase("ready");
track("scene_reached", { scene_index: loadedSession.history.length });
} catch (e) {
setError(String(e));
}
})();
return;
}
type PrebakedFirstAct = StartResponse & {
worldSetting: string;
styleGuide: string;
@@ -1766,9 +1790,6 @@ function PlayInner() {
fetchStart
.then(async (data) => {
// Resolve to a paintable src before committing to state. Proxy path:
// a fully-local blob: URL the browser paints atomically (no row-by-row
// "层层加载"). Direct path (default): the preloaded original URL.
const blobUrl = await getOrCreateBlobUrl(data.imageUrl);
lastImageOriginalUrlRef.current = data.imageUrl;