Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"ignores": [
// Its placeholders read as HTML, and it opens without a heading by design.
".github/pull_request_template.md",
// The challenge spec bars a title in intro.md; the Intro screen shows it from the manifest.
"*/intro.md",
// Fixture Markdown is test data, not documentation.
"test/fixtures/**",
"**/node_modules/**"
Expand Down
400 changes: 400 additions & 0 deletions test/vibe-coding-mad-libs.test.mjs

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions vibe-coding-mad-libs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Vibe Coding Mad Libs

A 10-minute terminal challenge. The participant fills four Mad Libs blanks,
Theme, Subject, Audience, and Style, by inventing answers or rolling physical
dice against [`dice-sheet.md`](dice-sheet.md). Pi builds a self-contained
`index.html` from the sentence, shows it in Chrome beside the station, and
then makes one change the participant asks for.

| File | Role |
| --- | --- |
| `mad-libs.mjs` | Pi extension: the four-blank form and `/mad-libs` |
| `prompt.mjs` | Assembles and checks the sentence |
| `preview.mjs` | Serves `index.html` on `http://127.0.0.1:4321/` with live reload |
| `install` | Installs the Pi packages the extension imports |
| `setup`, `cleanup` | Copy the dice sheet and start the preview; stop it |

## Run it standalone

Install Pi and the adapter at the versions boothstrap pins, as in
boothstrap's authoring guide, then, from this repo's root:

```sh
challenge="$PWD/vibe-coding-mad-libs"
"$challenge/install"
mkdir -p tmp/mad-libs && cd tmp/mad-libs
"$challenge/setup"
GEMINI_API_KEY=... pi --model google/gemini-3.8-flash \
--skill "$challenge/skills/start-challenge/SKILL.md" \
--extension "$challenge/mad-libs.mjs" \
'/skill:start-challenge'
"$challenge/cleanup"
```

`setup` and `cleanup` treat the current directory as the workspace. Without
boothstrap there's no `show_preview`, so Pi gives the preview URL to open
yourself, and no points: Pi says which step you finished instead. Setup fails
if port 4321 is already in use.
35 changes: 35 additions & 0 deletions vibe-coding-mad-libs/challenge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"id": "vibe-coding-mad-libs",
"title": "Vibe Coding Mad Libs",
"tagline": "Fill four blanks, and Pi builds the web app you described.",
"icon": "laptop",
"type": "terminal",
"agent": "pi",
"model": "google/gemini-3.8-flash",
"vault_services": ["gemini"],
"points": 100,
"estimated_minutes": 10,
"learning_objectives": ["prompting an agent to build a web app", "iterating on generated code"],
"featured_tech": ["Pi", "Gemini", "HTML"],
"intro": "intro.md",
"steps": [
{
"id": "fill-prompt",
"title": "Fill in your prompt",
"hint": "Fill the four blanks. Invent answers, or roll dice and ask Pi to show dice-sheet.md.",
"done_when": "The participant sent a prompt naming a kind of app, a subject, an audience, and a style, with no blank left unfilled."
},
{
"id": "build-app",
"title": "Build and try your app",
"hint": "Let Pi build it, open the preview, and try it out.",
"done_when": "index.html holds a working self-contained app built from the prompt, and the participant confirmed they tried it in the preview."
},
{
"id": "improve-app",
"title": "Make it yours",
"hint": "Ask Pi for one change, then try the updated app.",
"done_when": "Pi made one change the participant asked for to index.html, and the participant confirmed they tried the updated app."
}
]
}
10 changes: 10 additions & 0 deletions vibe-coding-mad-libs/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
# Stops the app preview setup started. Safe to run twice, after a crash, or
# after setup failed.
set -eu

package_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
workspace=${BOOTHSTRAP_WORKSPACE:-$PWD}

[ -d "$workspace" ] || exit 0
exec node "$package_dir/preview.mjs" stop "$workspace"
77 changes: 77 additions & 0 deletions vibe-coding-mad-libs/dice-sheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Optional dice sheet

Roll a d6 for Theme, a d6 for Subject, a d20 for Audience, and a d20 for
Style, then type the matching options into the form. You can also invent any
answer, or mix your own answers with rolled ones. Pi can't roll for you.

## Theme (d6)

| Roll | Option |
| --- | --- |
| 1 | Dashboard |
| 2 | Game |
| 3 | Generator |
| 4 | Visualizer |
| 5 | Calculator / What-If Tool |
| 6 | Scrollytelling Story |

## Subject (d6)

| Roll | Option |
| --- | --- |
| 1 | Space & Astronomy |
| 2 | Food & Recipes |
| 3 | Music & Sound |
| 4 | Weather & Climate |
| 5 | Sports & Fitness |
| 6 | Internet Culture & Memes |

