openmaic-custom/lib/export/use-export-classroom.ts
wyuc 86c8e0c543
feat: inline external assets at export for offline classrooms (#596) (#613)
* feat(export): add external-asset reference scanner for HTML inlining

* feat(export): add cached asset fetcher and data-uri encoder

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): recursively inline url() refs inside stylesheets

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): orchestrate inlining of links, scripts, images, and inline css

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(export): drop dead escapeAttr, harden importmap skip in scanner

* feat(export): inline importmap modules incl three/addons graph closure

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(export): extract shared module to break inline-assets import cycle

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): inline interactive-scene assets into .maic.zip at export

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(export): inline interactive assets in Resource Pack export too

* docs: document offline export asset inlining

* style: prettier formatting

* fix(test): resolve tsc type errors in export-classroom-inline test

* fix(export): keep importmap prefix fallback on partial failure, chunk base64, preserve media attr, log pptx inline failures

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* perf(export): parallelize asset fetching, share fetcher across scenes, promise-cache

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(export): retry transient asset fetches, bound concurrency, warn user on partial inline

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* perf(export): inline only woff2 fonts, drop woff/ttf siblings to halve size

Within @font-face blocks that have a woff2 url(), rewrite sibling woff/ttf/otf/eot
refs to url(about:invalid) instead of inlining them. Falls back to full inline for
ttf-only blocks. Drops katex output from ~1.79MB to ~0.71MB (~3x fewer font fetches).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* i18n(export): translate partial-inline warning and surface failed asset hosts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(proxy-media): follow redirects with per-hop SSRF revalidation (#398)

Replace blanket redirect rejection with a bounded 5-hop follow loop that
re-validates every Location target through validateUrlForSSRF before
fetching, preventing redirect-to-internal SSRF while allowing CDN
redirects (e.g. cdn.tailwindcss.com 302→/3.4.17).

* feat(export): route asset inlining through proxy-media to bypass CORS

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(export): keep importmap prefix fallback, forward proxy 4xx, cap proxy size, re-export FetchAsset

* style: prettier formatting for proxy-media + proxied-fetch

* fix(export): report CSS-nested asset failures in inline report

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
2026-05-31 18:36:39 +08:00

243 lines
8.2 KiB
TypeScript

'use client';
import { useState, useCallback } from 'react';
import { saveAs } from 'file-saver';
import { toast } from 'sonner';
import { useStageStore } from '@/lib/store/stage';
import { useI18n } from '@/lib/hooks/use-i18n';
import { db, getGeneratedAgentsByStageId } from '@/lib/utils/database';
import {
CLASSROOM_ZIP_FORMAT_VERSION,
CLASSROOM_ZIP_EXTENSION,
type ClassroomManifest,
type ManifestStage,
type ManifestAgent,
type ManifestScene,
type MediaIndexEntry,
} from './classroom-zip-types';
import { collectAudioFiles, collectMediaFiles, actionsToManifest } from './classroom-zip-utils';
import type { SpeechAction } from '@/lib/types/action';
import { createLogger } from '@/lib/logger';
import {
inlineHtmlAssets,
createAssetFetcher,
type InlineOptions,
type InlineReport,
} from './inline-assets';
import { createProxiedFetch } from './proxied-fetch';
import type { SceneContent } from '@/lib/types/stage';
export async function inlineSceneContent(
content: SceneContent,
options?: InlineOptions,
): Promise<{ content: SceneContent; report: InlineReport }> {
if (content?.type !== 'interactive' || !('html' in content) || !content.html) {
return { content, report: { inlined: [], failed: [] } };
}
const { html, report } = await inlineHtmlAssets(content.html, options);
return { content: { ...content, html }, report };
}
const log = createLogger('ExportClassroom');
export function useExportClassroom() {
const [exporting, setExporting] = useState(false);
const { t } = useI18n();
const exportClassroomZip = useCallback(async () => {
const { stage, scenes } = useStageStore.getState();
if (!stage?.id || scenes.length === 0) return;
setExporting(true);
const toastId = toast.loading(t('export.exporting'));
try {
const JSZip = (await import('jszip')).default;
const zip = new JSZip();
// 1. Read latest stage name from IndexedDB (may have been renamed on home page)
const freshStage = await db.stages.get(stage.id);
const latestName = freshStage?.name || stage.name;
// 2. Collect agents from DB
const agentRecords = await getGeneratedAgentsByStageId(stage.id);
// 3. Collect audio files
const audioFiles = await collectAudioFiles(scenes);
// 4. Collect media files (generated images/videos)
const mediaFiles = await collectMediaFiles(stage.id);
// 5. Build audioId → zipPath mapping for manifest
const audioIdToPath = new Map<string, string>();
for (const af of audioFiles) {
audioIdToPath.set(af.record.id, af.zipPath);
}
// 6. Build manifest
const manifestStage: ManifestStage = {
name: latestName,
description: stage.description,
language: stage.languageDirective,
style: stage.style,
createdAt: stage.createdAt,
updatedAt: stage.updatedAt,
};
const manifestAgents: ManifestAgent[] = agentRecords.map((a) => ({
name: a.name,
role: a.role,
persona: a.persona,
avatar: a.avatar,
color: a.color,
priority: a.priority,
}));
// Also include generatedAgentConfigs from stage if agents not in DB
if (manifestAgents.length === 0 && stage.generatedAgentConfigs?.length) {
for (const a of stage.generatedAgentConfigs) {
manifestAgents.push({
name: a.name,
role: a.role,
persona: a.persona,
avatar: a.avatar,
color: a.color,
priority: a.priority,
});
}
}
// Build agent ID → index mapping for multiAgent references
const agentIdToIndex = new Map<string, number>();
agentRecords.forEach((a, i) => agentIdToIndex.set(a.id, i));
if (stage.generatedAgentConfigs?.length && agentRecords.length === 0) {
stage.generatedAgentConfigs.forEach((a, i) => agentIdToIndex.set(a.id, i));
}
const aggregateReport: InlineReport = { inlined: [], failed: [] };
const sharedFetcher = createAssetFetcher({ fetchImpl: createProxiedFetch() });
const manifestScenes: ManifestScene[] = await Promise.all(
scenes.map(async (scene) => {
const { content, report } = await inlineSceneContent(scene.content, {
fetcher: sharedFetcher,
});
for (const u of report.inlined)
if (!aggregateReport.inlined.includes(u)) aggregateReport.inlined.push(u);
for (const f of report.failed)
if (!aggregateReport.failed.some((g) => g.url === f.url))
aggregateReport.failed.push(f);
return {
type: scene.type,
title: scene.title,
order: scene.order,
content,
actions: scene.actions
? actionsToManifest(scene.actions, audioIdToPath, agentIdToIndex)
: undefined,
whiteboards: scene.whiteboards,
...(scene.multiAgent?.enabled
? {
multiAgent: {
enabled: true,
agentIndices: (scene.multiAgent.agentIds ?? [])
.map((id) => agentIdToIndex.get(id))
.filter((i): i is number => i !== undefined),
directorPrompt: scene.multiAgent.directorPrompt,
},
}
: {}),
};
}),
);
// 7. Build mediaIndex
const mediaIndex: Record<string, MediaIndexEntry> = {};
for (const af of audioFiles) {
mediaIndex[af.zipPath] = {
type: 'audio',
format: af.record.format,
duration: af.record.duration,
voice: af.record.voice,
};
}
for (const mf of mediaFiles) {
mediaIndex[mf.zipPath] = {
type: 'generated',
mimeType: mf.record.mimeType,
size: mf.record.size,
prompt: mf.record.prompt,
};
}
// Check for missing audio references
for (const scene of scenes) {
for (const action of scene.actions ?? []) {
if (action.type === 'speech') {
const audioId = (action as SpeechAction).audioId;
if (audioId && !audioIdToPath.has(audioId)) {
const missingPath = `audio/${audioId}.mp3`;
mediaIndex[missingPath] = { type: 'audio', missing: true };
}
}
}
}
// 8. Assemble manifest
const manifest: ClassroomManifest = {
formatVersion: CLASSROOM_ZIP_FORMAT_VERSION,
exportedAt: new Date().toISOString(),
appVersion: process.env.npm_package_version || '0.0.0',
stage: manifestStage,
agents: manifestAgents,
scenes: manifestScenes,
mediaIndex,
};
zip.file('manifest.json', JSON.stringify(manifest, null, 2));
// 9. Add media blobs to ZIP
for (const af of audioFiles) {
zip.file(af.zipPath, af.record.blob);
}
for (const mf of mediaFiles) {
zip.file(mf.zipPath, mf.record.blob);
if (mf.record.poster) {
zip.file(mf.zipPath.replace(/\.\w+$/, '.poster.jpg'), mf.record.poster);
}
}
// 10. Generate and download
const zipBlob = await zip.generateAsync({ type: 'blob' });
const safeName = latestName.replace(/[\\/:*?"<>|]/g, '_') || 'classroom';
saveAs(zipBlob, `${safeName}${CLASSROOM_ZIP_EXTENSION}`);
if (aggregateReport.failed.length > 0) {
log.warn('Some interactive-scene assets could not be inlined:', aggregateReport.failed);
const hosts = [
...new Set(
aggregateReport.failed.map((f) => {
try {
return new URL(f.url).host;
} catch {
return f.url;
}
}),
),
];
toast.warning(t('export.inlinePartial', { count: aggregateReport.failed.length }), {
description: hosts.join(', '),
});
}
toast.success(t('export.exportSuccess'), { id: toastId });
} catch (error) {
log.error('Classroom ZIP export failed:', error);
toast.error(t('export.exportFailed'), { id: toastId });
} finally {
setExporting(false);
}
}, [t]);
return { exporting, exportClassroomZip };
}