Skip to content

StoryWriter Agent

Filelib/agents/storyWriter.ts
Orchestrated bylib/agents/storyOrchestrator.ts
Modelgemini-2.5-pro (Vertex AI)
Triggered byStory generation — after SafetyGuardian approves

What it does

Writes a complete children's story in the requested language and page count. Only runs after SafetyGuardian has approved the prompt. Produces story text per page plus an English image prompt for each page (used by the Illustrator agent).


Tools

ToolWhen calledParameters
submit_storyOnce, with the complete storytitle: string, pages: Page[]

The agent calls submit_story exactly once with the full story JSON.


Output structure

json
{
  "title": "Luna and the Golden Key",
  "pages": [
    {
      "pageNumber": 1,
      "text": "Story text in the target language...",
      "imagePrompt": "Detailed English illustration prompt for Imagen..."
    },
    {
      "pageNumber": 2,
      "text": "...",
      "imagePrompt": "..."
    }
  ]
}

Rules enforced by the agent prompt

  • Story text is written in the target language (Indonesian, French, Portuguese, etc.)
  • imagePrompt is always in English — required by Imagen for image generation
  • Page count is 4–6 depending on pageCount input (clamped: min 4, max 6)
  • Content is age-appropriate for 5–12, positive endings, simple vocabulary
  • Each page text is 2–4 sentences, suitable for one storybook page

Supported languages

CodeLanguage
enEnglish
idIndonesian
esSpanish
frFrench
ptPortuguese (Brazil)
pt_ptPortuguese (Portugal)
deGerman
itItalian
jaJapanese
koKorean
zhChinese (Simplified)
arArabic
hiHindi
ruRussian
viVietnamese
msMalay
filFilipino
tetTetum
myBurmese (Myanmar)
loLao
kmKhmer (Cambodia)

Flow

StoryOrchestrator (after SafetyGuardian approves)


StoryWriter LLM call

    └── submit_story({ title, pages[] })


    JSON extracted → returned to orchestrator


    Pages sent to Illustrator + Narrator (parallel)

Observability span

Span name: agent/StoryWriter

AttributeExample value
agent.nameStoryWriter
agent.modelgemini-2.5-pro
agent.languageid
agent.pageCount5
agent.pages_generated5
agent.successtrue
agent.duration_ms14200

Simulation test cases

Run with npm run simulate:

ScenarioExpected
English story — valid structuretitle + pages[], all fields present
Indonesian storyStory text contains Indonesian words
Exact page count — 5 pagespages.length === 5
Non-English story — image prompts in EnglishimagePrompt contains English words

Released under the MIT License.