Skip to content

Continuation Example

Generate a story, then continue it with new beats while preserving narrative context.

Source: examples/continuation.ts

Code

typescript
import { Engine, Character } from '@spectator-ai/core'

async function main() {
  const engine = new Engine({ provider: 'anthropic' })

  const characters = [
    Character.create({ name: 'Kira', traits: ['brave', 'curious'] }),
    Character.create({ name: 'Vorn', traits: ['mysterious', 'wise'] }),
  ]

  // Generate initial story
  console.log('=== Generating initial story ===\n')

  const story = await engine.generate({
    characters,
    plot: {
      beats: [
        { name: 'The Meeting', type: 'setup' },
        { name: 'The Journey Begins', type: 'inciting-incident' },
      ],
    },
    instructions: 'Set in a fantasy world with ancient ruins',
  })

  console.log(story.toMarkdown())
  console.log(`\n[${story.sceneCount} scenes, ${story.wordCount} words]\n`)

  // Continue the story with new beats
  console.log('\n=== Continuing the story ===\n')

  const continued = await engine.continue(story, {
    beats: [
      { name: 'The Discovery', type: 'rising-action' },
      { name: 'The Confrontation', type: 'climax' },
    ],
    instructions: 'Raise the stakes dramatically',
  })

  // Print only the new scenes
  const newScenes = continued.scenes.slice(story.sceneCount)
  for (const scene of newScenes) {
    console.log(`--- ${scene.beat?.name ?? 'Scene'} ---`)
    console.log(scene.text)
    console.log()
  }

  console.log(`\n[${continued.sceneCount} total scenes, ${continued.wordCount} words]`)

  // Show character state evolution
  if (continued.characterStates) {
    console.log('\n=== Final character states ===')
    for (const state of continued.characterStates) {
      console.log(`${state.characterName}: ${state.emotionalState}`)
      if (state.internalConflict) {
        console.log(`  Conflict: ${state.internalConflict}`)
      }
    }
  }
}

main().catch(console.error)

Walkthrough

  1. Initial generation — Two beats (setup and inciting-incident) produce 2 scenes.
  2. Continuationengine.continue() takes the existing story and new beats. The engine preserves all prior context (summaries, character states).
  3. New scenes onlycontinued.scenes.slice(story.sceneCount) extracts just the new scenes.
  4. Character states — After continuation, continued.characterStates shows how characters evolved across all 4 scenes.

Key Concepts

  • Inline plot — Beats are defined as a plain object instead of using Plot.create() or Plot.template()
  • Context preservation — The engine passes previous scene summaries and character states to the LLM for consistency
  • Incremental generation — Build stories iteratively, adding beats as needed

Run It

bash
npx tsx examples/continuation.ts

Released under the MIT License.