## Audience (d20)

| Roll | Option |
| --- | --- |
| 1 | First-time hackers |
| 2 | Exhausted night-shift nurses |
| 3 | Competitive dog groomers |
| 4 | Retired astronauts |
| 5 | Middle school science teachers |
| 6 | Touring musicians |
| 7 | Urban beekeepers |
| 8 | Long-haul truck drivers |
| 9 | Museum curators |
| 10 | Amateur storm chasers |
| 11 | Bakery owners |
| 12 | Marine biologists |
| 13 | Chess club captains |
| 14 | Film festival programmers |
| 15 | Community garden organizers |
| 16 | Ultramarathon runners |
| 17 | Librarians |
| 18 | Ski patrol |
| 19 | Podcast producers |
| 20 | Lighthouse keepers |

## Style (d20)

| Roll | Option |
| --- | --- |
| 1 | Cyberpunk |
| 2 | Cottagecore |
| 3 | Y2K Web 1.0 |
| 4 | Brutalist |
| 5 | Solarpunk |
| 6 | Dark Academia |
| 7 | Scandinavian Minimalism |
| 8 | Retro 80s Synthwave |
| 9 | Mid-Century Modern |
| 10 | Vaporwave |
| 11 | Art Nouveau |
| 12 | Deep Space |
| 13 | Terminal / Green Phosphor |
| 14 | Pastel Pop |
| 15 | Industrial Loft |
| 16 | Glassmorphism |
| 17 | Bauhaus |
| 18 | Art Deco |
| 19 | Claymorphism |
| 20 | Neon Noir |
7 changes: 7 additions & 0 deletions vibe-coding-mad-libs/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
# Installs the Pi packages the Mad Libs form extension imports, at the
# versions boothstrap's Pi uses, into this package's node_modules.
set -eu

package_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
npm ci --ignore-scripts --no-audit --no-fund --prefix "$package_dir"
14 changes: 14 additions & 0 deletions vibe-coding-mad-libs/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Tell Pi what to build by filling in one sentence:

> Build a [Theme] about [Subject] for [Audience] in [Style] style.

A form opens after Pi says hello. Type your own answers, or roll the dice
at the booth: press Esc and ask Pi to show `dice-sheet.md` to look up each
roll, then type `/mad-libs` to return to the form. The dice are optional,
and mixing rolled and invented answers is fine.

Pi then builds your app as one web page and offers to open it in Chrome
beside the station. Try it, ask Pi for one change, and try it again.

Your sentence and requests go to the Gemini model, so leave out anything
private. Everything here is wiped when you leave.
185 changes: 185 additions & 0 deletions vibe-coding-mad-libs/mad-libs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Pi extension: the four-blank Mad Libs form. It opens once Pi's welcome
// finishes, reopens with /mad-libs, and sends the filled sentence as the
// participant's message. Typing the sentence in Pi's editor works without it.

import { Input, matchesKey, truncateToWidth, wrapTextWithAnsi } from '@earendil-works/pi-tui'

import { FIELDS, PROMPT_TEMPLATE, assemblePrompt, fillSentence, findBlankField, hasUnfilledBlank, isFinishedPrompt } from './prompt.mjs'

/** The form's focus stops: one per field, then the Build button. */
const BUILD_BUTTON = FIELDS.length
const FOCUS_STOPS = FIELDS.length + 1

/** How many lines of the sentence preview the form shows. */
const PREVIEW_LINES = 5

/** Lists the text parts of a message's `content`, a string or an array of parts. */
function textParts(content) {
if (typeof content === 'string') return [content]

return content.filter((part) => part.type === 'text').map((part) => part.text)
}

/** True when this session's branch already holds a finished sentence from the participant. */
function promptAlreadySent(ctx) {
return ctx.sessionManager.getBranch().some((entry) => entry.type === 'message'
&& entry.message.role === 'user'
&& textParts(entry.message.content).some(isFinishedPrompt))
}

