openmaic-custom/lib/types/generation.ts
wyuc cec3c6aaea
Refactor widget actions into the scene action pipeline (#796)
* refactor: align widget actions with scene pipeline

* feat(edit): migrate legacy teacherActions off interactive content on load

The widget-actions pipeline no longer persists a teacherActions authoring
layer; playback reads only scene.actions. Add migrateInteractiveContent to
strip the dead field from existing documents as they load through migrateScene
(wired into all store/classroom load paths). Satisfies the #794 forward-migration
acceptance item.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(generation): default widget_setState.state to {} when the LLM omits it

The removed TeacherAction->Action converter always defaulted state to {}.
Restore that guard in the structured-output parser so an action missing
state can't forward state: undefined into the widget iframe, where
SET_WIDGET_STATE handlers dereference it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(edit): also strip nested widgetConfig.teacherActions in migration

Legacy WidgetConfig variants each declared their own teacherActions, so existing
documents can carry the dead field nested inside content.widgetConfig as well as
at the top level. Extend migrateInteractiveContent to drop both, preserving all
other widgetConfig fields.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(prompts): document stable widget-action selectors for diagram/game/viz3d

The interactive-actions prompt documented stable target selectors only for
procedural-skill and simulation widgets. Add the canonical selectors the
diagram / game / visualization3d content prompts actually emit (node IDs via
revealOrder; #zoom-in-btn / #canvas-container / #speed-slider / ...; #start-btn),
plus a rule to use widget_setState or a speech-only beat instead of guessing a
target when no stable selector is known (e.g. free-form code widgets). Hardens
against widget actions silently no-oping on non-existent selectors.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
2026-06-28 21:13:21 +08:00

245 lines
7.9 KiB
TypeScript

/**
* Generation Types - Two-Stage Content Generation System
*
* Stage 1: User requirements + documents → Scene Outlines (per-page)
* Stage 2: Scene Outlines → Full Scenes (slide/quiz/interactive/pbl with actions)
*/
import type { ActionType } from './action';
import type { MediaGenerationRequest } from '@/lib/media/types';
// ==================== PDF Image Types ====================
/**
* Image extracted from PDF with metadata
*/
export interface PdfImage {
id: string; // e.g., "img_1", "img_2"
src: string; // base64 data URL (empty when stored in IndexedDB)
pageNumber: number; // Page number in PDF
description?: string; // Optional description for AI context
storageId?: string; // Reference to IndexedDB (session_xxx_img_1)
width?: number; // Image width (px or normalized)
height?: number; // Image height (px or normalized)
}
/**
* Image mapping for post-processing: image_id → base64 URL
*/
export type ImageMapping = Record<string, string>;
// ==================== Stage 1 Input ====================
export interface UploadedDocument {
id: string;
name: string; // Original filename
type: 'pdf' | 'docx' | 'pptx' | 'txt' | 'md' | 'image' | 'other';
size: number; // Bytes
uploadedAt: Date;
contentSummary?: string; // Placeholder for parsing
extractedTopics?: string[]; // Placeholder for parsing
pageCount?: number;
storageRef?: string;
}
/**
* Simplified user requirements for course generation
* All details (topic, duration, style, etc.) should be included in the requirement text
*/
export interface UserRequirements {
requirement: string; // Single free-form text for all user input
userNickname?: string; // Student nickname for personalization
userBio?: string; // Student background for personalization
webSearch?: boolean; // Enable web search for richer context
interactiveMode?: boolean; // Enable Interactive Mode for interactive-first generation
taskEngineMode?: boolean; // Enable vocational task-engine generation path
}
// ==================== Stage 1 Output: Scene Outlines (Simplified) ====================
/**
* Widget outline configuration for interactive scenes
* Unified for both normal and ultra modes
*/
export interface WidgetOutline {
// Common field
concept?: string;
// Type-specific fields
keyVariables?: string[]; // simulation
diagramType?: 'flowchart' | 'mindmap' | 'hierarchy' | 'system'; // diagram
language?: 'python' | 'javascript' | 'typescript' | 'java' | 'cpp'; // code
gameType?: 'quiz' | 'puzzle' | 'strategy' | 'card' | 'action'; // game
visualizationType?: 'molecular' | 'solar' | 'anatomy' | 'geometry' | 'physics' | 'custom'; // visualization3d
objects?: string[]; // visualization3d
interactions?: string[]; // visualization3d
procedureType?: 'repair' | 'assembly' | 'inspection' | 'operation' | 'custom'; // procedural-skill
task?: string; // procedural-skill - task to perform
tools?: string[]; // procedural-skill - tools or materials involved
steps?: string[]; // procedural-skill - ordered procedure steps
successCriteria?: string[]; // procedural-skill - checks for completion
errorConsequences?: string[]; // procedural-skill - consequences for unsafe or incorrect actions
challenge?: string; // game - description of what player does
playerControls?: string[]; // game - what player controls
nodeCount?: number; // diagram - approximate node count
challengeType?: string; // code - type of coding challenge
}
/**
* Simplified scene outline
* Gives AI more freedom, only requiring intent description and key points
*/
export interface SceneOutline {
id: string;
type: 'slide' | 'quiz' | 'interactive' | 'pbl';
title: string;
description: string; // 1-2 sentences describing the purpose
keyPoints: string[]; // 3-5 core key points
teachingObjective?: string;
estimatedDuration?: number; // seconds
order: number;
languageNote?: string; // LLM-inferred language note for this scene
// Suggested image IDs (from PDF-extracted images)
suggestedImageIds?: string[]; // e.g., ["img_1", "img_3"]
// AI-generated media requests (when PDF images are insufficient)
mediaGenerations?: MediaGenerationRequest[]; // e.g., [{ type: 'image', prompt: '...', elementId: 'gen_img_1' }]
// Quiz-specific config
quizConfig?: {
questionCount: number;
difficulty: 'easy' | 'medium' | 'hard';
questionTypes: ('single' | 'multiple' | 'text')[];
};
/**
* @deprecated Use widgetType + widgetOutline instead
* Legacy interactive config - kept for backward compatibility only
*/
interactiveConfig?: {
conceptName: string;
conceptOverview: string;
designIdea: string;
subject?: string;
};
// PBL-specific config
pblConfig?: {
projectTopic: string;
projectDescription: string;
targetSkills: string[];
issueCount?: number;
/** Opt into role-play scenario planning on top of the standard PBL v2 structure. */
scenarioRoleplay?: boolean;
/** Optional scenario brief used only when scenarioRoleplay is true. */
scenarioBrief?: string;
};
// Widget fields (required for type === 'interactive' in unified mode)
widgetType?: WidgetType;
widgetOutline?: WidgetOutline;
}
// ==================== Stage 3 Output: Generated Content ====================
import type { PPTElement, SlideBackground } from '@openmaic/dsl';
import type { QuizQuestion } from './stage';
/**
* AI-generated slide content
*/
export interface GeneratedSlideContent {
elements: PPTElement[];
background?: SlideBackground;
remark?: string;
}
/**
* AI-generated quiz content
*/
export interface GeneratedQuizContent {
questions: QuizQuestion[];
}
// ==================== PBL Generation Types ====================
import type { PBLProjectConfig } from '@/lib/pbl/types';
import type { PBLProjectV2 } from '@/lib/pbl/v2/types';
/**
* AI-generated PBL content.
*
* PBL v2 generation returns a legacy-compatible `projectConfig` plus the full
* v2 payload so existing storage/rendering paths can migrate incrementally.
*/
export interface GeneratedPBLContent {
projectConfig: PBLProjectConfig;
projectV2?: PBLProjectV2;
}
// ==================== Interactive Generation Types ====================
import type { WidgetConfig, WidgetType } from './widgets';
/**
* Scientific model output from scientific modeling stage
*/
export interface ScientificModel {
core_formulas: string[];
mechanism: string[];
constraints: string[];
forbidden_errors: string[];
}
/**
* AI-generated interactive content
*/
export interface GeneratedInteractiveContent {
html: string;
scientificModel?: ScientificModel;
widgetType?: WidgetType;
widgetConfig?: WidgetConfig;
}
// ==================== Legacy Types (for compatibility) ====================
export interface SuggestedSlideElement {
type: 'text' | 'image' | 'shape' | 'chart' | 'latex' | 'line';
purpose: 'title' | 'subtitle' | 'content' | 'example' | 'diagram' | 'formula' | 'highlight';
contentHint: string;
position?: 'top' | 'center' | 'bottom' | 'left' | 'right';
chartType?: 'bar' | 'line' | 'pie' | 'radar';
textOutline?: string[];
}
export interface SuggestedQuizQuestion {
type: 'single' | 'multiple' | 'short_answer';
questionOutline: string;
suggestedOptions?: string[];
targetConceptId?: string;
difficulty: 'easy' | 'medium' | 'hard';
}
export interface SuggestedAction {
type: ActionType;
description: string;
timing?: 'start' | 'middle' | 'end' | 'after-content';
}
// ==================== Generation Session ====================
export interface GenerationProgress {
currentStage: 1 | 2 | 3;
overallProgress: number; // 0-100
stageProgress: number; // 0-100
statusMessage: string;
scenesGenerated: number;
totalScenes: number;
errors?: string[];
}
export interface GenerationSession {
id: string;
requirements: UserRequirements;
sceneOutlines?: SceneOutline[];
progress: GenerationProgress;
startedAt: Date;
completedAt?: Date;
generatedStageId?: string;
}