# Stage 9: Script Adaptation - Synchronize Script with Selected Frames

You are adapting the original script to match the actual selected and refined frames.

## Your Task

Read the original script and each selected frame image, then create an adapted script that precisely matches what was actually generated. Write to `script.json` in this directory.

## Why This Stage Exists

**The Problem:**
- Stage 3 creates an ideal script based on story understanding
- Stage 7 generates visual grids from that script
- Stage 8 extracts frames and the user selects/refines the best frame for each scene
- Selected frames may come from DIFFERENT grid iterations and be heavily refined
- Video generation (Stage 10) needs a script that matches the ACTUAL selected frames

**The Solution:**
This stage reconciles script with reality. You read:
1. Original script (`{{SCRIPT_PATH}}`) - the intent
2. Each scene's selected frame (listed below) - the reality

Then create an adapted script describing what's ACTUALLY shown in each selected frame.

## Input Files

**Original Script**: `{{SCRIPT_PATH}}`
**Selected Frames**: Listed in the "Selected Frames" section below — one per scene

Read all files carefully. Use your vision capabilities to analyze each frame image.

## Process

### Step 1: Analyze Each Selected Frame

For each scene's selected frame, analyze with vision:

**Frame-by-frame analysis:**
- What is ACTUALLY shown in this frame?
- Character appearances: age, clothing, facial features, expressions
- Location details: interior/exterior, specific objects visible, lighting
- Objects: what's present, their state (intact/destroyed/etc)
- Camera angle: what shot type is this REALLY? (close-up, medium, wide)
- Action captured: what moment is frozen in this frame?

**Pay attention to refinement feedback:**
- If a scene has refinement feedback, the user deliberately changed the frame
- The feedback tells you WHAT was changed and WHY
- The adapted script should reflect the refined version, not the original grid frame

**Consistency check:**
- Do character appearances stay consistent across selected frames?
- Do locations match between frames that share the same setting?
- Are objects consistent across frames?

### Step 2: Compare with Original Script

For each scene in `script.json`:
1. Does the selected frame match the scene's description in "what_is_shown"?
2. Is the shot_type accurate for what was generated?
3. Are character descriptions accurate?
4. Are object states accurate?

### Step 3: Create Adapted Script

Write a new `script.json` with the SAME structure as original, but with descriptions updated to match actual selected frames.

**What to preserve from original:**
- Scene numbers
- Duration targets (can adjust ±1-2 seconds if needed)
- Overall story structure and sequence
- Character IDs and location IDs

**What to adapt to reality:**
- `what_is_shown`: Rewrite to describe ACTUAL frame content
- `shot_type`: Update to match ACTUAL camera angle
- `camera`: Adjust if different from original
- Character descriptions: Match ACTUAL appearance in frames
- Object states: Match ACTUAL visibility and condition
- Location details: Match ACTUAL background/setting

**CRITICAL: Dialogue format for video generation:**
If scene has dialogue, use TWO text versions:
- `text_video`: Text for VIDEO GENERATOR - use `[pause]` markers for pauses, NOT ellipsis (...). Ellipsis causes Kling to switch speakers mid-line!
- `text_caption`: Text for CAPTIONS - with proper punctuation, ellipsis as written

Example:
```json
"dialogue": {
  "character": "paul",
  "text_video": "Yeah." [pause] "He can't swim.",
  "text_caption": "Yeah... he can't swim.",
  "delivery": "deadpan"
}
```

## Adapted Script Structure

Your output `script.json` should have:

```json
{
  "title": "Episode Title",
  "adaptation_notes": "Script adapted to match selected/refined frames. Key changes: [list major differences from original]",
  "style": {
    // Preserve from original script
  },
  "target_duration": 60,
  "scenes": [
    {
      "number": 1,
      "duration": 8,
      "action": "...",
      "what_is_shown": "[ADAPTED: describe actual selected frame content]",
      "frame_prompt": "[Description of what the selected frame shows — for reference]",
      "video_prompt": "[Optimized for video generation from this starting frame]",
      "shot_type": "[ACTUAL shot type from selected frame]",
      "camera": "...",
      "dialogue": null,
      "audio_ambient": "...",
      "frame_observations": "[Optional: notes on differences from original script]"
    }
  ],
  "characters": [
    {
      "id": "character_id",
      "actual_appearance": "[What's ACTUALLY shown in selected frames]",
      "appears_in": [1, 2, 3],
      "original_description": "[From assets.json for reference]"
    }
  ],
  "locations": [
    {
      "id": "location_id",
      "actual_appearance": "[What's ACTUALLY shown in selected frames]",
      "appears_in": [1, 2, 3]
    }
  ],
  "objects": [
    {
      "id": "object_id",
      "state_by_scene": {
        "1": "state description",
        "6": "different state"
      }
    }
  ],
  "total_duration": 72,
  "frame_analysis": {
    "frame_count": 16,
    "consistency_notes": "[Overall observations about character/location consistency across selected frames]",
    "major_differences": "[List key differences from original script]",
    "refinement_impact": "[How frame refinements changed the visual story]"
  }
}
```

## Critical Guidelines

1. **Selected Frames are Truth**: What you SEE in each selected frame is the source of truth, not the original script
2. **Be Precise**: "Medium shot of man" → "Medium shot of weathered man (50s, gray beard, blue cap, red flannel shirt)"
3. **Note Inconsistencies**: If character looks different between frames, note it
4. **Preserve Story**: Keep scene sequence and narrative flow from original
5. **Technical Accuracy**: Shot types must match actual camera angles (ECU, CU, medium, wide)
6. **Object States**: Track how objects change across frames (intact → destroyed)
7. **Add Detail**: Original might say "diner interior" - you should add "worn counter, coffee maker, window showing parking lot"
8. **Honor Refinements**: If a frame was refined with specific feedback, the adapted script should reflect the refined state

## When You're Done

Write these output files:

### 1. `user_message.txt`

A clear, conversational summary of what you did (2-4 paragraphs):

- **Major adaptations made**: List 3-5 key differences from original script
- **Consistency observations**: How well do selected frames maintain character/location consistency?
- **Refinement impact**: How did frame refinements change the visual story?
- **Readiness for video generation**: Are frame descriptions clear enough for video AI?

End with: "**Accept** if the adapted script accurately matches the selected frames. **Refine** if you want to adjust any scene descriptions."

### 2. `feedback_request.json`

```json
{
  "stage": "09-script-adapted",
  "iteration": "[ITERATION_NUMBER]",
  "user_message": "[COPY FROM user_message.txt]",
  "generated_file": "[PATH TO script.json]",
  "file_description": "script.json ([SIZE] bytes)",
  "options": [
    {
      "action": "accept",
      "label": "✓ Accept",
      "description": "Accept and proceed to video generation",
      "requires_text": false
    },
    {
      "action": "refine",
      "label": "✏️ Refine",
      "description": "Refine scene descriptions (provide feedback)",
      "requires_text": true
    },
    {
      "action": "regenerate",
      "label": "🔄 Regenerate",
      "description": "Completely regenerate adaptation from scratch",
      "requires_text": false
    }
  ]
}
```

This adapted script will be used in Stage 10 (Video Generation) to create precise prompts that match the selected frames.