/**
* Builds the form component for `ctx.ui.custom`. `draft` keeps answers
* across closes, keyed by field; `keybindings` supplies Pi's submit and
* cancel keys; `done` receives the sentence, or null when cancelled.
*/
function createForm({ tui, theme, keybindings, draft, done }) {
const inputs = FIELDS.map((field) => {
const input = new Input({ prompt: '', placeholder: field.placeholder })
// setValue leaves the cursor at column zero; typing the draft puts it at the end.
if (draft[field.key]) input.handleInput(draft[field.key])

return input
})
let focusedStop = 0
let hasFocus = true
let error = ''

/** Moves focus to stop `target`, wrapping around both ends. */
function focusStop(target) {
focusedStop = (target + FOCUS_STOPS) % FOCUS_STOPS
inputs.forEach((input, index) => {
input.focused = hasFocus && index === focusedStop
})
tui.requestRender()
}

/** Sends the sentence, or points at the first blank field. */
function build() {
const blankField = findBlankField(draft)
if (blankField) {
error = `Fill in ${blankField.label} first.`
focusStop(FIELDS.indexOf(blankField))
return
}

done(assemblePrompt(draft))
}

focusStop(0)

return {
get focused() {
return hasFocus
},
set focused(value) {
hasFocus = value
focusStop(focusedStop)
},
handleInput(data) {
if (keybindings.matches(data, 'tui.select.cancel')) return done(null)
if (matchesKey(data, 'shift+tab')) return focusStop(focusedStop - 1)
if (matchesKey(data, 'tab')) return focusStop(focusedStop + 1)
if (keybindings.matches(data, 'tui.input.submit')) {
return focusedStop === BUILD_BUTTON ? build() : focusStop(focusedStop + 1)
}
if (focusedStop === BUILD_BUTTON) return

const field = FIELDS[focusedStop]
inputs[focusedStop].handleInput(data)
draft[field.key] = inputs[focusedStop].getValue()
error = ''
tui.requestRender()
},
render(width) {
const fit = (text) => truncateToWidth(text, Math.max(1, width))
const marker = (stop) => (focusedStop === stop ? '›' : ' ')
const lines = [
fit(theme.fg('accent', theme.bold('Fill in the blanks'))),
fit(theme.fg('muted', 'Tab/Shift+Tab: move Enter: next, then Build Esc: back to chat')),
]
FIELDS.forEach((field, index) => {
lines.push(fit(`${marker(index)} ${field.label}`))
lines.push(...inputs[index].render(width).map(fit))
})
lines.push(fit(`${marker(BUILD_BUTTON)} [ Build ]`), '')

const preview = wrapTextWithAnsi(fillSentence(draft), Math.max(1, width))
lines.push(...preview.slice(0, PREVIEW_LINES).map(fit))
if (preview.length > PREVIEW_LINES) lines.push(fit(theme.fg('muted', '… (your answers are kept in full)')))
if (error) lines.push(fit(theme.fg('warning', error)))

return lines
},
invalidate() {
inputs.forEach((input) => input.invalidate())
},
}
}

/** Registers the form, its /mad-libs command, and the typed-sentence check with Pi. */
export default function madLibs(pi) {
const draft = Object.fromEntries(FIELDS.map((field) => [field.key, '']))
let formOpen = false
let openAfterWelcome = false

/**
* Opens the form in Pi's terminal UI and sends the sentence it returns.
* Once the idea is sent, only an explicit /mad-libs (`requested`) says so.
*/
async function openForm(ctx, { requested }) {
if (ctx.mode !== 'tui' || formOpen) return
if (promptAlreadySent(ctx)) {
if (requested) ctx.ui.notify('Your idea is already sent. Ask Pi for a change to the app instead.', 'info')
return
}

formOpen = true
let sentence
try {
sentence = await ctx.ui.custom((tui, theme, keybindings, done) => createForm({ tui, theme, keybindings, draft, done }))
} catch {
ctx.ui.notify(`The form couldn't open. Type the sentence instead: ${PROMPT_TEMPLATE}`, 'warning')
return
} finally {
formOpen = false
}

if (!sentence) {
ctx.ui.notify('Form closed. Type /mad-libs to reopen it.', 'info')
return
}

pi.sendUserMessage(sentence, ctx.isIdle() ? undefined : { deliverAs: 'followUp' })
}

pi.registerCommand('mad-libs', {
description: 'Open the four-blank app idea form',
handler: async (_args, ctx) => openForm(ctx, { requested: true }),
})

pi.on('session_start', (event, ctx) => {
openAfterWelcome = event.reason === 'startup' && ctx.mode === 'tui' && !promptAlreadySent(ctx)
})

// Not awaited: the form stays open while the participant types, and
// holding the event would stall Pi until they finish.
pi.on('agent_settled', (_event, ctx) => {
if (!openAfterWelcome) return

openAfterWelcome = false
openForm(ctx, { requested: false }).catch((error) => ctx.ui.notify(`The form failed: ${error.message}`, 'error'))
})

pi.on('input', (event, ctx) => {
Comment thread
theycallmeswift marked this conversation as resolved.
Resolved
const typedSentenceWithBlank = event.source === 'interactive'
&& /^\s*Build an?\b/i.test(event.text)
&& hasUnfilledBlank(event.text)
&& !promptAlreadySent(ctx)
if (!typedSentenceWithBlank) return { action: 'continue' }

ctx.ui.setEditorText(event.text)
ctx.ui.notify(`Fill in all four blanks: ${PROMPT_TEMPLATE}`, 'warning')

return { action: 'handled' }
})
}
Loading
Loading