3 Commits

Author SHA1 Message Date
Georg 7c5d194376 Stabilize TTS voice reload and reconnect logging 2026-05-19 17:10:21 +02:00
Georg 256cc2c7a7 Fix autosave resume choice restoration 2026-05-19 15:44:40 +02:00
Georg 90f81ee1b7 Prepare Ink Coolify release branch 2026-05-19 15:25:23 +02:00
97 changed files with 245 additions and 14249 deletions
-8
View File
@@ -1,8 +0,0 @@
{
"permissions": {
"allow": [
"Bash(wc:*)",
"Bash(git -C /workspaces/ai.interactive.fiction log --oneline -15)"
]
}
}
-19
View File
@@ -1,19 +0,0 @@
FROM node:18
# Install basic development tools
RUN apt update && apt install -y less git procps
# Install Kokoro JS dependencies if needed
RUN apt install -y build-essential python3
# Ensure default `node` user has access to `sudo`
ARG USERNAME=node
RUN apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
# Set the default user
USER node
# Set working directory
WORKDIR /workspace
-25
View File
@@ -1,25 +0,0 @@
{
"name": "Node.js Development",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
],
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash"
}
}
}
}
},
"forwardPorts": [3001],
"postCreateCommand": "npm install",
"remoteUser": "node"
}
-23
View File
@@ -1,23 +0,0 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/explicit-module-boundary-types": "off",
"no-console": "off"
},
"env": {
"node": true,
"jest": true
}
}
-92
View File
@@ -1,92 +0,0 @@
# Markup Guidelines
This file documents author-facing Ink tag conventions. The active parser normalizes tags into structured `StoryTag` objects before they reach the UI.
## Implemented Tag Forms
Use bracket tags for titles, filenames, and longer text:
```ink
#chapter[Eibenreith]
#image[statue.png](square)
#music[Kaiserpunk Waltz.mp3](crossfade, loop, lead=8)
#sfx[church-bells.ogg](max=8, fade)
#score[You reached an ending.]
#achievement[First Steps]
#alert[Try examining the room.]
```
Use colon tags for short identifiers, categories, and choice keys:
```ink
#action:movement
#key:l
#sort:last
#gated:noble
```
Bare flags are accepted as tags with no value:
```ink
#optional
```
## Right-Page Glossary Notes
Glossary notes are story tags scoped to the paragraph/block they belong to. They affect only the right-page story rendering, never choice text or command history.
```ink
The conductor points toward Eibenreith.
#gloss[Eibenreith](A fictional alpine town in the Kaiserpunk setting.)
```
The bracket value is the visible term to find. The parenthesized value is the note shown on hover/focus. The renderer marks every matching instance of the term in the same right-page block. The tag is not displayed and is not sent to TTS. Avoid raw Ink control characters in the explanation; `|`, `{`, and `}` must be escaped in Ink as `\|`, `\{`, and `\}` if they are needed literally.
## Choice Metadata
Choice tags are placed on the Ink choice they belong to:
```ink
* [__Schaue__: Aus dem Fenster.]
#action:orientation
#key:l
```
Implemented choice metadata:
- `#key:x`: reserves keyboard key `X` for the choice.
- `#letter[x]`: older equivalent for reserving keyboard key `X`.
- `#action:group` or `#action[group]`: stores a category/template hint.
The current UI renders all choices in one list. Explicit keys are assigned first; choices without explicit keys receive `1` through `0`, then `A` through `Z` in visible order while skipping explicit keys. `#optional` choices are displayed italic. Grouping columns, stable shuffling, `#gated[...]`, and `#sort[...]` are documented authoring conventions or future metadata, not fully implemented UI behavior yet.
## Popup And End-State Tags
These tags may appear as Ink global tags, paragraph tags, or empty tag-only lines. They are dispatched through the same tag channel as media tags.
```ink
#score[You reached the quiet ending.]
#error[The story ended unexpectedly.]
#achievement[First Steps]
#alert[Try examining objects before using them.]
```
- `#score[...]`: intended ending. When the turn reaches `inputMode: end`, the UI shows a localized ending popup with the tag value as the optional message.
- `#error[...]`: unrecoverable ending. The UI shows a localized error popup with the tag value as the optional message. The Ink engine emits this automatically if Ink runs out of content without an explicit `#score[...]` or `#error[...]`.
- `#achievement[...]`: queued localized achievement popup while the game continues.
- `#alert[...]`: queued localized player hint/tutorial popup while the game continues.
## Existing Media And Structure Tags
```ink
#chapter[Title]
#section
#textblock
#image[filename.png](landscape)
#image[filename.png](portrait pause=2)
#image[filename.png](square lead=1.5)
#music[track.mp3](crossfade, loop, lead=4)
#sfx[file.ogg](max=8 fade fade-duration=2)
```
Asset filenames resolve relative to the configured image, music, and sound folders.
+55 -209
View File
@@ -1,257 +1,103 @@
# AI Interactive Fiction # AI Interactive Fiction - Ink Coolify Release
AI Interactive Fiction is a web and CLI text adventure prototype that combines a deterministic world model with LLM-assisted command interpretation and narrative output. The web client presents the story as an animated, novel-like book page with synchronized text animation, optional TTS, music, and sound effects. This branch is the deployable Ink edition of the AI Interactive Fiction client/server. It contains the browser UI, the Ink server, the Eibenreith Ink source, compiled Ink output, media assets, fonts, locale files, and Docker/Coolify configuration.
## Quick Start The full multi-engine development tree lives on `main`. The historical prototype is intentionally not part of this branch; it is preserved on `codex/archive-prototype` and tag `prototype-archive-2026-05-19`.
Use Node.js 22 LTS for development. The project accepts Node >= 18.17, but current development has been done on Node 22. ## Local Ink Development
Use Node.js 22 LTS.
```powershell ```powershell
nvm install 22 nvm install 22
nvm use 22 nvm use 22
npm install npm install
npm run build
npm run dev npm run dev
``` ```
`npm run dev` and `npm run start` use `DEFAULT_GAME_ENGINE` from `.env` to choose the active engine. Supported values are `ink`, `yaml`, and `zcode`. The engine-specific scripts remain available when you want to bypass the default. `npm run dev` starts the Ink server through `ts-node` and watches `src/`, `data/ink-src/`, and `config/engines/ink.json`. The server compiles the configured Ink source when it starts.
Set `PORT` to choose a port; the server will try the next few ports if the requested one is already in use. Current engine defaults are YAML `3001`, Z-code `3002`, and Ink `3003` before port fallback. Useful commands:
## Commands
```powershell ```powershell
npm run dev # Start the web UI through ts-node/nodemon npm run build # Compile TypeScript to dist/
npm run start # Build/run the configured default engine from dist/ npm run start # Run the compiled Ink server
npm run dev:ink # Start the Ink engine server, watch ink source, compile on restart npm run dev:debug # Development server with Ink debug logging
npm run dev:yaml # Start the YAML engine server npm run dev:inspect # Development server with Node inspector on 0.0.0.0:9231
npm run dev:zcode # Start the Z-code engine server npm run start:debug # Compiled server with Ink debug logging
npm run start:ink # Build and run the compiled Ink engine server npm run start:inspect # Compiled server with Node inspector on 0.0.0.0:9231
npm run build # Compile TypeScript
npm run test # Run Jest tests
npm run lint # Run ESLint on src/
npm run start:cli # Run the CLI interface
npm run dev:cli # Run the CLI interface through ts-node/nodemon
``` ```
Each game engine also has `:debug` and `:inspect` variants. `:debug` enables engine-specific diagnostic logging. `:inspect` starts Node with the inspector and currently also enables that engine's debug flag, so it is the combined debug-plus-inspector mode. Set `PORT` to choose the server port. The Docker image defaults to `3000`.
## Docker / Coolify Ink Deployment ## Coolify 4 Deployment
The included `Dockerfile` builds and serves the Ink engine only. Coolify can use the repository Dockerfile directly. Configure Coolify to deploy this branch with the repository `Dockerfile`.
Set the Coolify environment variables from `coolify.env.example`; at minimum: Recommended environment:
```text ```text
NODE_ENV=production NODE_ENV=production
DEFAULT_GAME_ENGINE=ink
PORT=3000 PORT=3000
INK_CONFIG_FILE=./config/engines/ink.json INK_CONFIG_FILE=./config/engines/ink.json
``` ```
The container compiles TypeScript during image build and compiles the configured Ink source to JSON when the server starts. Coolify can watch `release/coolify-ink` and redeploy on webhook pushes. The intended flow is:
## Configuration 1. Write Ink locally in `data/ink-src/`.
2. Test locally with `npm run dev`.
3. Commit to the development branch.
4. Merge or cherry-pick the wanted deployment state into `release/coolify-ink`.
5. Push `release/coolify-ink` to the Git remote watched by Coolify.
Environment variables are loaded from `.env`. The container builds TypeScript during image build and compiles the configured Ink source at server startup.
- `PORT`: preferred web server port. ## Ink Configuration
- `DEFAULT_GAME_ENGINE`: engine used by `npm run dev` and `npm run start`; one of `ink`, `yaml`, or `zcode`.
- `DEFAULT_WORLD_FILE`: YAML world file to load. Defaults to `./data/worlds/example_world.yml`.
- `OPENROUTER_API_KEY`: API key for LLM command interpretation.
- `OPENROUTER_MODEL`: OpenRouter model name.
TTS provider settings are configured in the browser options menu and persisted in browser storage. Providers currently include `none`, browser speech synthesis, Kokoro, ElevenLabs, and OpenAI. Production should not assume a universal TTS default; the game or player state selects the active mode, and `none` is the safe fallback. The active game is configured in `config/engines/ink.json`.
## Starting A Game Important paths:
The web client no longer starts the game automatically. Browsers require a user gesture before audio playback, so the right page initially shows a start prompt and the command input is hidden. Use `new game` or `load` in the top bar to start. - `paths.inkSource`: main Ink source file.
- `paths.inkCompiled`: compiled Ink JSON target.
- `paths.mainGameFile`: compiled Ink JSON loaded by the server.
- `paths.music`: background music directory.
- `paths.sfx`: sound effect directory.
- `paths.images`: image directory.
The placeholder server API supports: Game metadata and language are sent to the client before game start. The client uses game language for hyphenation and TTS language hints; UI locale can still be overridden by the player.
- `newGame()` ## Browser Client
- `loadGame(slot)`
- `saveGame(slot)`
- `hasSaveGame(slot)`
- `getSaveGames()`
- `isGameRunning()`
Save slots are positive integers. Save behavior is engine-specific: the Ink client/server path persists Ink state, client history, choices, media state, and playback position for browser save/load; YAML and Z-code persistence still need regression testing and cleanup. The client lives in `public/` and is served as native browser modules. It renders structured `TurnResult` output from the server, including paragraphs, headings, choices, media events, alerts, score messages, achievements, and errors.
## Web Client TTS provider settings, volume controls, savegames, TTS cache, and rendered story history are stored in browser storage. Ink server state is also sent back to the browser save data so a client can recover after reload or server restart without server-side per-player sessions.
The browser app is built from native ES modules in `public/js/`. The loader dynamically imports modules, applies a cache-busting query string during development, resolves declared dependencies, and awaits module initialization in dependency order before the UI becomes usable. ## Story Tags
Major modules: Ink tags are parsed server-side into structured output objects. The client consumes structured turn data only.
- `module-registry.js`, `base-module.js`, `loader.js`: module lifecycle, dependency graph, progress overlay, state reporting. Common tags:
- `text-processor-module.js`, `paragraph-layout-module.js`, `layout-renderer-module.js`: SmartyPants, language-aware hyphenation, Knuth-Plass line breaking, DOM rendering.
- `markup-parser-module.js`: story markup fallback for chapters, sections, Markdown emphasis, right-page glossary notes, images, SFX, and music.
- `sentence-queue-module.js`, `playback-coordinator-module.js`, `animation-queue-module.js`: sentence preparation, synchronized playback, timing, fast-forward.
- `tts-factory-module.js` plus provider modules: TTS provider selection, voice settings, speed mapping, caching, and playback.
- `audio-manager-module.js`: master, speech, music, and sound effect volume, music playback, sound effects, and music ducking.
- `ui-controller-module.js`, `ui-display-handler-module.js`, `ui-input-handler-module.js`, `options-ui-module.js`: book UI, command input, options, top-bar controls, and game API calls.
- `choice-display-module.js`: choice-mode UI, click selection, keyboard-letter assignment, and future choice-template routing.
The static server sends no-cache headers for local development so stale ES modules do not mask changes. If the browser console shows `onpage-dialog.preload.js:121 Uncaught ReferenceError: browser is not defined`, ignore it; that comes from the installed ad blocker, not this project.
## Story Markup
Plain paragraphs are rendered paragraph by paragraph. Normal following paragraphs are horizontally indented and do not get a blank line between them. Special block markers change the treatment of the next paragraph.
Inline Markdown emphasis:
```text
*italic* or _italic_
**bold** or __bold__
***bold italic*** or ___bold italic___
```
Right-page glossary notes:
```text
The train stops at Eibenreith.
#gloss[Eibenreith](A fictional alpine town in the Kaiserpunk setting.)
```
Glossary markup is a normal story tag scoped to the paragraph/block it is attached to. The UI finds every matching visible instance of the term in that right-page block and adds a hover/focus note. The tag itself is not displayed, is not sent to TTS, and is ignored by choices and command history. Avoid raw Ink control characters in the explanation; `|`, `{`, and `}` must be escaped in Ink as `\|`, `\{`, and `\}` if they are needed literally.
Canonical block/media/control tags use Ink-style `#` syntax. In Ink these are real Ink tags. In YAML and Z-code narrative output, leading `#...` lines are parsed by the server into the same structured `StoryTag` objects before reaching the client. The browser only consumes structured `TurnResult` objects.
Tag format:
```text
#key
#key[value]
#key[value](options)
#key:value
```
For Ink choices, put choice-local tags under the choice they belong to. Explicit keyboard letters are supported with `# letter[x]`, `#letter[x]`, or the colon form `#key:x`; the client reserves those keys first, then assigns the remaining visible choices from `1` through `0`, then `A` through `Z` in order. `#optional` renders the choice in italic. `# action[name]` or `#action:name` is parsed as a category/template hint for future choice layouts, although the current UI displays all choices in one list.
Chapter:
```text
#chapter[The Mysterious Mansion]
The first paragraph uses a drop cap and no first-line indent.
Following paragraphs use the normal paragraph indent.
```
The heading is centered, italic, and uses the same text face as the body. The first paragraph after a chapter marker is unindented and receives the drop cap treatment.
Section or text block:
```text ```text
#chapter[Title]
#section #section
#image[file.png](landscape|portrait|square, pause=2)
The first paragraph starts a separated block without horizontal indent. #music[file.mp3](crossfade|queue|cut, loop=true, lead=5)
#sfx[file.ogg](duration=4, fade=true)
The following paragraph returns to the normal indent. #gloss[Term](Explanation shown on hover.)
#score[Optional score text]
#achievement[Optional achievement text]
#alert[Optional player hint]
#error[Optional error text]
``` ```
`#textblock` is treated the same way. The first paragraph after the marker is separated from previous content by one line of vertical space. Choice-local tags:
Images are story blocks:
```text ```text
#image[mansion-rain.jpg](landscape) #key:x
#image[portrait-letter.jpg](portrait pause=2) #optional
#image[seal.png](square lead=1.5) #action[name]
``` ```
Image file names are relative to `public/images/`. `landscape`/`widescreen` and `square` images are centered, near full page width, and line-snapped. `portrait` images sit beside prose at half page width. Image pauses (`pause=`, `delay=`, `lead=`, or a bare `2s`) are skippable and do not block background TTS preparation. Explicit choice keys are reserved first. Remaining choices receive keys from `1` through `0`, then `a` through `z`.
Sound effects are story tags:
```text
#sfx[squeaky-door.ogg]
#sfx[church-bells.ogg](max=8 fade fade-duration=2)
The door opens and the hall exhales.
```
The tag is parsed by the server into a `StoryTag` object. Sound effect paths are relative to `public/sounds/`. Optional parameters can limit playback (`max=`, `duration=`, `stop-after=`, `fade-after=`), choose the end mode (`fade` or `stop`/`cut`), and set `fade-duration=`.
Music can be placed as a block:
```text
#music[rain-theme.ogg](crossfade, loop, lead=4)
```
Music paths are relative to `public/music/`. Supported modes are `queue`, `crossfade`, and `cut`. Use `loop` or `once` to control repetition. `lead=<seconds>` delays the following text/TTS paragraph so the music can play alone before narration continues. To place that pause between a chapter heading and the dropcapped first paragraph, put the music tag after the chapter tag and before the first prose paragraph; TTS generation for the next spoken paragraph continues during the lead pause.
Game-state and player-message tags:
```text
#score[You found the quiet ending.]
#error[Ink story ended without an explicit ending tag.]
#achievement[First Steps]
#alert[Try examining objects before using them.]
```
`#score[...]` marks an intended ending and opens a localized ending popup when the turn reaches `inputMode: end`. `#error[...]` marks an unrecoverable ending and opens an error popup. If an Ink story runs out of content without an explicit `#score[...]` or `#error[...]`, the Ink engine emits an `#error[...]` tag. `#achievement[...]` and `#alert[...]` open localized queued popups while the game continues.
## Architecture Documentation
`SPECIFICATION.md` is the canonical architecture and implementation specification. `TODO.md` is the canonical progress and remaining-work list. The former loose Ink and Z-code inclusion notes have been folded into those two files.
## Assets
- `public/sounds/`: sound effects referenced by `#sfx[file]` tags.
- `public/music/`: background music referenced by `#music[file](...)` tags.
- `public/images/`: story images referenced by `#image[file](...)`.
- `public/fonts/`: font assets used by the book UI.
Keep third-party assets licensed for local redistribution, and document source and license in the folder README or alongside the file.
## Typography And Playback Behavior
The renderer is designed to behave like a scaled static book page. The page keeps its aspect ratio, and text sizes and word positions scale relative to the page instead of reflowing unpredictably at small browser sizes.
Text processing order:
1. Parse story markup and remove non-display media markers.
2. Apply Markdown emphasis spans and right-page glossary annotations.
3. Run SmartyPants for typographic punctuation.
4. Apply Hyphenopoly for the selected language.
5. Calculate line breaks with the Knuth-Plass algorithm.
6. Render absolutely positioned word spans and animate them in sync with audio or estimated duration.
When real TTS audio is available, animation duration is driven by measured audio length. With TTS disabled or unavailable, duration is estimated from text length and the persisted speed setting.
Fast-forwarding by page click or space completes the active animation and fades/stops current TTS playback so queued content can proceed.
The right page history is line-addressed rather than natively scrolled. The page has a fixed line count, all block heights snap to whole lines, and the custom scrollbar represents virtual history line position. The DOM keeps a moving window of history blocks around the active line instead of paginating the story.
## Changelog
### 2026-05-17
- Added Ink engine support with source compilation, engine config files, game metadata, locale-driven UI text, choice mode, keyboard choice letters, and one-list choice rendering.
- Added line-addressed right-page history, save/load reconstruction, image restoration, custom scrollbar plumbing, and virtual block-window rendering.
- Added story image rendering for landscape, portrait, and square images, including line-snapped sizing and portrait text exclusion.
- Added localized popups for endings, errors, achievements, and alerts through the tag channel.
- Added credits and third-party license UI.
- Added per-volume mute toggles and configurable music ducking amount.
- Added German typography handling for dialogue guillemets based on game metadata language.
### 2026-05-14
- Consolidated usage, markup, and architecture documentation into `README.md` and `TODO.md`.
- Added no-cache static serving and module URL cache busting so browser reloads pick up JS changes reliably during development.
- Fixed module loader dependency ordering so modules are initialized only after their declared dependencies are ready.
- Added the placeholder game API for `newGame`, `loadGame`, `saveGame`, `hasSaveGame`, `getSaveGames`, and `isGameRunning`.
- Changed the web UI to require a manual game start before showing the command input, which keeps browser audio autoplay restrictions manageable.
- Implemented story markup for chapters, text blocks, Markdown emphasis, image placeholders, sound effects, and music cues.
- Added music block parameters for playback mode, loop/once behavior, and lead-in delay.
- Added sound and music asset folders and playback plumbing for sound effects and background music.
- Added music ducking while TTS is active.
- Reworked book typography around Knuth-Plass line breaking, Hyphenopoly hyphenation, SmartyPants, paragraph indentation rules, drop caps, and responsive page scaling.
- Reworked TTS provider behavior, speed mapping, persistence, caching keys, top-bar/options synchronization, and OpenAI voice validation.
- Added development notes for ignoring the unrelated ad-blocker console error.
### Earlier Prototype Work
- Established the original animated fiction prototype with inkjs, SmartyPants, Hyphenopoly, Knuth-Plass line breaking, custom animation scheduling, save/load concepts, and media tags.
- Split the client from a monolithic prototype into focused modules for text processing, layout, animation, audio, persistence, TTS, and UI control.
-248
View File
@@ -1,248 +0,0 @@
# AI Interactive Fiction Specification
This is the single architecture and behavior specification for the project. Usage and changelog live in `README.md`; actionable work items live in `TODO.md`; authoring conventions live in `MARKUP_GUIDELINES.md`.
## Product Goal
AI Interactive Fiction is a shared book-style web client plus interchangeable game engine servers. The client renders interactive fiction as animated, carefully typeset illustrated prose with optional speech, music, sound effects, images, choices, and command input. Game engines own game state and emit a shared structured protocol.
The production client must tolerate speech being unavailable. The safe TTS provider default is `none`; a game or player preference may select another provider.
## Repository Layout
- `public/`: shared browser UI, assets, fonts, client modules, third-party browser libraries.
- `src/`: TypeScript servers, shared protocol types, engine implementations, YAML world model, CLI support.
- `config/engines/`: per-engine configuration files.
- `data/ink-src/`: Ink source files.
- `data/ink/`: compiled Ink JSON output.
- `data/worlds/`: YAML world files.
- `data/z-code/`: Z-machine story files such as `zork1.bin`.
- `data/zcode-prompts/`: prompt templates used by the current LLM-mediated Z-code narrator.
- `scripts/`: project utility scripts. Currently used: `check-node-version.js` and `run-engine.js`.
- `templates/`: not present in the current repository and not used.
## Engine Selection And Commands
`DEFAULT_GAME_ENGINE` in `.env` selects the engine used by:
```text
npm run dev
npm run start
```
Supported values are `ink`, `yaml`, and `zcode`.
Engine-specific commands bypass the default:
```text
npm run dev:ink
npm run dev:yaml
npm run dev:zcode
npm run start:ink
npm run start:yaml
npm run start:zcode
```
`dev:*` runs TypeScript through `ts-node` and `nodemon`. `start:*` runs compiled JavaScript from `dist/` and builds first through `prestart:*`. `*:debug` enables the engine's debug environment flag. `*:inspect` starts Node inspector and currently also enables debug for that engine.
The CLI path is YAML-only and uses `src/index.ts --cli`. It is useful for testing the YAML `GameRunner` without the browser UI. The old `test-server-yaml.ts` is a legacy static/YAML harness and should be removed once no workflow depends on it.
## Shared Server Protocol
All engines communicate with the browser through Socket.IO and the same game API:
```text
newGame()
loadGame(slot)
saveGame(slot)
hasSaveGame(slot)
getSaveGames()
isGameRunning()
chooseChoice(index)
```
The Ink engine additionally supports browser-owned session recovery:
```text
resumeGame(savedInkState)
exportGameState()
```
`exportGameState()` returns the current Ink state without creating a server-side save slot. The client stores that state with story history, choices, input mode, and media state in IndexedDB. `resumeGame(savedInkState)` rehydrates a fresh server-side InkEngine after a socket reconnect or browser reload without emitting duplicate narrative. This keeps durable player-specific state client-side for hosted multi-client Ink deployments.
Line-input engines also use `playerCommand` for free text.
Every engine emits `TurnResult` objects:
```ts
interface TurnResult {
turnId: number;
paragraphs: Array<{ text: string; tags?: StoryTag[] }>;
choices: ChoiceResult[];
inputMode: 'text' | 'choice' | 'end' | 'none';
globalTags?: StoryTag[];
gameState?: {
score?: number;
endState?: { type: 'intended' | 'error'; message?: string };
};
suggestions?: string[];
}
```
The browser consumes structured `TurnResult` data only. YAML and Z-code servers must parse or synthesize the same tag objects that Ink exposes through native tags.
## Game Engines
### YAML Engine
- Config: `config/engines/yaml.json`
- Server: `src/server-yaml.ts`
- World model: `data/worlds/*.yml`
- CLI entry: `src/index.ts --cli`
The YAML engine is no longer the architectural default; it is one engine beside Ink and Z-code. It uses `GameRunner`, `GameEngine`, and `YamlWorldParser`, emits `inputMode: 'text'`, and remains the best test bed for deterministic world-model plus LLM command interpretation.
### Ink Engine
- Config: `config/engines/ink.json`
- Server: `src/server-ink.ts`
- Engine: `src/engine/ink-engine.ts`
- Source: `data/ink-src/eibenreith.ink` plus included chapter files.
- Compiled output: `data/ink/eibenreith.ink.json`
The Ink server compiles source at startup using `inkjs/full`, then runs the compiled story with `inkjs`. Ink choices become `ChoiceResult` objects. Ink tags become shared `StoryTag` objects. Choice preview tags support `#key`, `#letter`, `#optional`, `#action`, `#gated`, and `#sort`.
The server keeps only ephemeral per-socket InkEngine instances. Browser IndexedDB owns durable Ink saves and the current autosave. If the socket reconnects or the page reloads, the browser sends the autosaved Ink state to `resumeGame()` and restores rendered history locally.
Ink does not provide arbitrary string input as a native async primitive comparable to choices. Future text-input turns should be implemented through a tag such as `#input[name](prompt)`: the server returns `inputMode: 'text'`, the UI shows command input for one round, then the server stores the submitted string into an Ink variable and continues.
### Z-code Engine
- Config: `config/engines/zcode.json`
- Server: `src/server-zcode.ts`
- Engine: `src/engine/zcode-llm-engine.ts`
- Story file: `data/z-code/zork1.bin` by default.
- Prompt templates: `data/zcode-prompts/*.yml`
The engine name is Z-code. Zork I is only the current game file and prompt target. The current implementation runs a Z-machine story through `ifvms`, keeps Z-machine state authoritative, and uses an LLM to translate natural-language input into parser commands and rewrite raw Z-machine output into prose.
Future work should separate Z-code-generic logic from Zork-specific prompt content more clearly.
## Client Module System
The browser client uses native ES modules, no bundler. The loader imports modules, analyzes dependency declarations, initializes modules in dependency order, tracks state/progress, and hides the loading overlay only when initialization and progress exit animations are complete.
Rules:
- Every app module extends `BaseModule`.
- Every app module registers with `moduleRegistry`.
- Required dependencies must be listed in `dependencies`.
- Modules should use authoritative dependencies instead of local fallbacks.
- Do not add fallback paths to hide bad dependency declarations or ordering bugs.
- `setTimeout` must not paper over initialization races. It is acceptable for animation, debounce, throttle, and browser rendering timing when locally justified.
Core modules:
- `loader.js`: module script loading, progress UI, dependency diagnostics.
- `module-registry.js`: registration and readiness promises.
- `base-module.js`: lifecycle, progress, state, event cleanup.
Primary client responsibilities:
- Text and typography: `text-processor`, `paragraph-layout`, `layout-renderer`.
- Markup: `markup-parser`.
- Queue/playback: `text-buffer`, `sentence-queue`, `playback-coordinator`, `animation-queue`.
- Audio/TTS: `audio-manager`, `tts-factory`, provider modules.
- UI: `ui-controller`, `ui-display-handler`, `ui-input-handler`, `choice-display`, `options-ui`, `ui-effects`.
- Persistence/history: `persistence-manager`, `story-history`.
- Networking: `socket-client`.
Known cleanup candidates: `debug-utils-module.js` is not loaded; `game-loop-module.js` still contains high-level glue from older architecture and should be audited before removal.
## Text Pipeline
Processing order:
1. Receive structured blocks and tags from a game engine.
2. Parse inline story markup and remove media markers from display/TTS text.
3. Apply Markdown emphasis.
4. Apply locale-aware SmartyPants typography.
5. Apply Hyphenopoly for the game metadata language.
6. Measure text using the exact page font settings.
7. Run Knuth-Plass line breaking.
8. Render absolutely positioned words into the page line-coordinate model.
9. Animate words in sync with measured TTS duration or estimated duration.
The external Knuth-Plass library should not be locally modified. Adaptation belongs in our modules.
## Right Page Layout And History
The right page is a virtual line-addressed content pane:
- `#page_right` does not use native scrolling.
- Page height is divided into `PAGE_LINE_COUNT = 25`.
- All block heights, margins, image spacing, and chapter/section spacing are exact line multiples.
- Stored block positions are line coordinates, not pixels.
- Window resize recalculates pixels from line coordinates.
- New content appends at the live bottom.
- Manual scrolling moves the active line and keeps a window of nearby blocks loaded.
- The custom scrollbar represents virtual line history, not DOM scroll state.
Portrait images may overlap line ranges with text next to them, but edges must still land on line boundaries.
## Markup And Tags
Canonical tag syntax:
```text
#key
#key[value]
#key[value](options)
#key:value
```
Supported story tags include:
- `#chapter[Title]`
- `#section` / `#textblock`
- `#image[file](landscape|portrait|square pause=2)`
- `#sfx[file](max=8 fade fade-duration=2)`
- `#music[file](crossfade loop lead=4)`
- `#gloss[term](definition)`
- `#score[...]`
- `#error[...]`
- `#achievement[...]`
- `#alert[...]`
Choice tags:
- `#key:x` or `#key[x]`
- `#letter[x]`
- `#optional`
- `#action[name]`
The active choice UI is one list. Explicit keys are reserved first, then remaining choices receive `1` through `0`, then `A` through `Z`.
Markdown emphasis:
```text
*italic* or _italic_
**bold** or __bold__
***bold italic*** or ___bold italic___
```
## Audio, TTS, And Media
TTS providers currently include `none`, Browser Speech, Kokoro, ElevenLabs, and OpenAI. Provider modules exist, but Browser Speech and Kokoro need focused validation before being considered production-ready.
TTS cache keys include provider, voice, provider speed value, language, and exact normalized TTS string. Fast-forward must accelerate visible animation and fade/stop active TTS without cancelling background generations unless the foreground block has been waiting long enough.
Music and sound effects are preloaded when requested. Music can queue, crossfade, cut, loop, play once, and lead into following text. Music ducks by a persisted percentage during TTS playback.
## Documentation Source Of Truth
- `README.md`: usage, commands, changelog, concise feature summary.
- `SPECIFICATION.md`: architecture and behavior.
- `TODO.md`: active status and backlog.
- `MARKUP_GUIDELINES.md`: writing/authoring rules for story files.
- `THIRD_PARTY_NOTICES.md` and `public/THIRD_PARTY_NOTICES.md`: license/credits material.
-47
View File
@@ -1,47 +0,0 @@
# Third-Party Library Audit
Date: 2026-05-17
## Summary
The project currently uses the expected browser-side typography/story libraries plus additional runtime packages:
- inkjs
- SmartyPants.js
- Hyphenopoly
- Knuth-Plass line breaking support (`knuth-and-plass.js`, `linebreak.js`, `linked-list.js`)
- Kokoro JS browser bundle
- Server/runtime npm packages: Express, Socket.IO, OpenAI SDK, Axios, cors, dotenv, js-yaml, ifvms
- EB Garamond font files
## Browser-vendored files
| Component | Files | Upstream/latest check | Local status |
| --- | --- | --- | --- |
| SmartyPants.js | `public/js/smartypants.js` | Local header says `smartypants.js 0.0.6`; npm `smartypants` latest is `0.2.2`. The old `smartypants.js` package name is unpublished from npm. | Not byte-identical to npm `smartypants` 0.0.5, 0.0.9, or 0.2.2. Treat as modified/older vendor code. |
| Hyphenopoly browser files | `public/js/Hyphenopoly.js`, `public/js/Hyphenopoly_Loader.js`, `public/js/hyphenopoly.module.js`, `public/js/patterns/*.wasm` | Browser header says `5.2.0-beta.1`; npm dependency is `6.0.0`; npm latest is `6.1.0`. | `Hyphenopoly.js` is effectively 5.2.0-beta.1 after line-ending normalization. `Hyphenopoly_Loader.js` has a small local/prototype difference in `H.hide`. Browser copy is older than package/latest. |
| Knuth-Plass adapter | `public/js/knuth-and-plass.js` | No authoritative upstream identified from headers or npm metadata. | Modified from the prototype copy and currently application-owned adapter code. |
| Line breaking support | `public/js/linebreak.js`, `public/js/linked-list.js` | No authoritative upstream identified from headers. Not the npm `linebreak` package 1.1.0. | Identical to prototype copies. `linked-list.js` still has a suspicious `get last() { return this.last; }` accessor inherited from the prototype. |
| Kokoro JS browser bundle | `public/js/kokoro-js.js` | npm `kokoro-js` latest is `1.2.1`; installed is `1.2.0`. | Byte-identical to `kokoro-js@1.2.0/dist/kokoro.web.js`; not latest. |
## Direct runtime npm packages
| Package | Installed | Latest checked | License | Status |
| --- | --- | --- | --- | --- |
| `inkjs` | 2.4.0 | 2.4.0 | MIT | Current. |
| `hyphenopoly` | 6.0.0 | 6.1.0 | MIT | Not latest. Browser vendored files are older than this dependency. |
| `kokoro-js` | 1.2.0 | 1.2.1 | Apache-2.0 | Not latest. |
| `ifvms` | 1.1.6 | 1.1.6 | MIT | Current. |
| `openai` | 4.91.0 | 6.38.0 | Apache-2.0 | Not latest major. |
| `socket.io` | 4.8.1 | 4.8.3 | MIT | Not latest patch. |
| `express` | 5.1.0 | 5.2.1 | MIT | Not latest patch. |
| `axios` | 1.8.4 | 1.16.1 | MIT | Not latest. |
| `cors` | 2.8.5 | 2.8.6 | MIT | Not latest patch. |
| `dotenv` | 16.4.7 | 17.4.2 | BSD-2-Clause | Not latest major. |
| `js-yaml` | 4.1.0 | 4.1.1 | MIT | Not latest patch. |
## Notices
The UI-readable license and credit notice is `public/THIRD_PARTY_NOTICES.md`.
The root `THIRD_PARTY_NOTICES.md` points to that served file so the repository has an obvious project-level notice entry.
-129
View File
@@ -1,129 +0,0 @@
# TODO And Progress
This is the active implementation checklist. Architecture lives in `SPECIFICATION.md`; usage lives in `README.md`; authoring conventions live in `MARKUP_GUIDELINES.md`.
## Current Status
- The shared client is feature-rich enough for Ink gameplay: line-based book layout, animated text, TTS, music, sound effects, images, choices, glossary notes, save/load restoration, and localized UI are implemented.
- The Ink engine is the current primary development engine.
- The YAML engine and Z-code engine need regression testing after the Ink-heavy client changes.
- Browser TTS and Kokoro provider modules exist but are not yet proven reliable.
- The codebase still contains logging noise and older architecture fragments that need cleanup.
## Shared Client
### Completed
- [x] Native ES module loader, dependency graph, progress overlay, and ordered initialization.
- [x] Responsive book layout that scales page, font sizes, and word positions relative to page size.
- [x] SmartyPants, German guillemet normalization, Hyphenopoly, and Knuth-Plass layout.
- [x] Paragraph/chapter/section/drop-cap rules.
- [x] Markdown emphasis with `*` and `_` syntax.
- [x] Right-page `#gloss[term](definition)` hover/focus notes.
- [x] Image rendering for landscape, square, and portrait cases, with history/save restoration.
- [x] Sound effect and music playback, including music lead-in, loop/once, and ducking.
- [x] TTS `none`, OpenAI, ElevenLabs, Browser Speech, and Kokoro provider modules.
- [x] TTS cache keys include provider, voice, speed, language, and exact normalized string.
- [x] Persisted speech enable state, provider, voice, speed, language, and volume preferences.
- [x] Fast-forward for text animation and active TTS fade/stop.
- [x] Choice UI, explicit keys, automatic key assignment, optional-choice styling, click and keyboard selection.
- [x] Localized popups for endings, errors, achievements, and alerts.
- [x] Credits/license dialog.
- [x] Line-addressed history scrolling model.
- [x] Choice-return turns continue to the choice point when autoplay is off.
### In Progress
- [ ] Polish custom scrollbar dragging so the thumb moves freely during drag and commits the scroll target only on release.
- [ ] Tighten automated checks around top-bar/options state initialization after reload.
- [ ] Improve automated visual regression coverage for page scaling, drop caps, image wrapping, and paragraph indentation.
- [ ] Improve automated audio tests for music ducking, sound effect timing, and fast-forward fadeout.
- [ ] Validate provider-specific speed conversion for all TTS providers against real API behavior.
### Pending
- [ ] Add a logging module with levels/categories to reduce console output and improve runtime performance.
- [ ] Show startup warnings/instructions when TTS APIs still need to be selected or configured.
- [ ] Put production-ready default option values into code/config.
- [ ] Get Browser TTS working reliably.
- [ ] Get Kokoro.js TTS working for English-language games.
- [ ] Get Kokoro.js TTS working for German-language games.
- [ ] Add a TTS module for self-hosted or local OpenAI-compatible servers.
- [ ] Test every documented `#tag` parameter and effect against parser, server, client rendering, playback, and save/load behavior.
- [ ] Remove local file paths and diff-comments from third-party license markdown, refresh included third-party licenses/material, update external libraries where possible, and move any local modifications into our code.
- [ ] Improve credits page layout with more window height, a larger notices markdown pane, and a Hollywood-style title scroll for creative credits.
- [ ] Clean up unused modules, obsolete functions, legacy comments, and vestigial fragments from older architectures.
- [ ] Add optical margin alignment/punctuation protrusion as typography polish if current hanging punctuation proves insufficient.
## Shared Server Architecture
### Completed
- [x] Shared `TurnResult` protocol used by all engines.
- [x] Shared game API shape: `newGame`, `loadGame`, `saveGame`, `hasSaveGame`, `getSaveGames`, `isGameRunning`.
- [x] Per-engine config files with metadata, locale, main game file, and asset paths.
- [x] `.env` default engine selection for `npm run dev` and `npm run start`.
- [x] Engine-specific dev/start/debug/inspect scripts.
- [x] YAML server renamed to `server-yaml.ts` so it is no longer implied as the generic server.
- [x] Z-code server/config/scripts use `zcode` naming; Zork is only the current story/prompt target.
### Pending
- [ ] Extract duplicated Express/Socket.IO/static-file/port-fallback setup into a shared server base.
- [ ] Replace session-local placeholder saves with durable server-side or browser-coordinated saves where appropriate.
- [ ] Clean up start scripts and add a Dockerfile for hosting the selected engine on Coolify.
- [ ] Decide whether `src/index.ts` should remain as the YAML CLI entry or be replaced by clearer `cli-yaml.ts` and engine-specific launchers.
- [ ] Remove `test-server-yaml.ts` if no current workflow depends on it.
- [ ] Add logger configuration to scripts: `LOG_LEVEL`, `LOG_CATEGORIES`, and engine debug defaults.
## Ink Engine
### Completed
- [x] Ink source compilation through `inkjs/full`.
- [x] Split Ink source files with a master include file.
- [x] Ink metadata handoff to client.
- [x] Ink choices converted to `ChoiceResult`.
- [x] Ink tags converted to shared `StoryTag`.
- [x] Choice preview tags for `#key`, `#letter`, `#optional`, and `#action`.
- [x] Save/load of Ink state plus client history state.
- [x] `#score`, `#error`, `#achievement`, and `#alert` tag behavior.
- [x] `#gloss[term](definition)` support on right-page text.
### Pending
- [ ] Add text-input turns to Ink games, switching the UI to command input for one round and returning to choices afterward.
- [ ] Add a full dynamic description of the created character to the score panel after the game intro.
- [ ] Continue authoring and testing Eibenreith content.
- [ ] Test all documented tag syntax inside real Ink source, including edge cases with includes and choice-local tags.
## YAML Engine
### Completed
- [x] Deterministic YAML world model and `GameRunner`.
- [x] YAML CLI path for testing without browser UI.
- [x] YAML web server emits `TurnResult` objects.
### Pending
- [ ] Test/debug the YAML engine after Ink-driven client changes.
- [ ] Continue development of the YAML engine.
- [ ] Replace command mirroring with the full LLM/world-model command loop when typography/audio testing no longer needs mirroring.
- [ ] Validate YAML-generated `#` tags through the shared parser/protocol path.
## Z-code Engine
### Completed
- [x] Z-code naming for engine scripts/config/server.
- [x] Current Zork I narrator implementation using `ifvms` plus OpenRouter prompt templates.
- [x] Z-code engine emits shared `TurnResult` objects.
### Pending
- [ ] Test/debug the Z-code engine after Ink-driven client changes.
- [ ] Finish the Z-code version: optimize prompt templates, choose the best LLM for the task, and test project memory behavior.
- [ ] Separate Z-code-generic logic from Zork-specific prompt assumptions.
- [ ] Validate save/restore of Z-machine state.
- [ ] Merge this branch with `master` after YAML and Z-code regression testing.
-7
View File
@@ -1,7 +0,0 @@
{
"folders": [
{
"path": "."
}
]
}
-18
View File
@@ -1,18 +0,0 @@
{
"engine": "yaml",
"locale": "en_US",
"paths": {
"mainGameFile": "data/worlds/example_world.yml",
"music": "public/music",
"sfx": "public/sounds",
"images": "public/images"
},
"metadata": {
"title": "The Mysterious Mansion",
"author": "AI Interactive Fiction",
"subtitle": "An open-world text adventure",
"version": "1.0.0",
"language": "en_US",
"copyright": "Prototype content for local development."
}
}
-19
View File
@@ -1,19 +0,0 @@
{
"engine": "zcode",
"locale": "en_US",
"paths": {
"mainGameFile": "data/z-code/zork1.bin",
"promptDir": "data/zcode-prompts",
"music": "public/music",
"sfx": "public/sounds",
"images": "public/images"
},
"metadata": {
"title": "Zork I",
"author": "Infocom",
"subtitle": "A narrated Z-code adventure",
"version": "1.0.0",
"language": "en_US",
"copyright": "Use only with a legally supplied Z-code story file."
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-706
View File
@@ -1,706 +0,0 @@
title: The Mysterious Mansion
author: AI Interactive Fiction
version: 1.0.0
introduction: |
#chapter[The Mysterious Mansion]
#music[Dark Jodler.mp3](lead=10)
The last thing you remember is the letter: heavy paper, black wax, your name written in a hand you almost recognized.
It asked you to come after dusk, alone, and promised that the house would answer what the sender could not.
Now you stand beyond the wrought iron gate, with rain cooling your face and the hill rising before you.
At its crest waits the old Victorian mansion, every dark window turned toward the path as if the building has been expecting you.
The gate gives under your hand with no protest, though its ironwork is wet enough to shine black.
Gravel shifts beneath your boots as you pass between the pillars, and the garden closes behind you with the soft finality of a curtain.
Halfway up the path, you stop and listen.
The rain has thinned to a whisper, but the house answers with other sounds: timber settling, gutters ticking, and something deep inside the walls that might be machinery or breath.
For a heartbeat you think the mansion is about to speak ... but only the wind moves through the ivy.
It drags the leaves across the brickwork in slow strokes, as if wiping dust from an old name.
# Room definitions
rooms:
# Starting area
front_yard:
name: Front Yard
description: |
You follow the gravel path up the hill.
The rain softens to a drizzle, and moonlight peeks through gaps in the clouds.
Ancient oak trees frame the property, their branches swaying in the gentle breeze.
At the top of three worn stone steps, the mansion's front door waits under a sagging porch roof.
The porch boards are swollen with rain, each one bending under your weight before it remembers its shape.
A brass knocker hangs at eye level, polished bright at the edges where countless hands have touched it and left no warmth behind.
The letter in your pocket presses against your ribs.
You remember the last line now: come before the clocks learn your name.
Somewhere above you, behind a blind upper window, a pale shape passes from left to right and is gone.
You tell yourself it was a reflection, then look back at the path and find no light behind you bright enough to make one.
The house waits.
#sfx[squeaky-door.ogg]
When you reach for the handle, it turns before your fingers touch it, and the door opens with a long, complaining squeak.
exits:
- direction: north
targetRoomId: entrance_hall
description: large wooden doors lead into the mansion
- direction: south
targetRoomId: street
description: wrought iron gates lead back to the street
objects:
- strange_letter
- garden_statue
characters: []
# Main entrance
entrance_hall:
name: Entrance Hall
description: |
Grand chandeliers hang from the high ceiling, their crystals covered in cobwebs.
A wide staircase curves up to the second floor, and paintings of stern-faced
individuals watch you from ornate frames on the walls.
The floor is polished marble, though dusty from neglect.
exits:
- direction: south
targetRoomId: front_yard
description: the main entrance doors
- direction: north
targetRoomId: grand_staircase
description: the grand staircase
- direction: east
targetRoomId: dining_room
description: an archway leads to what appears to be a dining room
- direction: west
targetRoomId: library
description: a door marked 'Library'
objects:
- dusty_key
- umbrella_stand
characters:
- butler_ghost
# Library
library:
name: Library
description: |
Bookshelves line every wall, reaching from floor to ceiling.
A reading desk sits in the center of the room, a leather-bound book
open upon it. A gentle fire crackles in the fireplace, casting
dancing shadows across the room.
exits:
- direction: east
targetRoomId: entrance_hall
description: the door back to the entrance hall
- direction: north
targetRoomId: secret_study
description: a hidden door in the bookshelf
isLocked: true
keyId: old_brass_key
objects:
- leather_book
- reading_glasses
- old_brass_key
characters: []
# Dining Room
dining_room:
name: Dining Room
description: |
A long table dominates this room, set for a dinner party that never happened.
Fine china and silverware rest atop an elegant tablecloth, now gray with dust.
A chandelier hangs above, and a sideboard against the wall holds various serving dishes.
exits:
- direction: west
targetRoomId: entrance_hall
description: the archway back to the entrance hall
- direction: north
targetRoomId: kitchen
description: a swinging door to what must be the kitchen
objects:
- silver_candlestick
- dusty_plate
characters:
- dining_ghost
# Kitchen
kitchen:
name: Kitchen
description: |
This once-busy kitchen now stands silent. Copper pots and pans hang from hooks,
and an old cast-iron stove sits cold against the wall. A large preparation table
occupies the center of the room, and a pantry door stands ajar.
exits:
- direction: south
targetRoomId: dining_room
description: the swinging door back to the dining room
- direction: east
targetRoomId: pantry
description: the pantry door
objects:
- rusty_knife
- cookbook
characters: []
# Pantry
pantry:
name: Pantry
description: |
Shelves line the walls of this small room, holding preserves in dusty jars
and sacks of long-expired ingredients. A small window provides minimal light,
and a musty smell permeates the air.
exits:
- direction: west
targetRoomId: kitchen
description: the door back to the kitchen
objects:
- dusty_jar
- strange_bottle
characters: []
# Grand Staircase
grand_staircase:
name: Grand Staircase
description: |
The staircase curves gracefully upward, its wooden railings polished to a soft glow
despite the overall neglect of the mansion. Family portraits line the walls,
following your movement with their painted eyes.
exits:
- direction: south
targetRoomId: entrance_hall
description: back down to the entrance hall
- direction: north
targetRoomId: upper_landing
description: up to the second floor
objects:
- family_portrait
characters: []
# Upper Landing
upper_landing:
name: Upper Landing
description: |
The upper landing connects several rooms on the second floor. A faded
carpet runs down the center of the hallway, and doors line both sides.
A large window at the end of the hall shows the rainy night outside.
exits:
- direction: south
targetRoomId: grand_staircase
description: down the grand staircase
- direction: east
targetRoomId: master_bedroom
description: a door marked 'Master Bedroom'
- direction: west
targetRoomId: study
description: a door marked 'Study'
objects: []
characters: []
# Master Bedroom
master_bedroom:
name: Master Bedroom
description: |
A large four-poster bed dominates this room, its once-luxurious hangings
now faded and torn. A vanity sits in the corner, its mirror clouded with age,
and a wardrobe stands against the far wall.
exits:
- direction: west
targetRoomId: upper_landing
description: the door back to the upper landing
objects:
- jewelry_box
- old_diary
characters:
- lady_ghost
# Study
study:
name: Study
description: |
This cozy room contains a large desk covered in papers, a comfortable
armchair, and a globe that seems to rotate slowly on its own. Bookshelves
line the walls, filled with volumes on various esoteric subjects.
exits:
- direction: east
targetRoomId: upper_landing
description: the door back to the upper landing
objects:
- strange_device
- important_letter
characters: []
# Secret Study (hidden room)
secret_study:
name: Secret Study
description: |
Hidden behind the library bookshelf, this small room appears to be a
private study. A desk with a locked drawer sits against one wall, and
shelves hold unusual artifacts and rare books. A single candle provides
dim illumination.
exits:
- direction: south
targetRoomId: library
description: the hidden door back to the library
objects:
- ancient_tome
- crystal_key
characters: []
# Street (exit area)
street:
name: Street
description: |
The quiet street outside the mansion is shrouded in fog. Streetlamps cast
pools of yellow light that barely penetrate the mist. The mansion's gates
loom behind you, while the way back to town lies ahead.
exits:
- direction: north
targetRoomId: front_yard
description: the mansion gates
objects: []
characters: []
# Object definitions
objects:
strange_letter:
name: Strange Letter
description: |
A weathered envelope containing an invitation to visit the mansion.
The handwriting is elegant but unfamiliar, and the letter is signed
simply with the initial "M".
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
garden_statue:
name: Garden Statue
description: |
A weathered stone statue of a weeping angel. Its face is covered by its hands,
and detailed wings spread out from its back. Something about it makes you uneasy.
traits:
- fixed
states: {}
allowedActions:
- examine
dusty_key:
name: Dusty Key
description: |
An old iron key, covered in dust. It looks like it might fit an old door somewhere.
traits:
- takeable
- key
states: {}
allowedActions:
- take
- examine
- use
umbrella_stand:
name: Umbrella Stand
description: |
A brass stand holding several antique umbrellas, all in various states of decay.
traits:
- fixed
- container
states:
open: true
containedObjects: []
allowedActions:
- examine
leather_book:
name: Leather Book
description: |
A thick tome bound in dark leather. The pages are filled with strange symbols
and diagrams that seem to shift slightly when you're not looking directly at them.
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
reading_glasses:
name: Reading Glasses
description: |
A pair of wire-rimmed spectacles. The lenses have a slight blue tint to them.
traits:
- takeable
- wearable
states:
worn: false
allowedActions:
- take
- wear
- examine
old_brass_key:
name: Brass Key
description: |
A small brass key with intricate engravings. It seems to be quite old but well-maintained.
traits:
- takeable
- key
states: {}
allowedActions:
- take
- examine
- use
silver_candlestick:
name: Silver Candlestick
description: |
A tarnished silver candlestick with an unlit candle. It feels heavy in your hand.
traits:
- takeable
- light_source
states:
lit: false
allowedActions:
- take
- light
- examine
dusty_plate:
name: Dusty Plate
description: |
A fine china plate covered in a layer of dust. Despite its age, the painted pattern is still vivid.
traits:
- takeable
states: {}
allowedActions:
- take
- examine
rusty_knife:
name: Rusty Knife
description: |
An old kitchen knife with a rusted blade. It's dull, but still might be useful.
traits:
- takeable
- sharp
states: {}
allowedActions:
- take
- examine
- use
cookbook:
name: Cookbook
description: |
A yellowed cookbook filled with strange recipes. Some ingredients are unusual, and
the instructions sometimes reference phases of the moon or specific star alignments.
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
dusty_jar:
name: Dusty Jar
description: |
A glass jar containing what might once have been fruit preserves, now unidentifiable.
Best not to open it.
traits:
- takeable
- container
states:
open: false
allowedActions:
- take
- examine
strange_bottle:
name: Strange Bottle
description: |
A small bottle containing a glowing blue liquid. The label is written in a language you don't recognize.
traits:
- takeable
- drinkable
states: {}
allowedActions:
- take
- drink
- examine
family_portrait:
name: Family Portrait
description: |
A large painting of a stern-looking family - a husband, wife, and three children.
The father's eyes seem to follow you, and there's something oddly familiar about his face.
traits:
- fixed
states: {}
allowedActions:
- examine
jewelry_box:
name: Jewelry Box
description: |
An ornate wooden box inlaid with mother-of-pearl. Inside are several pieces of
antique jewelry, including a ruby necklace that catches the light strangely.
traits:
- takeable
- container
states:
open: true
containedObjects:
- ruby_necklace
allowedActions:
- take
- open
- close
- examine
ruby_necklace:
name: Ruby Necklace
description: |
A delicate gold chain with a large ruby pendant. The gem seems to glow with an inner light,
and it feels warm to the touch.
traits:
- takeable
- wearable
states:
worn: false
allowedActions:
- take
- wear
- examine
old_diary:
name: Old Diary
description: |
A leather-bound diary with yellowed pages. The entries detail the daily life of
the mansion's former mistress, and hint at a growing fear of something in the house.
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
strange_device:
name: Strange Device
description: |
A brass contraption with gears, dials, and a glass dome. It's purpose isn't clear,
but it occasionally ticks and whirs on its own.
traits:
- takeable
states:
active: false
allowedActions:
- take
- use
- examine
important_letter:
name: Important Letter
description: |
A sealed envelope addressed to "The Heir." The wax seal bears the same crest
that you've seen throughout the mansion.
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
ancient_tome:
name: Ancient Tome
description: |
A massive book bound in what appears to be human skin. The title, "Liber Umbrarum,"
is embossed in gold on the spine. The pages contain rituals and incantations.
traits:
- takeable
- readable
states: {}
allowedActions:
- take
- read
- examine
crystal_key:
name: Crystal Key
description: |
A key made of clear crystal that catches the light in mesmerizing ways. Despite
its appearance, it feels as solid as metal and cool to the touch.
traits:
- takeable
- key
states: {}
allowedActions:
- take
- use
- examine
# Character definitions
characters:
butler_ghost:
name: Ghostly Butler
description: |
The translucent figure of an elderly butler, dressed in formal attire from a bygone era.
He stands with perfect posture, hands clasped behind his back.
dialogue:
greeting: "Welcome to the mansion, sir/madam. We've been expecting you."
mansion: "This estate has belonged to the Montgomery family for generations. Such a shame what happened to them."
family: "The Montgomerys? All gone now, I'm afraid. The master, his wife, and their children. A tragedy."
tragedy: "I'm not at liberty to discuss the details, but the answers you seek may be found in the study."
yourself: "Me? I've served this house for longer than I care to remember. Even death couldn't release me from my duties."
defaultResponse: "I'm afraid I cannot help you with that particular inquiry."
inventory: []
mood: formal
dining_ghost:
name: Dining Guest
description: |
A spectral figure in elegant dinner attire, seated at the table. She appears to be
a young woman, and she plays absently with a spectral fork.
dialogue:
greeting: "Oh, a new guest! How delightful. Will you join us for dinner? It's been so long since we had fresh company."
dinner: "We've been waiting for the main course for... goodness, how long has it been now? Years, I suppose."
herself: "My name? It's... it's strange, I can't quite recall. I remember coming here for a dinner party, but then..."
party: "It was supposed to be a celebration. The master of the house had made some sort of discovery. Something important."
discovery: "In the secret study, I believe. Behind the library. The master was very excited about it."
defaultResponse: "I'm sorry, my mind isn't what it used to be. The years blur together when you're like this."
inventory: []
mood: wistful
lady_ghost:
name: Ghostly Lady
description: |
The elegant apparition of a woman in Victorian dress, her face partly obscured by a veil.
She sits at the vanity, brushing her long hair with a ghostly brush.
dialogue:
greeting: "A visitor? How unusual. Are you lost, or are you here for a purpose?"
purpose: "Everyone who comes to this house has a purpose, whether they know it or not."
herself: "I was the lady of this house once. Now I am bound to it, as are we all."
family: "My husband was obsessed with his research. My children... I tried to protect them. I failed."
research: "The barriers between worlds, the nature of reality itself. He found something, in the end. Something that should have remained hidden."
hidden: "In his secret study. The key is... well, I suppose you'll have to find that yourself. Some secrets reveal themselves only to those who seek them."
defaultResponse: "There are some things I cannot speak of. The house has its rules, even for the dead."
inventory: []
mood: melancholy
# Action definitions
actions:
look:
patterns:
- "look around"
- "look at [object]"
- "examine [object]"
- "check [object]"
- "inspect [object]"
- "observe [object]"
- "view [object]"
handler: "look"
go:
patterns:
- "go [direction]"
- "move [direction]"
- "walk [direction]"
- "head [direction]"
- "travel [direction]"
- "enter [direction]"
requiresObject: true
handler: "go"
take:
patterns:
- "take [object]"
- "get [object]"
- "pick up [object]"
- "grab [object]"
- "collect [object]"
requiresObject: true
handler: "take"
drop:
patterns:
- "drop [object]"
- "put down [object]"
- "discard [object]"
- "leave [object]"
requiresObject: true
handler: "drop"
inventory:
patterns:
- "inventory"
- "check inventory"
- "show inventory"
- "what am I carrying"
- "what do I have"
handler: "inventory"
use:
patterns:
- "use [object]"
- "use [object] on [target]"
- "use [object] with [target]"
- "apply [object] to [target]"
requiresObject: true
requiresTarget: false
handler: "use"
talk:
patterns:
- "talk to [object]"
- "speak to [object]"
- "ask [object] about [topic]"
- "tell [object] about [topic]"
- "converse with [object]"
requiresObject: true
handler: "talk"
read:
patterns:
- "read [object]"
- "read from [object]"
- "examine [object]"
- "look at [object]"
requiresObject: true
handler: "look"
help:
patterns:
- "help"
- "commands"
- "what can I do"
- "show help"
handler: "help"
wear:
patterns:
- "wear [object]"
- "put on [object]"
- "don [object]"
requiresObject: true
handler: "use"
# Initial game state
initialState:
currentRoomId: front_yard
inventory:
- strange_letter
visitedRooms: []
flags:
hasMetButler: false
hasFoundSecret: false
counters:
moveCount: 0
-30
View File
@@ -1,30 +0,0 @@
# Z-Code Story Files
Place your Z-machine story files here. The Z-code narrator engine looks for
`zork1.bin` by default. This can be overridden with the `ZCODE_STORY_FILE`
environment variable.
## Obtaining Zork I
The Zork I story file (`zork1.bin`, also distributed as `ZORK1.DAT` or as a
`.z3` or `.z5` file) is copyrighted by Infocom / Activision. It is not
included in this repository.
You can obtain a legal copy via:
- The **Zork Trilogy** on GOG.com or Steam (includes the original data files).
- The [Internet Archive](https://archive.org/details/Zork_I_The_Great_Underground_Empire_1980_Infocom)
hosts a playable version in-browser; the original data files are part of some
archived distributions listed under the Infocom catalogue.
Once you have the file, rename it to `zork1.bin` and place it in this folder,
or set `ZCODE_STORY_FILE=./path/to/your/file` in your `.env`.
## Supported Formats
The `ifvms` interpreter accepts:
- `.z3`, `.z4`, `.z5`, `.z8` - raw Z-machine story files
- `.zblorb` - Blorb-wrapped story files (may include sound resources)
- Any file with the correct Z-machine header (the extension is ignored)
Zork I is a Z-machine version 3 (`.z3`) game.
Binary file not shown.
@@ -1,44 +0,0 @@
# Character Generation Prompt
# Called once at game start to create a unique player character.
# No user_template is needed — the system message IS the full prompt.
# Expected output: 300-500 words of vivid character description prose. No JSON.
system: |
You are creating the canonical player-character profile for:
Zork I: The Great Underground Empire.
Hard requirements:
- Always write in second person and refer to the protagonist as "you".
- Never call the protagonist "he", "she", "they", or by a third-person noun.
- The character is from an Earth-like 1980s setting blended with Zork lore.
- The character is NOT an American treasure hunter.
- Tone: vivid, concrete, grounded, literary, and emotionally specific.
- Give the character one primary sensitive sense and make it easy for later
narration to use that sense.
Generate a complete persona that includes:
- Random full name.
- Gender, nationality, race, age.
- Skin color, eye color, hair color, body size, body build.
- Personal style, hairstyle.
- Tattoos (optional), piercings (optional), scars (optional).
- Distinctive standout trait (at least one clearly unusual detail).
- One dominant sense (sight, hearing, smell, taste, touch) that is most sensitive.
- Exactly three sentences of backstory.
- Personality, likes, dislikes, hopes, fears, worldview.
- Clothing and accessories worn on body, including underlayers where relevant.
- Do NOT list bags, tools, or equipment.
- Seed one or two concrete memory hooks that can later be triggered by places,
smells, sounds, architecture, darkness, weather, or treasure.
Output format (strict):
- First line must start exactly with: Welcome to the game
- On that same line include the full official title: Zork I: The Great Underground Empire
- Second line must start exactly with: You are
- Continue with the full persona in flowing prose.
- Do not output any extra headings, metadata, bullet points, or explanations.
Ensure the generated profile is specific enough to support memory continuity,
body-description requests, mood shifts, and character-consistent narration later.
user_template: ""
-112
View File
@@ -1,112 +0,0 @@
# Command Translator Prompt
# Called for every player input. Converts free natural-language text into a
# Z-machine parser command, or decides to reply directly / execute session tools.
# Expected output: a JSON object (see schema below).
system: |
You are the command-intent router for a literary Zork I engine.
Hard rules:
- Keep player-character continuity in second person ("you").
- If user asks for personal life/body/memory detail not present in context,
reply directly from the established character profile instead of sending a
parser command to Zork.
- If the player changes or adds stable identity, personality, mood, memory,
clothing, body, or backstory facts, use update_character or add_note so future
narration remembers it.
- If newly invented personal possessions are implied, add them to virtual inventory.
Choose one response mode:
MODE A — command
Use for one parser action.
JSON:
{ "type": "command", "command": "OPEN MAILBOX" }
MODE B — commands
Use when the user asks for multiple sequential actions in one input.
Example: "Take and read the pamphlet" -> TAKE PAMPHLET, READ PAMPHLET.
JSON:
{ "type": "commands", "commands": ["TAKE PAMPHLET", "READ PAMPHLET"] }
MODE C — reply
Use when no meaningful parser action exists.
Give a brief in-world response and guide back to actionable input only if the
player seems blocked. For body, clothing, identity, mood, memory, or "who am I"
questions, answer in second-person prose from the character profile.
JSON:
{ "type": "reply", "text": "..." }
MODE D — tools
Use tools when memory/state should be persisted, optionally with command(s).
JSON shape:
{
"type": "tools",
"tools": [ ... ],
"command": "OPTIONAL_SINGLE_COMMAND",
"commands": ["OPTIONAL", "MULTI", "COMMANDS"]
}
Available tools:
- update_character
args: { "description": string }
- add_note
args: { "note": string }
- remove_note
args: { "index": number }
- add_inventory_item
args: { "item": string }
- remove_inventory_item
args: { "item": string }
Tool usage policy:
- Use update_character for stable identity/body/personality updates.
- Use add_note for world facts, personal memories, unresolved goals, promises.
- Use add_inventory_item when narration introduces an on-person personal item
(even if Z-machine parser does not track it).
- Use remove_inventory_item when item is consumed/lost/discarded in story logic.
Command policy:
- Use terse Zork-style imperatives, uppercase preferred.
- Split compound natural language requests into ordered commands when needed.
- Avoid impossible commands when a helpful reply is better.
- Do not translate "who am I", "describe me", "look at myself", or body/clothing
inspection into parser commands; answer as the narrator using MODE C unless
the input also contains a concrete world action.
- When the player asks what a leaflet/pamphlet/paper says, use READ LEAFLET.
- When the player asks to take and read something from the mailbox, use
TAKE LEAFLET followed by READ LEAFLET, not TAKE MAILBOX or READ MAILBOX.
- When the player asks to look inside the mailbox, use LOOK IN MAILBOX.
- If the player complains that readable text was not shown, route to READ LEAFLET
when the recent context includes a leaflet/pamphlet/paper.
Output only valid JSON in exactly one mode.
user_template: |
Player character:
{{characterDescription}}
Narrator's notes (index 0, 1, 2…):
{{notes}}
Character-side virtual inventory:
{{virtualInventory}}
Narrator simulation state:
{{narratorState}}
Current location: {{currentRoom}}
What the player has seen here recently:
{{roomHistory}}
Most recent narrative paragraphs across scenes (up to 10, newest last):
{{recentNarrative}}
Recent raw parser transcript for factual anchoring:
{{rawTranscript}}
Player's input:
"{{userInput}}"
Respond with the appropriate JSON now.
-76
View File
@@ -1,76 +0,0 @@
# Output Evaluator Prompt
# Called after each Z-machine response. Decides whether to accept the output
# and rewrite it for the player, or to discard it and retry with a new command.
# Expected output: a JSON object (see schema below).
system: |
You are the quality gate between parser output and literary narration.
Decide whether to accept parser output or retry with a better command.
Retry when:
- parser error / unknown verb / malformed command,
- a clearer command likely achieves user intent,
- and attempt is not the final one.
Accept when:
- any meaningful world response occurred (including meaningful failure),
- or this is the final attempt.
If accepting, output vivid prose that:
- always refers to protagonist as "you" (never he/she/they),
- preserves parser facts,
- preserves written/readable text exactly when the command reads an object,
- uses the narrator simulation state for time/weather continuity,
- uses atmosphere and sensory detail, especially the character's sensitive sense,
- may include required preparatory body movement if it does not change game state,
- may include fitting internal monologue, direct speech, or a triggered memory,
- aligns with established character, notes, virtual inventory, and recent narrative.
Keep output concrete and scene-rooted.
Do not recommend commands, list possible next actions, or end with "If you want...".
Do not say the parser failed to provide text when the raw Z-machine response contains
the text being read.
Output JSON only:
- Accept:
{ "decision": "accept", "text": "..." }
- Retry:
{ "decision": "retry", "command": "..." }
user_template: |
Player character:
{{characterDescription}}
Narrator's notes:
{{notes}}
Character-side virtual inventory:
{{virtualInventory}}
Narrator simulation state:
{{narratorState}}
Current location: {{currentRoom}}
What the player has seen here recently:
{{roomHistory}}
Most recent narrative paragraphs across scenes (up to 10, newest last):
{{recentNarrative}}
Recent raw parser transcript for factual anchoring:
{{rawTranscript}}
---
Original player intent: "{{userIntent}}"
Command tried: {{commandTried}}
Attempt: {{attempt}} of {{maxAttempts}}
Raw Z-machine response:
---
{{zcodeOutput}}
---
Decide now: accept and rewrite, or retry with a new command?
Respond with the appropriate JSON.
-77
View File
@@ -1,77 +0,0 @@
# Text Rewriter Prompt
# Called for the game's opening text, and for re-entry into rooms that have
# no prior player-facing history yet.
# Expected output: polished prose. No JSON.
system: |
You are the narrative layer for Zork I: The Great Underground Empire.
Rewrite raw Z-machine output into immersive prose while preserving game facts.
Core stance:
- Always narrate the player-character in second person: "you".
- Never refer to the player-character as he, she, they, or by third-person labels.
- Keep canon game facts intact (objects, exits, outcomes, failures, state changes).
- Do not invent gameplay-critical facts that contradict Z-machine output.
Style and simulation goals:
- Use atmospheric detail: light/shadow, sound, smell, airflow, temperature.
- Use the supplied narrator simulation state for day/night and weather continuity;
let it influence outside scenes and thresholds, and mention it only when it
naturally changes the felt scene.
- Make physical actions visceral when movement/exertion occurs.
- Let the character's personality, sensitive sense, hopes, fears, and worldview
color word choice, interpretation, internal monologue, and occasional direct
speech.
- Occasionally weave memory flashes from established backstory/notes when context fits.
- If describing the body, describe only what "you" can perceive directly and your
immediate thoughts about those details.
- Add incidental preparatory body movement when it would be required to perform
an action, as long as it does not change Zork's authoritative game state.
- Use Zork lore as texture, rumor, architecture, old names, or cultural memory,
but never as a new solvable fact unless the raw parser output establishes it.
Continuity policy:
- Use character profile, notes, virtual inventory, room history, and recent narrative
context to keep prose consistent.
- If prior context introduced non-Zork personal possessions, they can appear in prose
as personal details but must not be treated as parser-available game objects unless
present in Z-machine output.
Output constraints:
- Return prose only. No JSON, no labels, no headings.
- Prefer short paragraphs (2-5 sentences each).
- Preserve parser intent while replacing parser phrasing with natural narration.
- Do not recommend commands, list possible actions, or end with "If you want...".
- Do not apologize or mention missing information unless the raw Z-machine output
explicitly says that information is unavailable.
- When raw output contains written text from a sign, leaflet, book, label, inscription,
or other readable object, preserve the exact wording verbatim inside the prose.
user_template: |
The player character:
{{characterDescription}}
Narrator's notes about the story so far:
{{notes}}
Character-side virtual inventory (can exist even if Zork does not track it):
{{virtualInventory}}
Narrator simulation state:
{{narratorState}}
What the player has seen in this location before (most recent last):
{{roomHistory}}
Most recent narrative paragraphs across scenes (up to 10, newest last):
{{recentNarrative}}
Recent raw parser transcript for factual anchoring:
{{rawTranscript}}
Raw Z-machine output to rewrite:
---
{{zcodeOutput}}
---
Rewrite the above as prose for the player now.
-64
View File
@@ -1,64 +0,0 @@
/**
* Command-line interface for running the interactive fiction game
*/
export declare class GameRunner {
private engine;
private llmProvider;
private rl;
private gameContext;
private gameHistory;
private suggestedCommands;
constructor();
/**
* Initialize the game
*/
initialize(worldPath: string): Promise<void>;
/**
* Start the game in CLI mode
*/
start(): Promise<void>;
/**
* The main game loop for CLI mode
*/
private gameLoop;
/**
* Process a player command and return the narrative response
* Used by both CLI and web interfaces
*/
processCommand(input: string): Promise<string>;
/**
* End the game
*/
end(): void;
/**
* Update the game context with new narrative
*/
private updateGameContext;
/**
* Get the current game state
* Used by web interface
*/
getGameState(): {
world: import("../interfaces/world-model").WorldModel;
currentRoomId: string;
inventory: string[];
visitedRooms: string[];
flags: Record<string, boolean>;
counters: Record<string, number>;
};
/**
* Get the current room description
* Used by web interface
*/
getCurrentRoomDescription(): string;
/**
* Get suggested actions for the current game state
* Used by web interface
*/
getSuggestions(): string[];
/**
* Load a saved game state
* Used by web interface
*/
loadGameState(savedState: any): void;
}
-262
View File
@@ -1,262 +0,0 @@
"use strict";
/**
* Command-line interface for running the interactive fiction game
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameRunner = void 0;
const readline = __importStar(require("readline"));
const path = __importStar(require("path"));
const dotenv = __importStar(require("dotenv"));
const game_engine_1 = require("../engine/game-engine");
const openrouter_provider_1 = require("../llm/openrouter-provider");
// Load environment variables
dotenv.config();
class GameRunner {
constructor() {
this.rl = null;
this.gameContext = '';
this.gameHistory = [];
this.suggestedCommands = [];
this.engine = new game_engine_1.TextAdventureEngine();
this.llmProvider = new openrouter_provider_1.OpenRouterProvider();
}
/**
* Initialize the game
*/
async initialize(worldPath) {
console.log('Initializing game...');
// Initialize LLM provider
const apiKey = process.env.OPENROUTER_API_KEY;
const model = process.env.OPENROUTER_MODEL;
if (!apiKey || !model) {
throw new Error('Missing required environment variables: OPENROUTER_API_KEY and/or OPENROUTER_MODEL');
}
await this.llmProvider.initialize({
apiKey,
model,
temperature: 0.7,
maxTokens: 800
});
// Load the world
const resolvedPath = path.resolve(worldPath);
console.log(`Loading world from ${resolvedPath}...`);
await this.engine.loadWorld(resolvedPath);
console.log('Game initialized successfully!');
}
/**
* Start the game in CLI mode
*/
async start() {
// Create readline interface for CLI mode
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
try {
// Display introduction
const introText = await this.engine.start();
console.log('\n' + introText + '\n');
// Look at initial room
const initialLook = this.engine.processAction({ action: 'look', confidence: 1 });
// Generate narrative description
const narrativeRequest = {
action: 'look',
result: initialLook.message,
roomDescription: this.engine.getCurrentRoomDescription(),
visibleObjects: this.engine.getVisibleObjects(),
visibleCharacters: this.engine.getVisibleCharacters(),
tone: 'descriptive'
};
const narrative = await this.llmProvider.generateNarrative(narrativeRequest);
console.log('\n' + narrative.text + '\n');
// Store suggestions if available
if (narrative.suggestions && narrative.suggestions.length > 0) {
this.suggestedCommands = narrative.suggestions;
}
// Update game context
this.updateGameContext(narrative.text);
// Start the game loop
this.gameLoop();
}
catch (error) {
console.error('Error starting game:', error);
this.end();
}
}
/**
* The main game loop for CLI mode
*/
gameLoop() {
if (!this.rl)
return;
this.rl.question('> ', async (input) => {
if (input.toLowerCase() === 'quit' || input.toLowerCase() === 'exit') {
this.end();
return;
}
const response = await this.processCommand(input);
console.log('\n' + response + '\n');
// Continue the game loop
this.gameLoop();
});
}
/**
* Process a player command and return the narrative response
* Used by both CLI and web interfaces
*/
async processCommand(input) {
try {
// Process player input
const actionRequest = {
playerInput: input,
currentRoom: this.engine.getWorldModel().rooms[this.engine.getCurrentState().currentRoomId].name,
visibleObjects: this.engine.getVisibleObjects().map(id => this.engine.getWorldModel().objects[id].name),
visibleCharacters: this.engine.getVisibleCharacters().map(id => this.engine.getWorldModel().characters[id].name),
possibleActions: this.engine.getAvailableActions(),
inventory: this.engine.getCurrentState().inventory.map(id => this.engine.getWorldModel().objects[id].name),
gameContext: this.gameContext
};
if (this.rl) {
console.log('Thinking...');
}
// Translate player input to action
const action = await this.llmProvider.translateAction(actionRequest);
// Process the action in the game engine
const actionResult = this.engine.processAction(action);
// If state changed, update it
if (actionResult.stateChanged && actionResult.newState) {
this.engine.getCurrentState().currentRoomId = actionResult.newState.currentRoomId;
this.engine.getCurrentState().inventory = actionResult.newState.inventory;
this.engine.getCurrentState().visitedRooms = actionResult.newState.visitedRooms;
this.engine.getCurrentState().flags = actionResult.newState.flags;
this.engine.getCurrentState().counters = actionResult.newState.counters;
}
// Generate narrative description
const narrativeRequest = {
action: `${action.action}${action.object ? ' ' + action.object : ''}${action.target ? ' on ' + action.target : ''}`,
result: actionResult.message,
roomDescription: this.engine.getCurrentRoomDescription(),
visibleObjects: this.engine.getVisibleObjects().map(id => this.engine.getWorldModel().objects[id].name),
visibleCharacters: this.engine.getVisibleCharacters().map(id => this.engine.getWorldModel().characters[id].name),
previousContext: this.gameHistory.slice(-3).join('\n'),
tone: 'descriptive'
};
const narrative = await this.llmProvider.generateNarrative(narrativeRequest);
// Store suggestions if available
if (narrative.suggestions && narrative.suggestions.length > 0) {
this.suggestedCommands = narrative.suggestions;
}
// Update game context with the new narrative
this.updateGameContext(narrative.text);
// Return the narrative text
return narrative.text;
}
catch (error) {
console.error('Error processing input:', error);
return 'Something went wrong. Please try again.';
}
}
/**
* End the game
*/
end() {
console.log('\nThanks for playing!');
if (this.rl) {
this.rl.close();
this.rl = null;
}
this.engine.end();
if (process.env.NODE_ENV !== 'production') {
process.exit(0);
}
}
/**
* Update the game context with new narrative
*/
updateGameContext(narrative) {
// Add to history
this.gameHistory.push(narrative);
// Keep history limited to last 10 entries
if (this.gameHistory.length > 10) {
this.gameHistory.shift();
}
// Update current context (last 5 entries)
this.gameContext = this.gameHistory.slice(-5).join('\n');
}
/**
* Get the current game state
* Used by web interface
*/
getGameState() {
return {
world: this.engine.getWorldModel(),
currentRoomId: this.engine.getCurrentState().currentRoomId,
inventory: this.engine.getCurrentState().inventory,
visitedRooms: this.engine.getCurrentState().visitedRooms,
flags: this.engine.getCurrentState().flags,
counters: this.engine.getCurrentState().counters
};
}
/**
* Get the current room description
* Used by web interface
*/
getCurrentRoomDescription() {
const roomId = this.engine.getCurrentState().currentRoomId;
return this.engine.getWorldModel().rooms[roomId].description;
}
/**
* Get suggested actions for the current game state
* Used by web interface
*/
getSuggestions() {
return this.suggestedCommands;
}
/**
* Load a saved game state
* Used by web interface
*/
loadGameState(savedState) {
// Set the current state to match the saved state
this.engine.getCurrentState().currentRoomId = savedState.currentRoomId;
this.engine.getCurrentState().inventory = savedState.inventory;
this.engine.getCurrentState().visitedRooms = savedState.visitedRooms;
this.engine.getCurrentState().flags = savedState.flags;
this.engine.getCurrentState().counters = savedState.counters;
}
}
exports.GameRunner = GameRunner;
//# sourceMappingURL=game-runner.js.map
-1
View File
File diff suppressed because one or more lines are too long
-39
View File
@@ -1,39 +0,0 @@
export type EngineName = 'yaml' | 'ink' | 'zcode' | string;
export interface GameMetadata {
title: string;
author?: string;
subtitle?: string;
version?: string;
copyright?: string;
language?: string;
}
export interface GamePaths {
mainGameFile: string;
inkSource?: string;
inkCompiled?: string;
promptDir?: string;
music?: string;
sfx?: string;
images?: string;
[key: string]: string | undefined;
}
export interface GameEngineConfig {
engine: EngineName;
locale: 'en_US' | 'de_DE' | string;
paths: GamePaths;
metadata: GameMetadata;
}
export declare function projectPath(relativeOrAbsolutePath: string): string;
export declare function loadGameConfig(configPath: string, engine: EngineName): GameEngineConfig;
export declare function ensureConfiguredAssetDirectories(config: GameEngineConfig): void;
export declare function clientGameConfig(config: GameEngineConfig): {
engine: string;
locale: string;
metadata: GameMetadata;
assets: {
music: string;
sfx: string;
sounds: string;
images: string;
};
};
-96
View File
@@ -1,96 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.projectPath = projectPath;
exports.loadGameConfig = loadGameConfig;
exports.ensureConfiguredAssetDirectories = ensureConfiguredAssetDirectories;
exports.clientGameConfig = clientGameConfig;
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const PROJECT_ROOT = path_1.default.resolve(__dirname, '../..');
function fallbackConfig(engine) {
return {
engine,
locale: 'en_US',
paths: {
mainGameFile: engine === 'ink'
? 'data/ink/story.ink.json'
: engine === 'zcode'
? 'data/z-code/zork1.bin'
: 'data/worlds/example_world.yml',
music: 'public/music',
sfx: 'public/sounds',
images: 'public/images',
},
metadata: {
title: 'AI Interactive Fiction',
author: 'Generative AI',
subtitle: 'An open-world text adventure',
version: '1.0.0',
copyright: '',
language: 'en_US',
},
};
}
function projectPath(relativeOrAbsolutePath) {
return path_1.default.isAbsolute(relativeOrAbsolutePath)
? relativeOrAbsolutePath
: path_1.default.resolve(PROJECT_ROOT, relativeOrAbsolutePath);
}
function loadGameConfig(configPath, engine) {
const absolutePath = projectPath(configPath);
if (!(0, fs_1.existsSync)(absolutePath)) {
console.warn(`[config] Missing ${absolutePath}; using ${engine} defaults.`);
return fallbackConfig(engine);
}
const parsed = JSON.parse((0, fs_1.readFileSync)(absolutePath, 'utf8'));
const fallback = fallbackConfig(engine);
return {
engine: parsed.engine ?? fallback.engine,
locale: parsed.locale ?? fallback.locale,
paths: {
...fallback.paths,
...(parsed.paths ?? {}),
},
metadata: {
...fallback.metadata,
...(parsed.metadata ?? {}),
language: parsed.metadata?.language ?? parsed.locale ?? fallback.metadata.language,
},
};
}
function ensureConfiguredAssetDirectories(config) {
const directories = [
config.paths.music,
config.paths.sfx,
config.paths.images,
config.paths.inkSource ? path_1.default.dirname(config.paths.inkSource) : undefined,
config.paths.inkCompiled ? path_1.default.dirname(config.paths.inkCompiled) : undefined,
config.paths.mainGameFile ? path_1.default.dirname(config.paths.mainGameFile) : undefined,
config.paths.promptDir,
];
for (const directory of directories) {
if (!directory)
continue;
const absolutePath = projectPath(directory);
if (!(0, fs_1.existsSync)(absolutePath)) {
(0, fs_1.mkdirSync)(absolutePath, { recursive: true });
}
}
}
function clientGameConfig(config) {
return {
engine: config.engine,
locale: config.locale,
metadata: config.metadata,
assets: {
music: '/music/',
sfx: '/sounds/',
sounds: '/sounds/',
images: '/images/',
},
};
}
//# sourceMappingURL=game-config.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"game-config.js","sourceRoot":"","sources":["../../src/config/game-config.ts"],"names":[],"mappings":";;;;;AA4DA,kCAIC;AAED,wCAsBC;AAED,4EAkBC;AAED,4CAYC;AA1HD,gDAAwB;AACxB,2BAAyD;AA+BzD,MAAM,YAAY,GAAG,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;AAEtD,SAAS,cAAc,CAAC,MAAkB;IACxC,OAAO;QACL,MAAM;QACN,MAAM,EAAE,OAAO;QACf,KAAK,EAAE;YACL,YAAY,EACV,MAAM,KAAK,KAAK;gBACd,CAAC,CAAC,yBAAyB;gBAC3B,CAAC,CAAC,MAAM,KAAK,OAAO;oBAClB,CAAC,CAAC,uBAAuB;oBACzB,CAAC,CAAC,+BAA+B;YACvC,KAAK,EAAE,cAAc;YACrB,GAAG,EAAE,eAAe;YACpB,MAAM,EAAE,eAAe;SACxB;QACD,QAAQ,EAAE;YACR,KAAK,EAAE,wBAAwB;YAC/B,MAAM,EAAE,eAAe;YACvB,QAAQ,EAAE,8BAA8B;YACxC,OAAO,EAAE,OAAO;YAChB,SAAS,EAAE,EAAE;YACb,QAAQ,EAAE,OAAO;SAClB;KACF,CAAC;AACJ,CAAC;AAED,SAAgB,WAAW,CAAC,sBAA8B;IACxD,OAAO,cAAI,CAAC,UAAU,CAAC,sBAAsB,CAAC;QAC5C,CAAC,CAAC,sBAAsB;QACxB,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,YAAY,EAAE,sBAAsB,CAAC,CAAC;AACzD,CAAC;AAED,SAAgB,cAAc,CAAC,UAAkB,EAAE,MAAkB;IACnE,MAAM,YAAY,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;IAC7C,IAAI,CAAC,IAAA,eAAU,EAAC,YAAY,CAAC,EAAE,CAAC;QAC9B,OAAO,CAAC,IAAI,CAAC,oBAAoB,YAAY,WAAW,MAAM,YAAY,CAAC,CAAC;QAC5E,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAA,iBAAY,EAAC,YAAY,EAAE,MAAM,CAAC,CAA8B,CAAC;IAC3F,MAAM,QAAQ,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,OAAO;QACL,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM;QACxC,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM;QACxC,KAAK,EAAE;YACL,GAAG,QAAQ,CAAC,KAAK;YACjB,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC;SACxB;QACD,QAAQ,EAAE;YACR,GAAG,QAAQ,CAAC,QAAQ;YACpB,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC;YAC1B,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,QAAQ,IAAI,MAAM,CAAC,MAAM,IAAI,QAAQ,CAAC,QAAQ,CAAC,QAAQ;SACnF;KACF,CAAC;AACJ,CAAC;AAED,SAAgB,gCAAgC,CAAC,MAAwB;IACvE,MAAM,WAAW,GAAG;QAClB,MAAM,CAAC,KAAK,CAAC,KAAK;QAClB,MAAM,CAAC,KAAK,CAAC,GAAG;QAChB,MAAM,CAAC,KAAK,CAAC,MAAM;QACnB,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS;QACzE,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS;QAC7E,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS;QAC/E,MAAM,CAAC,KAAK,CAAC,SAAS;KACvB,CAAC;IAEF,KAAK,MAAM,SAAS,IAAI,WAAW,EAAE,CAAC;QACpC,IAAI,CAAC,SAAS;YAAE,SAAS;QACzB,MAAM,YAAY,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAA,eAAU,EAAC,YAAY,CAAC,EAAE,CAAC;YAC9B,IAAA,cAAS,EAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAgB,gBAAgB,CAAC,MAAwB;IACvD,OAAO;QACL,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,MAAM,EAAE;YACN,KAAK,EAAE,SAAS;YAChB,GAAG,EAAE,UAAU;YACf,MAAM,EAAE,UAAU;YAClB,MAAM,EAAE,UAAU;SACnB;KACF,CAAC;AACJ,CAAC"}
-77
View File
@@ -1,77 +0,0 @@
/**
* Core Game Engine
* Manages game state and processes actions
*/
import { GameEngine, ActionResult } from '../interfaces/engine';
import { WorldModel, GameState } from '../interfaces/world-model';
import { ActionResponse } from '../interfaces/llm';
export declare class TextAdventureEngine implements GameEngine {
private worldModel;
private gameState;
private actionHandlers;
constructor();
/**
* Load a world model from a file
*/
loadWorld(worldModelPath: string): Promise<void>;
/**
* Get the current game state
*/
getCurrentState(): GameState;
/**
* Get the world model
*/
getWorldModel(): WorldModel;
/**
* Process an action from the player
*/
processAction(action: ActionResponse): ActionResult;
/**
* Save the current game state to a file
*/
saveGame(filename: string): Promise<void>;
/**
* Load a game state from a save file
*/
loadGame(filename: string): Promise<void>;
/**
* Get a list of available actions in the current context
*/
getAvailableActions(): string[];
/**
* Get a list of visible objects in the current room
*/
getVisibleObjects(): string[];
/**
* Get a list of visible characters in the current room
*/
getVisibleCharacters(): string[];
/**
* Get the description of the current room
*/
getCurrentRoomDescription(): string;
/**
* Start the game and return the introduction text
*/
start(): Promise<string>;
/**
* End the game (cleanup resources if needed)
*/
end(): void;
/**
* Get the current room object
*/
private getCurrentRoom;
/**
* Register default action handlers
*/
private registerDefaultActionHandlers;
/**
* Find an object by name in a list of object IDs
*/
private findObjectByName;
/**
* Find a character by name in a list of character IDs
*/
private findCharacterByName;
}
-607
View File
@@ -1,607 +0,0 @@
"use strict";
/**
* Core Game Engine
* Manages game state and processes actions
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextAdventureEngine = void 0;
const fs = __importStar(require("fs/promises"));
const yaml_parser_1 = require("../world-model/yaml-parser");
class TextAdventureEngine {
constructor() {
this.worldModel = null;
this.gameState = null;
this.actionHandlers = {};
this.registerDefaultActionHandlers();
}
/**
* Load a world model from a file
*/
async loadWorld(worldModelPath) {
try {
this.worldModel = await yaml_parser_1.YamlWorldParser.loadFromFile(worldModelPath);
this.gameState = { ...this.worldModel.initialState };
// Mark the initial room as visited
if (!this.gameState.visitedRooms.includes(this.gameState.currentRoomId)) {
this.gameState.visitedRooms.push(this.gameState.currentRoomId);
}
}
catch (error) {
console.error(`Failed to load world from ${worldModelPath}:`, error);
throw new Error(`Could not load world: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get the current game state
*/
getCurrentState() {
if (!this.gameState) {
throw new Error('Game state not initialized. Please load a world first.');
}
return { ...this.gameState };
}
/**
* Get the world model
*/
getWorldModel() {
if (!this.worldModel) {
throw new Error('World model not initialized. Please load a world first.');
}
return this.worldModel;
}
/**
* Process an action from the player
*/
processAction(action) {
if (!this.worldModel || !this.gameState) {
return {
success: false,
message: 'Game not initialized',
stateChanged: false
};
}
const handler = this.actionHandlers[action.action.toLowerCase()];
if (!handler) {
return {
success: false,
message: `I don't know how to "${action.action}"`,
stateChanged: false
};
}
return handler(this.gameState, this.worldModel, action);
}
/**
* Save the current game state to a file
*/
async saveGame(filename) {
if (!this.gameState || !this.worldModel) {
throw new Error('Cannot save: game not initialized');
}
const saveData = {
worldModelName: this.worldModel.title,
worldModelVersion: this.worldModel.version,
timestamp: new Date().toISOString(),
gameState: this.gameState
};
try {
await fs.writeFile(filename, JSON.stringify(saveData, null, 2), 'utf8');
}
catch (error) {
console.error(`Failed to save game to ${filename}:`, error);
throw new Error(`Could not save game: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Load a game state from a save file
*/
async loadGame(filename) {
try {
const fileContents = await fs.readFile(filename, 'utf8');
const saveData = JSON.parse(fileContents);
// Check if the save file matches the current world model
if (!this.worldModel) {
throw new Error('World model not loaded');
}
if (saveData.worldModelName !== this.worldModel.title ||
saveData.worldModelVersion !== this.worldModel.version) {
throw new Error('Save file is for a different world or version');
}
// Load the game state
this.gameState = saveData.gameState;
}
catch (error) {
console.error(`Failed to load game from ${filename}:`, error);
throw new Error(`Could not load save file: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get a list of available actions in the current context
*/
getAvailableActions() {
if (!this.worldModel)
return [];
// Common actions always available
const availableActions = ['look', 'inventory', 'help'];
// Add movement actions based on current room exits
const currentRoom = this.getCurrentRoom();
if (currentRoom) {
currentRoom.exits.forEach(exit => {
availableActions.push(`go ${exit.direction.toLowerCase()}`);
});
}
// Add object interactions based on visible objects
const visibleObjects = this.getVisibleObjects();
const objects = this.worldModel.objects;
visibleObjects.forEach(objId => {
const obj = objects[objId];
if (obj) {
obj.allowedActions.forEach(action => {
availableActions.push(`${action} ${obj.name.toLowerCase()}`);
});
}
});
// Add character interactions
const visibleCharacters = this.getVisibleCharacters();
visibleCharacters.forEach(charId => {
availableActions.push(`talk to ${this.worldModel.characters[charId].name.toLowerCase()}`);
});
// Add inventory object actions
this.gameState.inventory.forEach(objId => {
const obj = objects[objId];
if (obj) {
obj.allowedActions.forEach(action => {
availableActions.push(`${action} ${obj.name.toLowerCase()}`);
});
}
});
return Array.from(new Set(availableActions)); // Remove duplicates
}
/**
* Get a list of visible objects in the current room
*/
getVisibleObjects() {
if (!this.worldModel || !this.gameState)
return [];
const currentRoom = this.getCurrentRoom();
if (!currentRoom)
return [];
const visibleObjects = [...currentRoom.objects];
// Add objects from open containers
currentRoom.objects.forEach(objId => {
const obj = this.worldModel.objects[objId];
if (obj && obj.traits.includes('container') && obj.states?.open && obj.containedObjects) {
visibleObjects.push(...obj.containedObjects);
}
});
return visibleObjects;
}
/**
* Get a list of visible characters in the current room
*/
getVisibleCharacters() {
if (!this.worldModel || !this.gameState)
return [];
const currentRoom = this.getCurrentRoom();
return currentRoom ? currentRoom.characters : [];
}
/**
* Get the description of the current room
*/
getCurrentRoomDescription() {
const currentRoom = this.getCurrentRoom();
if (!currentRoom)
return 'You are in a void. Something has gone wrong.';
return currentRoom.description;
}
/**
* Start the game and return the introduction text
*/
async start() {
if (!this.worldModel) {
throw new Error('World not loaded. Please load a world before starting.');
}
// Reset game state to initial state
this.gameState = { ...this.worldModel.initialState };
return this.worldModel.introduction;
}
/**
* End the game (cleanup resources if needed)
*/
end() {
// Cleanup could happen here if needed
console.log('Game ended');
}
/**
* Get the current room object
*/
getCurrentRoom() {
if (!this.worldModel || !this.gameState)
return null;
const roomId = this.gameState.currentRoomId;
return this.worldModel.rooms[roomId] || null;
}
/**
* Register default action handlers
*/
registerDefaultActionHandlers() {
// Look action
this.actionHandlers['look'] = (state, world, action) => {
const room = world.rooms[state.currentRoomId];
// If an object is specified, look at that object
if (action.object) {
// Try to find the object in the room or inventory
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, [...visibleObjects, ...state.inventory]);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
return {
success: true,
message: obj.description,
stateChanged: false
};
}
// Look at the room
const objectDescriptions = room.objects
.map(id => world.objects[id])
.map(obj => `You can see ${obj.name.toLowerCase()} here.`);
const characterDescriptions = room.characters
.map(id => world.characters[id])
.map(char => `${char.name} is here.`);
const exitDescriptions = room.exits
.map(exit => `There is an exit ${exit.direction.toLowerCase()}${exit.description ? ` (${exit.description})` : ''}.`);
const fullDescription = [
room.description,
...objectDescriptions,
...characterDescriptions,
...exitDescriptions
].join('\n');
return {
success: true,
message: fullDescription,
stateChanged: false
};
};
// Go action
this.actionHandlers['go'] = (state, world, action) => {
const room = world.rooms[state.currentRoomId];
if (!action.object) {
return {
success: false,
message: 'Go where?',
stateChanged: false
};
}
// Find the exit that matches the direction
const direction = action.object.toLowerCase();
const exit = room.exits.find(e => e.direction.toLowerCase() === direction);
if (!exit) {
return {
success: false,
message: `You can't go ${direction} from here.`,
stateChanged: false
};
}
if (exit.isLocked) {
if (!exit.keyId) {
return {
success: false,
message: `The way ${direction} is locked.`,
stateChanged: false
};
}
if (!state.inventory.includes(exit.keyId)) {
return {
success: false,
message: `The way ${direction} is locked and you don't have the key.`,
stateChanged: false
};
}
// Player has the key, unlock the exit
exit.isLocked = false;
return {
success: true,
message: `You unlock the way ${direction} and proceed.`,
stateChanged: true,
newState: {
...state,
currentRoomId: exit.targetRoomId,
visitedRooms: state.visitedRooms.includes(exit.targetRoomId)
? state.visitedRooms
: [...state.visitedRooms, exit.targetRoomId]
}
};
}
// Exit is not locked, just move
return {
success: true,
message: `You go ${direction}.`,
stateChanged: true,
newState: {
...state,
currentRoomId: exit.targetRoomId,
visitedRooms: state.visitedRooms.includes(exit.targetRoomId)
? state.visitedRooms
: [...state.visitedRooms, exit.targetRoomId]
}
};
};
// Take action
this.actionHandlers['take'] = (state, world, action) => {
if (!action.object) {
return {
success: false,
message: 'Take what?',
stateChanged: false
};
}
// Find the object in the current room
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, visibleObjects);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Check if the object can be taken
if (!obj.traits.includes('takeable')) {
return {
success: false,
message: `You can't take the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
}
// Remove object from room and add to inventory
const room = world.rooms[state.currentRoomId];
const newRoomObjects = room.objects.filter(id => id !== objId);
room.objects = newRoomObjects;
// Update state
return {
success: true,
message: `You take the ${obj.name.toLowerCase()}.`,
stateChanged: true,
newState: {
...state,
inventory: [...state.inventory, objId]
}
};
};
// Inventory action
this.actionHandlers['inventory'] = (state, world) => {
if (state.inventory.length === 0) {
return {
success: true,
message: 'Your inventory is empty.',
stateChanged: false
};
}
const items = state.inventory
.map(id => world.objects[id])
.map(obj => obj.name)
.join(', ');
return {
success: true,
message: `You are carrying: ${items}.`,
stateChanged: false
};
};
// Drop action
this.actionHandlers['drop'] = (state, world, action) => {
if (!action.object) {
return {
success: false,
message: 'Drop what?',
stateChanged: false
};
}
// Find the object in the inventory
const objId = this.findObjectByName(action.object, state.inventory);
if (!objId) {
return {
success: false,
message: `You don't have any ${action.object}.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Remove object from inventory and add to room
const room = world.rooms[state.currentRoomId];
room.objects.push(objId);
// Update state
return {
success: true,
message: `You drop the ${obj.name.toLowerCase()}.`,
stateChanged: true,
newState: {
...state,
inventory: state.inventory.filter(id => id !== objId)
}
};
};
// Use action
this.actionHandlers['use'] = (state, world, action) => {
if (!action.object) {
return {
success: false,
message: 'Use what?',
stateChanged: false
};
}
// Find the object in inventory or visible objects
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, [...state.inventory, ...visibleObjects]);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Check if the object can be used
if (!obj.allowedActions.includes('use')) {
return {
success: false,
message: `You can't use the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
}
// Check if there's a target
if (action.target) {
const targetId = this.findObjectByName(action.target, [...state.inventory, ...visibleObjects]);
if (!targetId) {
return {
success: false,
message: `You don't see any ${action.target} here.`,
stateChanged: false
};
}
const target = world.objects[targetId];
// TODO: Implement object-specific use logic (could be extended with a more sophisticated system)
return {
success: true,
message: `You use the ${obj.name.toLowerCase()} on the ${target.name.toLowerCase()}.`,
stateChanged: false
};
}
// Simple use without target
return {
success: true,
message: `You use the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
};
// Talk action
this.actionHandlers['talk'] = (state, world, action) => {
if (!action.object) {
return {
success: false,
message: 'Talk to whom?',
stateChanged: false
};
}
// Find the character in the room
const visibleCharacters = this.getVisibleCharacters();
const charId = this.findCharacterByName(action.object, visibleCharacters);
if (!charId) {
return {
success: false,
message: `You don't see anyone called ${action.object} here.`,
stateChanged: false
};
}
const character = world.characters[charId];
// If a topic is provided
if (action.parameters?.topic) {
const topic = action.parameters.topic.toLowerCase();
const response = character.dialogue[topic] || character.defaultResponse;
return {
success: true,
message: `${character.name}: "${response}"`,
stateChanged: false
};
}
// No specific topic
return {
success: true,
message: `${character.name} looks ready to talk. You could ask about: ${Object.keys(character.dialogue).join(', ')}.`,
stateChanged: false
};
};
// Help action
this.actionHandlers['help'] = () => {
return {
success: true,
message: [
'Available commands:',
'- look: Examine your surroundings or a specific object',
'- go [direction]: Move in a direction',
'- take [object]: Pick up an object',
'- drop [object]: Put down an object',
'- inventory: Check what you\'re carrying',
'- use [object] (on [target]): Use an object, optionally on another object',
'- talk to [character] (about [topic]): Speak with a character',
'- help: Show this help text',
'',
'You can type commands in natural language. The AI will interpret your intent.'
].join('\n'),
stateChanged: false
};
};
// Examine action (alias for look)
this.actionHandlers['examine'] = this.actionHandlers['look'];
}
/**
* Find an object by name in a list of object IDs
*/
findObjectByName(name, objectIds) {
if (!this.worldModel)
return null;
const normalizedName = name.toLowerCase();
for (const id of objectIds) {
const obj = this.worldModel.objects[id];
if (obj && obj.name.toLowerCase() === normalizedName) {
return id;
}
}
return null;
}
/**
* Find a character by name in a list of character IDs
*/
findCharacterByName(name, characterIds) {
if (!this.worldModel)
return null;
const normalizedName = name.toLowerCase();
for (const id of characterIds) {
const character = this.worldModel.characters[id];
if (character && character.name.toLowerCase() === normalizedName) {
return id;
}
}
return null;
}
}
exports.TextAdventureEngine = TextAdventureEngine;
//# sourceMappingURL=game-engine.js.map
File diff suppressed because one or more lines are too long
-30
View File
@@ -1,30 +0,0 @@
import { TurnResult } from '../interfaces/turn-result';
export interface InkCompileResult {
sourcePath: string;
outputPath: string;
warningCount: number;
}
export declare function compileInkSource(sourcePath: string, outputPath: string): InkCompileResult;
export declare class InkEngine {
private readonly storyPath;
private story;
private nextTurnId;
private storyJson;
private readonly choicePreviewTagKeys;
constructor(storyPath: string);
isRunning(): boolean;
newGame(): TurnResult;
chooseChoice(choiceIndex: number): TurnResult;
saveGame(): string;
resumeGame(savedState: string): void;
loadGame(savedState: string): TurnResult;
private restoreState;
private loadStory;
private continueStory;
private getChoiceTags;
private extractChoicePreviewTags;
private resolveInkPath;
private findNamedInkChild;
private getInkContainerMap;
private isNamedContainerMap;
}
-292
View File
@@ -1,292 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InkEngine = void 0;
exports.compileInkSource = compileInkSource;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const inkjs_1 = require("inkjs");
const tag_parser_1 = require("../utils/tag-parser");
const { Compiler } = require('inkjs/full');
function compileInkSource(sourcePath, outputPath) {
const resolvedSource = path_1.default.resolve(sourcePath);
const resolvedOutput = path_1.default.resolve(outputPath);
if (!(0, fs_1.existsSync)(resolvedSource)) {
throw new Error(`Ink source file not found: ${resolvedSource}`);
}
const warnings = [];
const errors = [];
const source = (0, fs_1.readFileSync)(resolvedSource, 'utf8').replace(/^\uFEFF/, '');
const sourceDir = path_1.default.dirname(resolvedSource);
const fileHandler = {
ResolveInkFilename: (filename) => path_1.default.isAbsolute(filename) ? filename : path_1.default.resolve(sourceDir, filename),
LoadInkFileContents: (filename) => (0, fs_1.readFileSync)(path_1.default.isAbsolute(filename) ? filename : path_1.default.resolve(sourceDir, filename), 'utf8')
.replace(/^\uFEFF/, ''),
};
const compiler = new Compiler(source, {
sourceFilename: resolvedSource,
fileHandler,
errorHandler: (message, type) => {
if (type === 1) {
warnings.push(message);
}
else {
errors.push(message);
}
},
});
const story = compiler.Compile();
if (!story || errors.length > 0) {
throw new Error(`Ink compilation failed:\n${errors.join('\n')}`);
}
if (warnings.length > 0) {
warnings.forEach((warning) => console.warn(`[ink] ${warning}`));
}
(0, fs_1.mkdirSync)(path_1.default.dirname(resolvedOutput), { recursive: true });
(0, fs_1.writeFileSync)(resolvedOutput, story.ToJson(), 'utf8');
return {
sourcePath: resolvedSource,
outputPath: resolvedOutput,
warningCount: warnings.length,
};
}
class InkEngine {
constructor(storyPath) {
this.storyPath = storyPath;
this.story = null;
this.nextTurnId = 1;
this.storyJson = null;
this.choicePreviewTagKeys = new Set(['action', 'key', 'letter', 'optional', 'gated', 'sort']);
}
isRunning() {
if (!this.story)
return false;
return this.story.canContinue || this.story.currentChoices.length > 0;
}
newGame() {
this.story = this.loadStory();
this.nextTurnId = 1;
return this.continueStory();
}
chooseChoice(choiceIndex) {
if (!this.story) {
throw new Error('No active Ink story');
}
const choice = this.story.currentChoices.find((item) => item.index === choiceIndex);
if (!choice) {
throw new Error(`Ink choice ${choiceIndex} is not available`);
}
this.story.ChooseChoiceIndex(choice.index);
return this.continueStory();
}
saveGame() {
if (!this.story) {
throw new Error('No active Ink story to save');
}
return JSON.stringify({
inkState: this.story.state.toJson(),
nextTurnId: this.nextTurnId,
});
}
resumeGame(savedState) {
this.restoreState(savedState);
}
loadGame(savedState) {
this.restoreState(savedState);
return this.continueStory();
}
restoreState(savedState) {
this.story = this.loadStory();
let inkState = savedState;
try {
const parsed = JSON.parse(savedState);
if (parsed && typeof parsed.inkState === 'string') {
inkState = parsed.inkState;
if (Number.isInteger(parsed.nextTurnId)) {
this.nextTurnId = Math.max(1, parsed.nextTurnId);
}
}
}
catch {
// Backward compatibility with raw Ink state JSON.
}
this.story.state.LoadJson(inkState);
}
loadStory() {
const resolvedPath = path_1.default.resolve(this.storyPath);
if (!(0, fs_1.existsSync)(resolvedPath)) {
throw new Error(`Ink story file not found: ${resolvedPath}`);
}
this.storyJson = JSON.parse((0, fs_1.readFileSync)(resolvedPath, 'utf8'));
return new inkjs_1.Story(this.storyJson);
}
continueStory() {
if (!this.story) {
throw new Error('No active Ink story');
}
const paragraphs = [];
const globalTags = [];
const turnTags = [];
while (this.story.canContinue) {
const rawText = this.story.Continue();
const text = String(rawText || '').trim();
const tags = (0, tag_parser_1.parseTags)(this.story.currentTags || []);
turnTags.push(...tags);
tags
.filter((tag) => tag.key === 'title' || tag.key === 'author')
.forEach((tag) => globalTags.push(tag));
if (text) {
paragraphs.push({ text, tags });
}
else {
tags.forEach((tag) => globalTags.push(tag));
}
}
const choices = this.story.currentChoices.map((choice) => {
const tags = this.getChoiceTags(choice);
const category = (0, tag_parser_1.getTagValue)(tags, 'action');
const letter = (0, tag_parser_1.getTagValue)(tags, 'letter') || (0, tag_parser_1.getTagValue)(tags, 'key');
return {
index: choice.index,
text: String(choice.text || '').trim(),
tags,
category,
letter,
};
});
const inputMode = choices.length > 0 ? 'choice' : 'end';
const gameState = {};
if (inputMode === 'end') {
const errorTag = turnTags.find((tag) => tag.key === 'error');
const scoreTag = turnTags.find((tag) => tag.key === 'score');
if (!errorTag && !scoreTag) {
const message = 'Ink story ended without an explicit #score ending tag.';
const generatedErrorTag = { key: 'error', value: message };
globalTags.push(generatedErrorTag);
turnTags.push(generatedErrorTag);
}
const finalErrorTag = turnTags.find((tag) => tag.key === 'error');
const finalScoreTag = turnTags.find((tag) => tag.key === 'score');
if (finalErrorTag) {
gameState.endState = {
type: 'error',
message: finalErrorTag.value || finalErrorTag.param,
};
}
else if (finalScoreTag) {
const numericScore = Number(finalScoreTag?.value);
if (Number.isFinite(numericScore)) {
gameState.score = numericScore;
}
gameState.endState = {
type: 'intended',
message: finalScoreTag.value || finalScoreTag.param,
};
}
}
return {
turnId: this.nextTurnId++,
paragraphs,
choices,
inputMode,
globalTags: globalTags.length > 0 ? globalTags : undefined,
gameState: Object.keys(gameState).length > 0 ? gameState : undefined,
};
}
getChoiceTags(choice) {
const directTags = (0, tag_parser_1.parseTags)(choice?.tags || []);
const previewTags = this.extractChoicePreviewTags(choice);
const merged = new Map();
[...previewTags, ...directTags].forEach((tag) => {
merged.set(`${tag.key}:${tag.value || ''}:${tag.param || ''}`, tag);
});
return Array.from(merged.values());
}
extractChoicePreviewTags(choice) {
const pathString = String(choice?.pathStringOnChoice || choice?.targetPath?.toString?.() || '').trim();
if (!pathString || !this.storyJson)
return [];
const container = this.resolveInkPath(pathString);
if (!Array.isArray(container))
return [];
const tags = [];
for (let index = 0; index < container.length; index += 1) {
const token = container[index];
if (typeof token === 'string' && token.replace(/^\^/, '').trim() === '')
continue;
if (token === '\n')
continue;
if (token !== '#')
break;
const rawParts = [];
index += 1;
while (index < container.length && container[index] !== '/#') {
const part = container[index];
if (typeof part === 'string') {
rawParts.push(part.replace(/^\^/, ''));
}
index += 1;
}
const tag = (0, tag_parser_1.parseTags)([rawParts.join('').trim()])[0];
if (tag && this.choicePreviewTagKeys.has(tag.key)) {
tags.push(tag);
}
}
return tags;
}
resolveInkPath(pathString) {
const parts = pathString.split('.').filter(Boolean);
let node = this.storyJson?.root;
for (const part of parts) {
if (!node)
return null;
if (Array.isArray(node) && /^\d+$/.test(part)) {
node = node[Number(part)];
}
else if (Array.isArray(node)) {
node = this.findNamedInkChild(node, part);
}
else if (this.isNamedContainerMap(node) && part in node) {
node = node[part];
}
else {
return null;
}
}
return node;
}
findNamedInkChild(container, part) {
for (let index = container.length - 1; index >= 0; index -= 1) {
const item = container[index];
if (this.isNamedContainerMap(item) && part in item) {
return item[part];
}
if (!Array.isArray(item))
continue;
const namedMap = this.getInkContainerMap(item);
if (namedMap?.['#n'] === part) {
return item;
}
if (namedMap && part in namedMap) {
return namedMap[part];
}
}
return null;
}
getInkContainerMap(container) {
for (let index = container.length - 1; index >= 0; index -= 1) {
const item = container[index];
if (this.isNamedContainerMap(item)) {
return item;
}
}
return null;
}
isNamedContainerMap(value) {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
}
exports.InkEngine = InkEngine;
//# sourceMappingURL=ink-engine.js.map
-1
View File
File diff suppressed because one or more lines are too long
-84
View File
@@ -1,84 +0,0 @@
/**
* Z-code LLM Engine
*
* Runs a Z-machine story file as a headless subprocess via the
* `ifvms` CLI, and wraps every I/O exchange with OpenRouter LLM calls that
* translate free natural-language player input into parser commands and
* re-voice the Z-machine's raw output as polished narrative prose.
*
* Configuration (environment variables):
* ZCODE_STORY_FILE - path to the .z5/.z8/.bin story file (default: ./data/z-code/zork1.bin)
* ZCODE_MAX_RETRIES - maximum command retry attempts per turn (default: 3)
* ZCODE_HISTORY_SIZE - player-facing outputs stored per room (default: 5)
* OPENROUTER_API_KEY, OPENROUTER_MODEL - required
*/
import { TurnResult } from '../interfaces/turn-result';
export interface ZcodeSession {
characterDescription: string;
notes: string[];
recentParagraphs: string[];
rawTranscript: string[];
turnCount: number;
timeOfDay: string;
weather: string;
virtualInventory: string[];
/** roomName -> last N player-facing output strings */
roomHistory: Record<string, string[]>;
currentRoom: string;
running: boolean;
}
export type ZcodeTurnResult = TurnResult;
export declare class ZcodeLlmEngine {
private zmachine;
private session;
private prompts;
private llm;
private model;
private resolvedFallbackModel;
private llmCallCounter;
private maxRetries;
private historySize;
private nextTurnId;
private storyPath;
private static readonly DEPRECATED_MODEL_REPLACEMENTS;
constructor(options?: {
storyPath?: string;
promptDir?: string;
});
private createCompletion;
private resolveFallbackModel;
isRunning(): boolean;
/**
* Start a new game: launch the Z-machine story, generate the player character, rewrite the
* intro text, and return the first TurnResult for the client.
*/
newGame(): Promise<ZcodeTurnResult>;
/**
* Process player free-text input. Returns the next TurnResult.
*/
processInput(userInput: string): Promise<ZcodeTurnResult>;
private runCommandPlan;
/**
* Save the current game state. Returns a JSON string suitable for storing
* in the socket's save-game slot map.
*/
saveGame(): Promise<string>;
/**
* Load a previously saved game. Returns the first TurnResult after restore.
*/
loadGame(savedJson: string): Promise<ZcodeTurnResult>;
private runSingleCommandLoop;
private generateCharacter;
private rewriteText;
private translateCommand;
private evaluateOutput;
private executeTool;
private appendRecentParagraph;
private extractCommands;
private appendRawTranscript;
private advanceNarratorState;
private getDeterministicCommandPlan;
private appendRoomHistory;
private buildCommonVars;
private buildTurnResult;
}
-989
View File
@@ -1,989 +0,0 @@
"use strict";
/**
* Z-code LLM Engine
*
* Runs a Z-machine story file as a headless subprocess via the
* `ifvms` CLI, and wraps every I/O exchange with OpenRouter LLM calls that
* translate free natural-language player input into parser commands and
* re-voice the Z-machine's raw output as polished narrative prose.
*
* Configuration (environment variables):
* ZCODE_STORY_FILE - path to the .z5/.z8/.bin story file (default: ./data/z-code/zork1.bin)
* ZCODE_MAX_RETRIES - maximum command retry attempts per turn (default: 3)
* ZCODE_HISTORY_SIZE - player-facing outputs stored per room (default: 5)
* OPENROUTER_API_KEY, OPENROUTER_MODEL - required
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZcodeLlmEngine = void 0;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const yaml = __importStar(require("js-yaml"));
const axios_1 = __importDefault(require("axios"));
const dotenv = __importStar(require("dotenv"));
const turn_result_1 = require("../interfaces/turn-result");
dotenv.config();
const DEBUG_ENABLED = /^(1|true|yes|on)$/i.test(process.env.ZCODE_DEBUG ?? '');
function debugLog(message, details) {
if (!DEBUG_ENABLED)
return;
if (typeof details === 'undefined') {
console.log(`[ZcodeLlm:debug] ${message}`);
return;
}
console.log(`[ZcodeLlm:debug] ${message}`, details);
}
function compactText(text, maxLength = 12000) {
if (text.length <= maxLength)
return text;
return `${text.slice(0, maxLength)}\n...[truncated ${text.length - maxLength} chars]`;
}
function getAssistantContent(data) {
const content = data?.choices?.[0]?.message?.content;
if (typeof content === 'string')
return content;
if (Array.isArray(content)) {
return content
.map((part) => {
if (typeof part === 'string')
return part;
if (typeof part?.text === 'string')
return part.text;
if (typeof part?.content === 'string')
return part.content;
return '';
})
.join('')
.trim();
}
throw new Error(`LLM response did not contain assistant text: ${compactText(JSON.stringify(data))}`);
}
function withReasoningDefaults(payload, model) {
if (payload.reasoning || !/\bgpt-5/i.test(model))
return payload;
return {
...payload,
reasoning: {
effort: process.env.OPENROUTER_REASONING_EFFORT ?? 'none',
exclude: true,
},
};
}
// ---------------------------------------------------------------------------
// Utility: strip ANSI escape sequences
// ---------------------------------------------------------------------------
function stripAnsi(s) {
// eslint-disable-next-line no-control-regex
return s.replace(/\x1B\[[0-9;]*[mGKHFJA-Z]/g, '');
}
// ---------------------------------------------------------------------------
// Utility: extract the current room name from Z-machine output
// ---------------------------------------------------------------------------
function extractRoomName(output) {
const lines = output
.split('\n')
.map(l => l.trim())
.filter(l => l.length > 0);
if (lines.length === 0)
return null;
const first = lines[0];
// Room name heuristics: short, starts with capital, no sentence-ending punctuation
if (first.length < 65 &&
/^[A-Z]/.test(first) &&
!/[.!?]$/.test(first) &&
!/^(You |I |It |There |The [a-z])/.test(first)) {
return first;
}
return null;
}
function isReadCommand(command) {
return /^READ\b/i.test(command.trim());
}
function isParserComplaint(output) {
const text = output.toLowerCase();
return [
"i don't know the word",
"i don't understand",
"that's not a verb",
"you can't see any",
"you don't have",
"you aren't carrying",
"what do you want to",
"what do you want to read",
"what do you want to take",
"which do you mean",
"there is no",
].some(fragment => text.includes(fragment));
}
function formatExactReadOutput(command, zcodeOutput) {
const object = command.replace(/^READ\s+/i, '').trim().toLowerCase();
const label = object ? `the ${object}` : 'it';
const cleanedOutput = zcodeOutput
.split('\n')
.filter((line, index) => index !== 0 || line.trim().toUpperCase() !== command.trim().toUpperCase())
.join('\n')
.trim();
return `You read ${label}.\n\n${cleanedOutput}`;
}
function pickInitialWeather() {
const options = [
'cool, unsettled air under a low grey sky',
'a dry bright afternoon with thin wind moving through the grass',
'misty weather with damp earth-smell clinging to everything outside',
'a mild overcast day, quiet enough that small sounds carry',
];
return options[Math.floor(Math.random() * options.length)];
}
function timeOfDayForTurn(turnCount) {
const phases = [
'late morning',
'early afternoon',
'late afternoon',
'dusk',
'early evening',
'night',
'deep night',
'pre-dawn',
'morning',
];
return phases[Math.floor(turnCount / 12) % phases.length];
}
function evolveWeather(previous, turnCount) {
if (turnCount > 0 && turnCount % 9 !== 0)
return previous;
const transitions = [
'the air has cooled and carries a faint mineral dampness',
'the wind has shifted, restless but not yet stormy',
'the light has thinned behind a veil of cloud',
'the weather holds steady, quiet and watchful',
'a trace of moisture gathers in the air',
];
return transitions[Math.floor(turnCount / 9) % transitions.length];
}
// ---------------------------------------------------------------------------
// ZcodeProcess manages the ifvms zvm child process
// ---------------------------------------------------------------------------
class ZcodeProcess {
constructor() {
this.proc = null;
this.outputBuffer = '';
this.pendingResolve = null;
this.debounceTimer = null;
}
/** Start the Z-machine with the given story file, return the opening text. */
async launch(storyPath) {
const zvm = this.locateZvm();
this.proc = (0, child_process_1.spawn)(zvm, [storyPath], {
stdio: ['pipe', 'pipe', 'pipe'],
shell: true,
cwd: process.cwd(),
});
this.proc.stdout.on('data', (chunk) => {
this.outputBuffer += stripAnsi(chunk.toString());
this.scheduleResolve();
});
this.proc.stderr.on('data', (chunk) => {
// Log but don't throw ifvms may emit warnings on stderr
console.warn('[zvm]', chunk.toString().trim());
});
this.proc.on('exit', () => {
// If the process exits while we are waiting for output, resolve immediately
if (this.pendingResolve) {
const resolver = this.pendingResolve;
this.pendingResolve = null;
resolver(this.outputBuffer.trim());
this.outputBuffer = '';
}
this.proc = null;
});
return this.waitForPrompt();
}
/** Send a line of input and return all output until the next prompt. */
async sendLine(text) {
if (!this.proc)
throw new Error('Z-machine process is not running');
this.outputBuffer = '';
this.proc.stdin.write(text + '\n');
return this.waitForPrompt();
}
isAlive() {
return this.proc !== null && !this.proc.killed;
}
kill() {
if (this.proc) {
this.proc.kill();
this.proc = null;
}
}
// ---- private ----
waitForPrompt() {
return new Promise((resolve) => {
// Wrap to allow debounce timer to cancel a previous waiter safely
const wrapped = (text) => resolve(text);
this.pendingResolve = wrapped;
// Safety timeout: if no prompt detected after 15 s, resolve with what we have
const safety = setTimeout(() => {
if (this.pendingResolve === wrapped) {
this.pendingResolve = null;
const text = this.outputBuffer.trim();
this.outputBuffer = '';
resolve(text);
}
}, 15000);
// Ensure the safety timeout does not keep Node alive indefinitely
if (safety.unref)
safety.unref();
// Override so debounce also cancels the safety timer
this.pendingResolve = (text) => {
clearTimeout(safety);
resolve(text);
};
// Data may already be buffered
this.scheduleResolve();
});
}
/** Debounced check: resolve when the buffer ends with a Z-machine prompt. */
scheduleResolve() {
if (!/\n>\s*$/.test(this.outputBuffer))
return;
if (this.debounceTimer)
clearTimeout(this.debounceTimer);
this.debounceTimer = setTimeout(() => {
this.debounceTimer = null;
if (!this.pendingResolve)
return;
const text = this.outputBuffer.replace(/\n>\s*$/, '').trim();
this.outputBuffer = '';
const resolver = this.pendingResolve;
this.pendingResolve = null;
resolver(text);
}, 80);
}
locateZvm() {
const binDir = path.join(process.cwd(), 'node_modules', '.bin');
const candidates = process.platform === 'win32'
? ['zvm.cmd', 'zvm.ps1', 'zvm']
: ['zvm'];
for (const name of candidates) {
const full = path.join(binDir, name);
if (fs.existsSync(full))
return full;
}
// Fall through to shell PATH lookup (works if ifvms is installed globally)
return 'zvm';
}
}
// ---------------------------------------------------------------------------
// Prompt loader
// ---------------------------------------------------------------------------
function loadPrompts(promptDir) {
function load(filename) {
const filePath = path.join(promptDir, filename);
if (!fs.existsSync(filePath)) {
throw new Error(`Prompt file not found: ${filePath}`);
}
return yaml.load(fs.readFileSync(filePath, 'utf8'));
}
return {
characterGeneration: load('character-generation.yml'),
textRewriter: load('text-rewriter.yml'),
commandTranslator: load('command-translator.yml'),
outputEvaluator: load('output-evaluator.yml'),
};
}
function renderTemplate(template, vars) {
return template.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] ?? '');
}
function logLlmError(scope, err) {
if (axios_1.default.isAxiosError(err)) {
const ax = err;
console.error(`[ZcodeLlm] ${scope} failed: ${ax.message}`);
if (ax.response) {
console.error(`[ZcodeLlm] ${scope} status=${ax.response.status} data=`, ax.response.data);
if (ax.response.status === 404) {
console.error('[ZcodeLlm] Hint: OPENROUTER_MODEL is likely invalid or unavailable for your API key.');
}
}
return;
}
console.error(`[ZcodeLlm] ${scope} failed:`, err);
}
// ---------------------------------------------------------------------------
// ZcodeLlmEngine
// ---------------------------------------------------------------------------
class ZcodeLlmEngine {
constructor(options = {}) {
this.zmachine = new ZcodeProcess();
this.session = null;
this.resolvedFallbackModel = null;
this.llmCallCounter = 0;
this.nextTurnId = 1;
const apiKey = process.env.OPENROUTER_API_KEY;
const model = process.env.OPENROUTER_MODEL;
if (!apiKey || !model) {
throw new Error('Missing required environment variables: OPENROUTER_API_KEY and OPENROUTER_MODEL');
}
const replacement = ZcodeLlmEngine.DEPRECATED_MODEL_REPLACEMENTS[model] ?? null;
if (replacement) {
this.model = replacement;
console.warn(`[ZcodeLlm] Replacing deprecated model '${model}' with '${replacement}'.`);
}
else {
this.model = model;
}
debugLog('active LLM model configured', {
requestedModel: model,
activeModel: this.model,
});
this.maxRetries = parseInt(process.env.ZCODE_MAX_RETRIES ?? '3', 10);
this.historySize = parseInt(process.env.ZCODE_HISTORY_SIZE ?? '5', 10);
this.storyPath = path.resolve(options.storyPath ?? process.env.ZCODE_STORY_FILE ?? './data/z-code/zork1.bin');
const promptDir = path.resolve(options.promptDir ?? './data/zcode-prompts');
this.prompts = loadPrompts(promptDir);
this.llm = axios_1.default.create({
baseURL: 'https://openrouter.ai/api/v1',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
});
}
async createCompletion(payload) {
const withConfiguredModel = {
...withReasoningDefaults(payload, this.model),
model: this.model,
};
const callId = ++this.llmCallCounter;
debugLog(`LLM call #${callId} request`, {
model: this.model,
payload: compactText(JSON.stringify(withConfiguredModel, null, 2)),
});
try {
const response = await this.llm.post('/chat/completions', withConfiguredModel);
debugLog(`LLM call #${callId} response`, {
model: this.model,
status: response.status,
data: compactText(JSON.stringify(response.data, null, 2)),
});
return response;
}
catch (err) {
if (axios_1.default.isAxiosError(err) && err.response?.status === 404) {
const fallbackModel = await this.resolveFallbackModel();
this.model = fallbackModel;
console.warn(`[ZcodeLlm] Switching active model to '${fallbackModel}'.`);
const withFallbackModel = {
...withReasoningDefaults(payload, fallbackModel),
model: fallbackModel,
};
debugLog(`LLM call #${callId} fallback request`, {
model: fallbackModel,
payload: compactText(JSON.stringify(withFallbackModel, null, 2)),
});
const fallbackResponse = await this.llm.post('/chat/completions', withFallbackModel);
debugLog(`LLM call #${callId} fallback response`, {
model: fallbackModel,
status: fallbackResponse.status,
data: compactText(JSON.stringify(fallbackResponse.data, null, 2)),
});
return fallbackResponse;
}
debugLog(`LLM call #${callId} error`, {
message: err instanceof Error ? err.message : String(err),
});
throw err;
}
}
async resolveFallbackModel() {
if (this.resolvedFallbackModel)
return this.resolvedFallbackModel;
const preferred = [
process.env.OPENROUTER_FALLBACK_MODEL,
'openai/gpt-5.5',
'openai/gpt-5.4',
'openai/gpt-5.4-mini',
'openai/gpt-5.4-nano',
'openai/gpt-5.3-chat',
'~anthropic/claude-sonnet-latest',
'~anthropic/claude-opus-latest',
'anthropic/claude-sonnet-4.6',
'anthropic/claude-sonnet-4',
'openai/gpt-4o-mini',
].filter((v) => Boolean(v && v.trim()));
try {
const response = await this.llm.get('/models');
const ids = new Set(Array.isArray(response.data?.data)
? response.data.data
.map((m) => (typeof m?.id === 'string' ? m.id : null))
.filter((id) => Boolean(id))
: []);
debugLog('OpenRouter model list fetched for fallback resolution', {
preferred,
availableCount: ids.size,
});
for (const candidate of preferred) {
if (ids.has(candidate)) {
this.resolvedFallbackModel = candidate;
return candidate;
}
}
const firstAvailable = response.data?.data?.[0]?.id;
if (typeof firstAvailable === 'string' && firstAvailable.length > 0) {
this.resolvedFallbackModel = firstAvailable;
return firstAvailable;
}
}
catch (err) {
logLlmError('resolveFallbackModel', err);
}
this.resolvedFallbackModel = 'openai/gpt-4o-mini';
return this.resolvedFallbackModel;
}
// ---- Public API -----------------------------------------------------------
isRunning() {
return this.session?.running === true && this.zmachine.isAlive();
}
/**
* Start a new game: launch the Z-machine story, generate the player character, rewrite the
* intro text, and return the first TurnResult for the client.
*/
async newGame() {
// Kill any existing game
if (this.zmachine.isAlive())
this.zmachine.kill();
this.nextTurnId = 1;
if (!fs.existsSync(this.storyPath)) {
throw new Error(`Story file not found: ${this.storyPath}\n` +
'Place zork1.bin in ./data/z-code/ (see README in that folder).');
}
debugLog('launching Z-machine', { storyPath: this.storyPath });
const rawIntro = await this.zmachine.launch(this.storyPath);
debugLog('Z-machine intro output', compactText(rawIntro));
// Generate the player character before showing any text
const characterDescription = await this.generateCharacter();
this.session = {
characterDescription,
notes: [],
roomHistory: {},
currentRoom: extractRoomName(rawIntro) ?? 'Unknown Location',
recentParagraphs: [],
rawTranscript: [`[intro]\n${rawIntro}`],
turnCount: 0,
timeOfDay: timeOfDayForTurn(0),
weather: pickInitialWeather(),
virtualInventory: [],
running: true,
};
// Rewrite the opening text with the character's narrative voice
debugLog('session initialized', {
currentRoom: this.session.currentRoom,
characterDescription,
timeOfDay: this.session.timeOfDay,
weather: this.session.weather,
});
const introText = await this.rewriteText(rawIntro);
this.appendRecentParagraph(introText);
this.appendRoomHistory(this.session.currentRoom, introText);
return this.buildTurnResult(introText);
}
/**
* Process player free-text input. Returns the next TurnResult.
*/
async processInput(userInput) {
if (!this.session?.running) {
throw new Error('No active game session');
}
debugLog('processInput start', {
userInput,
currentRoom: this.session.currentRoom,
turnCount: this.session.turnCount,
timeOfDay: this.session.timeOfDay,
weather: this.session.weather,
notes: this.session.notes,
virtualInventory: this.session.virtualInventory,
});
this.advanceNarratorState();
const deterministicCommands = this.getDeterministicCommandPlan(userInput);
if (deterministicCommands.length > 0) {
debugLog('deterministic command plan selected', {
userInput,
commands: deterministicCommands,
});
return this.runCommandPlan(userInput, deterministicCommands);
}
const cmdResponse = await this.translateCommand(userInput);
debugLog('command translator parsed response', cmdResponse);
// Execute any tool calls first
if (cmdResponse.type === 'tools') {
for (const tool of cmdResponse.tools) {
this.executeTool(tool);
}
// If the translator also supplied a Z-machine command, continue to game loop
if (!cmdResponse.command && !cmdResponse.commands?.length) {
// Pure tool action — generate a brief acknowledgement via the rewriter
const ack = await this.rewriteText(`(The narrator pauses. ${userInput})`);
this.appendRecentParagraph(ack);
return this.buildTurnResult(ack);
}
}
if (cmdResponse.type === 'reply') {
this.appendRecentParagraph(cmdResponse.text);
return this.buildTurnResult(cmdResponse.text);
}
const commands = this.extractCommands(cmdResponse);
if (commands.length === 0) {
const fallback = await this.rewriteText("You hesitate, uncertain what action to take.");
this.appendRecentParagraph(fallback);
return this.buildTurnResult(fallback);
}
return this.runCommandPlan(userInput, commands);
}
async runCommandPlan(userInput, commands) {
const texts = [];
for (const command of commands) {
const text = await this.runSingleCommandLoop(userInput, command);
texts.push(text);
if (!this.isRunning())
break;
}
const combined = texts.join('\n\n');
return this.buildTurnResult(combined);
}
/**
* Save the current game state. Returns a JSON string suitable for storing
* in the socket's save-game slot map.
*/
async saveGame() {
if (!this.session)
throw new Error('No active session to save');
const tmpFile = path.join(os.tmpdir(), `zcode-save-${Date.now()}.qzl`);
try {
// Ask the Z-machine to save, supply the temp file path, and discard the output
await this.zmachine.sendLine('SAVE');
await this.zmachine.sendLine(tmpFile);
let zcodeSave = '';
if (fs.existsSync(tmpFile)) {
zcodeSave = fs.readFileSync(tmpFile).toString('base64');
}
return JSON.stringify({ session: this.session, zcodeSave });
}
finally {
if (fs.existsSync(tmpFile))
fs.unlinkSync(tmpFile);
}
}
/**
* Load a previously saved game. Returns the first TurnResult after restore.
*/
async loadGame(savedJson) {
var _a, _b, _c, _d, _e, _f;
const { session, zcodeSave } = JSON.parse(savedJson);
if (this.zmachine.isAlive())
this.zmachine.kill();
const tmpFile = path.join(os.tmpdir(), `zcode-restore-${Date.now()}.qzl`);
try {
fs.writeFileSync(tmpFile, Buffer.from(zcodeSave, 'base64'));
await this.zmachine.launch(this.storyPath);
await this.zmachine.sendLine('RESTORE');
const restoreOutput = await this.zmachine.sendLine(tmpFile);
this.session = { ...session, running: true };
(_a = this.session).rawTranscript ?? (_a.rawTranscript = []);
(_b = this.session).recentParagraphs ?? (_b.recentParagraphs = []);
(_c = this.session).virtualInventory ?? (_c.virtualInventory = []);
(_d = this.session).turnCount ?? (_d.turnCount = 0);
(_e = this.session).timeOfDay ?? (_e.timeOfDay = timeOfDayForTurn(this.session.turnCount));
(_f = this.session).weather ?? (_f.weather = pickInitialWeather());
const text = await this.rewriteText(restoreOutput);
this.appendRecentParagraph(text);
return this.buildTurnResult(text);
}
finally {
if (fs.existsSync(tmpFile))
fs.unlinkSync(tmpFile);
}
}
// ---- Core game loop -------------------------------------------------------
async runSingleCommandLoop(userIntent, firstCommand) {
let command = firstCommand;
let lastOutput = '';
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
debugLog('sending Z-machine command', {
userIntent,
command,
attempt,
maxRetries: this.maxRetries,
});
const rawOutput = await this.zmachine.sendLine(command);
lastOutput = rawOutput;
this.appendRawTranscript(command, rawOutput);
debugLog('received Z-machine output', {
command,
attempt,
output: compactText(rawOutput),
});
const newRoom = extractRoomName(rawOutput);
if (newRoom) {
this.session.currentRoom = newRoom;
debugLog('current room updated', newRoom);
}
if (isReadCommand(command) && !isParserComplaint(rawOutput)) {
const exactText = formatExactReadOutput(command, rawOutput);
debugLog('accepted exact READ output without LLM paraphrase', {
command,
text: compactText(exactText),
});
this.appendRecentParagraph(exactText);
this.appendRoomHistory(this.session.currentRoom, exactText);
return exactText;
}
const evalResponse = await this.evaluateOutput(userIntent, command, rawOutput, attempt);
debugLog('output evaluator decision', evalResponse);
if (evalResponse.decision === 'accept') {
this.appendRecentParagraph(evalResponse.text);
this.appendRoomHistory(this.session.currentRoom, evalResponse.text);
return evalResponse.text;
}
// Retry with the LLM-suggested command
if (attempt < this.maxRetries) {
debugLog('retrying with evaluator command', {
previousCommand: command,
nextCommand: evalResponse.command,
});
command = evalResponse.command;
}
}
// Max retries exceeded — force a rewrite of the last output
const fallbackText = await this.rewriteText(lastOutput);
this.appendRecentParagraph(fallbackText);
this.appendRoomHistory(this.session.currentRoom, fallbackText);
return fallbackText;
}
// ---- LLM calls ------------------------------------------------------------
async generateCharacter() {
const cfg = this.prompts.characterGeneration;
try {
const response = await this.createCompletion({
messages: [
{ role: 'system', content: cfg.system },
{ role: 'user', content: 'Create the player character now.' },
],
temperature: 0.9,
max_tokens: 600,
});
return getAssistantContent(response.data).trim();
}
catch (err) {
logLlmError('generateCharacter', err);
return 'You are a wary but curious explorer, driven more by persistence than bravery. You have come to the old house seeking answers, carrying a notebook of unfinished questions and a habit of checking every corner twice.';
}
}
async rewriteText(zcodeOutput) {
const cfg = this.prompts.textRewriter;
const vars = this.buildCommonVars();
vars['zcodeOutput'] = zcodeOutput;
try {
const response = await this.createCompletion({
messages: [
{ role: 'system', content: cfg.system },
{ role: 'user', content: renderTemplate(cfg.user_template, vars) },
],
temperature: 0.75,
max_tokens: 800,
});
return getAssistantContent(response.data).trim();
}
catch (err) {
logLlmError('rewriteText', err);
return zcodeOutput;
}
}
async translateCommand(userInput) {
const cfg = this.prompts.commandTranslator;
const vars = this.buildCommonVars();
vars['userInput'] = userInput;
try {
const response = await this.createCompletion({
messages: [
{ role: 'system', content: cfg.system },
{ role: 'user', content: renderTemplate(cfg.user_template, vars) },
],
temperature: 0.2,
max_tokens: 300,
response_format: { type: 'json_object' },
});
const parsed = JSON.parse(getAssistantContent(response.data));
return parsed;
}
catch (err) {
logLlmError('translateCommand', err);
// Fallback: pass input directly to Z-machine parser
return { type: 'command', command: userInput.toUpperCase() };
}
}
async evaluateOutput(userIntent, commandTried, zcodeOutput, attempt) {
const cfg = this.prompts.outputEvaluator;
const vars = this.buildCommonVars();
vars['userIntent'] = userIntent;
vars['commandTried'] = commandTried;
vars['zcodeOutput'] = zcodeOutput;
vars['attempt'] = String(attempt);
vars['maxAttempts'] = String(this.maxRetries);
try {
const response = await this.createCompletion({
messages: [
{ role: 'system', content: cfg.system },
{ role: 'user', content: renderTemplate(cfg.user_template, vars) },
],
temperature: 0.3,
max_tokens: 500,
response_format: { type: 'json_object' },
});
return JSON.parse(getAssistantContent(response.data));
}
catch (err) {
logLlmError('evaluateOutput', err);
// Fallback: accept the raw output as-is
return { decision: 'accept', text: zcodeOutput };
}
}
// ---- Session helpers -------------------------------------------------------
executeTool(tool) {
if (!this.session)
return;
debugLog('executing tool call', tool);
switch (tool.name) {
case 'update_character':
if (typeof tool.args['description'] === 'string') {
this.session.characterDescription = tool.args['description'];
debugLog('tool updated character', this.session.characterDescription);
}
break;
case 'add_note':
if (typeof tool.args['note'] === 'string') {
this.session.notes.push(tool.args['note']);
debugLog('tool added note', {
note: tool.args['note'],
notes: this.session.notes,
});
}
break;
case 'remove_note': {
const idx = Number(tool.args['index']);
if (Number.isInteger(idx) &&
idx >= 0 &&
idx < this.session.notes.length) {
this.session.notes.splice(idx, 1);
debugLog('tool removed note', {
index: idx,
notes: this.session.notes,
});
}
break;
}
case 'add_inventory_item': {
const item = String(tool.args['item'] ?? '').trim();
if (!item)
break;
const exists = this.session.virtualInventory.some((it) => it.toLowerCase() === item.toLowerCase());
if (!exists)
this.session.virtualInventory.push(item);
debugLog('tool added inventory item', {
item,
virtualInventory: this.session.virtualInventory,
});
break;
}
case 'remove_inventory_item': {
const item = String(tool.args['item'] ?? '').trim();
if (!item)
break;
this.session.virtualInventory = this.session.virtualInventory.filter((it) => it.toLowerCase() !== item.toLowerCase());
debugLog('tool removed inventory item', {
item,
virtualInventory: this.session.virtualInventory,
});
break;
}
}
}
appendRecentParagraph(text) {
if (!this.session)
return;
const trimmed = text.trim();
if (!trimmed)
return;
this.session.recentParagraphs.push(trimmed);
if (this.session.recentParagraphs.length > 10) {
this.session.recentParagraphs.splice(0, this.session.recentParagraphs.length - 10);
}
}
extractCommands(cmdResponse) {
const list = [];
if (cmdResponse.type === 'command') {
list.push(cmdResponse.command);
}
else if (cmdResponse.type === 'commands') {
list.push(...cmdResponse.commands);
}
else if (cmdResponse.type === 'tools') {
if (cmdResponse.command)
list.push(cmdResponse.command);
if (Array.isArray(cmdResponse.commands))
list.push(...cmdResponse.commands);
}
return list
.map((c) => String(c).trim())
.filter(Boolean)
.map((c) => c.toUpperCase());
}
appendRawTranscript(command, output) {
if (!this.session)
return;
this.session.rawTranscript.push([`> ${command}`, output.trim()].filter(Boolean).join('\n'));
if (this.session.rawTranscript.length > 12) {
this.session.rawTranscript.splice(0, this.session.rawTranscript.length - 12);
}
}
advanceNarratorState() {
if (!this.session)
return;
this.session.turnCount += 1;
this.session.timeOfDay = timeOfDayForTurn(this.session.turnCount);
this.session.weather = evolveWeather(this.session.weather, this.session.turnCount);
debugLog('narrator state advanced', {
turnCount: this.session.turnCount,
timeOfDay: this.session.timeOfDay,
weather: this.session.weather,
});
}
getDeterministicCommandPlan(userInput) {
const normalized = userInput.toLowerCase();
const context = [
this.session?.currentRoom ?? '',
this.session?.recentParagraphs.join('\n') ?? '',
Object.values(this.session?.roomHistory ?? {}).flat().join('\n'),
].join('\n').toLowerCase();
const mentionsLeaflet = /\b(leaflet|pamphlet|brochure|paper|it|this)\b/.test(normalized);
const contextHasLeaflet = /\b(leaflet|pamphlet|brochure)\b/.test(context);
const mentionsMailbox = /\bmail\s*box|mailbox\b/.test(normalized);
const asksToRead = /\bread\b/.test(normalized) ||
/\bwhat (does|did|do).*say\b/.test(normalized) ||
/\btell me what it says\b/.test(normalized) ||
/\byou did not tell me\b/.test(normalized);
const asksToTake = /\b(take|get|grab|pick up|pluck)\b/.test(normalized);
const asksToOpen = /\bopen\b/.test(normalized);
const asksToLookIn = /\blook (in|inside|into)\b/.test(normalized) || /\binside\b/.test(normalized);
if (mentionsMailbox && asksToOpen && asksToLookIn) {
return ['OPEN MAILBOX', 'LOOK IN MAILBOX'];
}
if (mentionsMailbox && asksToOpen) {
return ['OPEN MAILBOX'];
}
if (asksToRead && (mentionsLeaflet || mentionsMailbox || contextHasLeaflet)) {
if (asksToTake || mentionsMailbox) {
return ['TAKE LEAFLET', 'READ LEAFLET'];
}
return ['READ LEAFLET'];
}
if (asksToTake && (mentionsLeaflet || (mentionsMailbox && contextHasLeaflet))) {
return ['TAKE LEAFLET'];
}
return [];
}
appendRoomHistory(room, text) {
if (!this.session)
return;
const history = this.session.roomHistory[room] ?? [];
history.push(text);
if (history.length > this.historySize) {
history.splice(0, history.length - this.historySize);
}
this.session.roomHistory[room] = history;
}
buildCommonVars() {
const s = this.session;
const notes = s.notes.length > 0
? s.notes.map((n, i) => `${i + 1}. ${n}`).join('\n')
: '(none)';
const virtualInventory = s.virtualInventory.length > 0
? s.virtualInventory.map((n, i) => `${i + 1}. ${n}`).join('\n')
: '(none)';
const recentNarrative = s.recentParagraphs.length > 0
? s.recentParagraphs.join('\n\n---\n\n')
: '(none)';
const rawTranscript = s.rawTranscript.length > 0
? s.rawTranscript.join('\n\n---\n\n')
: '(none)';
const history = (s.roomHistory[s.currentRoom] ?? []).join('\n\n---\n\n');
return {
characterDescription: s.characterDescription,
notes,
virtualInventory,
recentNarrative,
rawTranscript,
roomHistory: history || '(no prior visits)',
currentRoom: s.currentRoom,
narratorState: [
`Turn count: ${s.turnCount}`,
`Time of day: ${s.timeOfDay}`,
`Outside weather drift: ${s.weather}`,
].join('\n'),
};
}
buildTurnResult(text) {
const alive = this.zmachine.isAlive();
if (!alive && this.session)
this.session.running = false;
const paragraphs = (0, turn_result_1.textToParagraphs)(text);
return {
turnId: this.nextTurnId++,
paragraphs,
choices: [],
inputMode: alive ? 'text' : 'end',
gameState: { statusLine: this.session?.currentRoom },
};
}
}
exports.ZcodeLlmEngine = ZcodeLlmEngine;
ZcodeLlmEngine.DEPRECATED_MODEL_REPLACEMENTS = {
'anthropic/claude-3-opus-20240229': 'openai/gpt-5.5',
'openai/gpt-5.4-mini': 'openai/gpt-5.5',
};
//# sourceMappingURL=zcode-llm-engine.js.map
File diff suppressed because one or more lines are too long
-4
View File
@@ -1,4 +0,0 @@
/**
* Main entry point for the AI Interactive Fiction application
*/
export {};
-112
View File
@@ -1,112 +0,0 @@
"use strict";
/**
* Main entry point for the AI Interactive Fiction application
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = __importStar(require("dotenv"));
const game_runner_1 = require("./cli/game-runner");
// YAML CLI entry point. The web default is selected by scripts/run-engine.js.
const server_yaml_1 = require("./server-yaml");
const game_config_1 = require("./config/game-config");
// Load environment variables
console.log('Loading environment variables...');
try {
const result = dotenv.config();
if (result.error) {
console.error('Error loading .env file:', result.error);
}
else {
console.log('Environment variables loaded successfully');
}
}
catch (error) {
console.error('Exception when loading env:', error);
}
async function main() {
try {
console.log('=== AI Interactive Fiction ===');
console.log('A modern take on classic text adventures with LLM-powered interactions');
console.log('');
// Get the world file path from the YAML engine config, with environment override.
const engineConfig = (0, game_config_1.loadGameConfig)(process.env.YAML_CONFIG_FILE || './config/engines/yaml.json', 'yaml');
const worldFile = (0, game_config_1.projectPath)(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
console.log(`Using world file: ${worldFile}`);
console.log(`OpenRouter API Key: ${process.env.OPENROUTER_API_KEY ? 'Found' : 'Missing'}`);
console.log(`OpenRouter Model: ${process.env.OPENROUTER_MODEL || 'Not specified'}`);
// Check if we should run in CLI mode
const args = process.argv.slice(2);
const cliMode = args.includes('--cli') || args.includes('-c');
if (cliMode) {
// CLI mode
console.log('Starting in CLI mode...');
// Create game runner and initialize
console.log('Creating game runner...');
const gameRunner = new game_runner_1.GameRunner();
console.log('Initializing game...');
await gameRunner.initialize(worldFile);
// Start the CLI game
console.log('Starting CLI game...');
await gameRunner.start();
}
else {
// Web interface mode - explicitly start the server with port fallback
console.log('Starting in web interface mode...');
// Get port configuration
const DEFAULT_PORT = 3000;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300;
// Start the web server with port fallback
console.log('Starting web server...');
await (0, server_yaml_1.startServer)(PORT, PORT_RANGE);
}
}
catch (error) {
console.error('Failed to start:', error);
if (error instanceof Error) {
console.error('Error name:', error.name);
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
}
process.exit(1);
}
}
// Start the application
console.log('Starting application...');
main().catch(error => {
console.error('Unhandled error in main:', error);
process.exit(1);
});
//# sourceMappingURL=index.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGH,+CAAiC;AACjC,mDAA+C;AAC/C,8EAA8E;AAC9E,+CAA4C;AAC5C,sDAAmE;AAEnE,6BAA6B;AAC7B,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;AAChD,IAAI,CAAC;IACH,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;IAC/B,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC;AAAC,OAAO,KAAK,EAAE,CAAC;IACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;AACtD,CAAC;AAED,KAAK,UAAU,IAAI;IACjB,IAAI,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;QAC9C,OAAO,CAAC,GAAG,CAAC,wEAAwE,CAAC,CAAC;QACtF,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAEhB,kFAAkF;QAClF,MAAM,YAAY,GAAG,IAAA,4BAAc,EACjC,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,4BAA4B,EAC5D,MAAM,CACP,CAAC;QACF,MAAM,SAAS,GAAG,IAAA,yBAAW,EAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,YAAY,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACjG,OAAO,CAAC,GAAG,CAAC,qBAAqB,SAAS,EAAE,CAAC,CAAC;QAC9C,OAAO,CAAC,GAAG,CAAC,uBAAuB,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;QAC3F,OAAO,CAAC,GAAG,CAAC,qBAAqB,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,eAAe,EAAE,CAAC,CAAC;QAEpF,qCAAqC;QACrC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE9D,IAAI,OAAO,EAAE,CAAC;YACZ,WAAW;YACX,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;YAEvC,oCAAoC;YACpC,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;YACvC,MAAM,UAAU,GAAG,IAAI,wBAAU,EAAE,CAAC;YAEpC,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;YACpC,MAAM,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;YAEvC,qBAAqB;YACrB,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;YACpC,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;aAAM,CAAC;YACN,sEAAsE;YACtE,OAAO,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;YAEjD,yBAAyB;YACzB,MAAM,YAAY,GAAG,IAAI,CAAC;YAC1B,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;YAC1E,MAAM,UAAU,GAAG,GAAG,CAAC;YAEvB,0CAA0C;YAC1C,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;YACtC,MAAM,IAAA,yBAAW,EAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;QACzC,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,OAAO,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;YACzC,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;YAC/C,OAAO,CAAC,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,wBAAwB;AACxB,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;AACvC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;IACjD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
-39
View File
@@ -1,39 +0,0 @@
/**
* Interfaces for the game engine
*/
import { WorldModel, GameState } from './world-model';
import { ActionResponse, NarrativeResponse } from './llm';
export interface ActionResult {
success: boolean;
message: string;
stateChanged: boolean;
newState?: GameState;
}
export interface GameEngine {
loadWorld(worldModelPath: string): Promise<void>;
getCurrentState(): GameState;
getWorldModel(): WorldModel;
processAction(action: ActionResponse): ActionResult;
saveGame(filename: string): Promise<void>;
loadGame(filename: string): Promise<void>;
getAvailableActions(): string[];
getVisibleObjects(): string[];
getVisibleCharacters(): string[];
getCurrentRoomDescription(): string;
start(): Promise<string>;
end(): void;
}
export interface GameSession {
engine: GameEngine;
history: {
playerInput: string;
actionResponse: ActionResponse;
actionResult: ActionResult;
narrativeResponse: NarrativeResponse;
}[];
startTime: Date;
lastInteractionTime: Date;
}
export interface ActionHandler {
execute(gameState: GameState, worldModel: WorldModel, action: ActionResponse): ActionResult;
}
-6
View File
@@ -1,6 +0,0 @@
"use strict";
/**
* Interfaces for the game engine
*/
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=engine.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../../src/interfaces/engine.ts"],"names":[],"mappings":";AAAA;;GAEG"}
-46
View File
@@ -1,46 +0,0 @@
/**
* Interfaces for LLM integration
*/
export interface LlmConfig {
apiKey: string;
model: string;
temperature?: number;
maxTokens?: number;
topP?: number;
frequencyPenalty?: number;
presencePenalty?: number;
}
export interface ActionRequest {
playerInput: string;
currentRoom: string;
visibleObjects: string[];
visibleCharacters: string[];
possibleActions: string[];
inventory: string[];
gameContext: string;
}
export interface ActionResponse {
action: string;
object?: string;
target?: string;
parameters?: Record<string, string>;
confidence: number;
}
export interface NarrativeRequest {
action: string;
result: string;
roomDescription: string;
visibleObjects: string[];
visibleCharacters: string[];
previousContext?: string;
tone?: string;
}
export interface NarrativeResponse {
text: string;
suggestions?: string[];
}
export interface LlmProvider {
initialize(config: LlmConfig): Promise<void>;
translateAction(request: ActionRequest): Promise<ActionResponse>;
generateNarrative(request: NarrativeRequest): Promise<NarrativeResponse>;
}
-6
View File
@@ -1,6 +0,0 @@
"use strict";
/**
* Interfaces for LLM integration
*/
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=llm.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../../src/interfaces/llm.ts"],"names":[],"mappings":";AAAA;;GAEG"}
-36
View File
@@ -1,36 +0,0 @@
export type InputMode = 'text' | 'choice' | 'end';
export interface StoryTag {
key: string;
value?: string;
param?: string;
}
export interface ParagraphResult {
text: string;
tags: StoryTag[];
}
export interface ChoiceResult {
index: number;
text: string;
tags: StoryTag[];
category?: string;
letter?: string;
}
export interface TurnResult {
turnId: number;
paragraphs: ParagraphResult[];
choices: ChoiceResult[];
inputMode: InputMode;
globalTags?: StoryTag[];
gameState?: {
currentRoomId?: string;
score?: number;
moves?: number;
statusLine?: string;
endState?: {
type: 'intended' | 'error';
message?: string;
};
};
suggestions?: string[];
}
export declare function textToParagraphs(text: string, tags?: StoryTag[]): ParagraphResult[];
-36
View File
@@ -1,36 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.textToParagraphs = textToParagraphs;
/**
* Shared engine-to-client turn protocol.
*/
const tag_parser_1 = require("../utils/tag-parser");
function textToParagraphs(text, tags = []) {
return String(text || '')
.replace(/\r\n?/g, '\n')
.split(/\n{2,}/)
.map((paragraph) => paragraph.trim())
.filter(Boolean)
.map((paragraph) => {
const lines = paragraph.split('\n');
const paragraphTags = [...tags];
const textLines = [];
let tagPrefixOpen = true;
for (const line of lines) {
const trimmed = line.trim();
const maybeTag = tagPrefixOpen && trimmed.startsWith('#') ? (0, tag_parser_1.parseTag)(trimmed) : null;
if (maybeTag) {
paragraphTags.push(maybeTag);
}
else {
tagPrefixOpen = false;
textLines.push(line);
}
}
return {
text: textLines.join('\n').trim(),
tags: paragraphTags,
};
});
}
//# sourceMappingURL=turn-result.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"turn-result.js","sourceRoot":"","sources":["../../src/interfaces/turn-result.ts"],"names":[],"mappings":";;AA6CA,4CA6BC;AA1ED;;GAEG;AACH,oDAA+C;AA0C/C,SAAgB,gBAAgB,CAAC,IAAY,EAAE,OAAmB,EAAE;IAClE,OAAO,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;SACtB,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC;SACvB,KAAK,CAAC,QAAQ,CAAC;SACf,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;SACpC,MAAM,CAAC,OAAO,CAAC;SACf,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE;QACjB,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,aAAa,GAAe,CAAC,GAAG,IAAI,CAAC,CAAC;QAC5C,MAAM,SAAS,GAAa,EAAE,CAAC;QAC/B,IAAI,aAAa,GAAG,IAAI,CAAC;QAEzB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YAC5B,MAAM,QAAQ,GAAG,aAAa,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAQ,EAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAErF,IAAI,QAAQ,EAAE,CAAC;gBACb,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,aAAa,GAAG,KAAK,CAAC;gBACtB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;QAED,OAAO;YACL,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE;YACjC,IAAI,EAAE,aAAa;SACpB,CAAC;IACJ,CAAC,CAAC,CAAC;AACP,CAAC"}
-61
View File
@@ -1,61 +0,0 @@
/**
* Core interfaces for the interactive fiction world model
*/
export interface Room {
id: string;
name: string;
description: string;
exits: Exit[];
objects: string[];
characters: string[];
}
export interface Exit {
direction: string;
targetRoomId: string;
description?: string;
isLocked?: boolean;
keyId?: string;
}
export interface GameObject {
id: string;
name: string;
description: string;
traits: string[];
states: Record<string, boolean>;
containedObjects?: string[];
allowedActions: string[];
}
export interface Character {
id: string;
name: string;
description: string;
dialogue: Record<string, string>;
inventory: string[];
defaultResponse: string;
mood?: string;
}
export interface Action {
name: string;
patterns: string[];
requiresObject?: boolean;
requiresTarget?: boolean;
handler: string;
}
export interface GameState {
currentRoomId: string;
inventory: string[];
visitedRooms: string[];
flags: Record<string, boolean>;
counters: Record<string, number>;
}
export interface WorldModel {
title: string;
author: string;
version: string;
introduction: string;
rooms: Record<string, Room>;
objects: Record<string, GameObject>;
characters: Record<string, Character>;
actions: Record<string, Action>;
initialState: GameState;
}
-6
View File
@@ -1,6 +0,0 @@
"use strict";
/**
* Core interfaces for the interactive fiction world model
*/
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=world-model.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"world-model.js","sourceRoot":"","sources":["../../src/interfaces/world-model.ts"],"names":[],"mappings":";AAAA;;GAEG"}
-36
View File
@@ -1,36 +0,0 @@
/**
* OpenRouter LLM Provider
* Handles communication with OpenRouter API for LLM interactions
*/
import { LlmProvider, LlmConfig, ActionRequest, ActionResponse, NarrativeRequest, NarrativeResponse } from '../interfaces/llm';
export declare class OpenRouterProvider implements LlmProvider {
private apiKey;
private model;
private client;
private temperature;
private maxTokens;
/**
* Initialize the OpenRouter provider with configuration
*/
initialize(config: LlmConfig): Promise<void>;
/**
* Translate player input into a structured action for the game engine
*/
translateAction(request: ActionRequest): Promise<ActionResponse>;
/**
* Generate narrative prose based on game events
*/
generateNarrative(request: NarrativeRequest): Promise<NarrativeResponse>;
/**
* Build the system and user prompts for action translation
*/
private buildActionPrompt;
/**
* Build the system and user prompts for narrative generation
*/
private buildNarrativePrompt;
/**
* Validate and normalize the action response
*/
private validateActionResponse;
}
-192
View File
@@ -1,192 +0,0 @@
"use strict";
/**
* OpenRouter LLM Provider
* Handles communication with OpenRouter API for LLM interactions
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRouterProvider = void 0;
const axios_1 = __importDefault(require("axios"));
class OpenRouterProvider {
constructor() {
this.apiKey = '';
this.model = '';
this.temperature = 0.7;
this.maxTokens = 800;
}
/**
* Initialize the OpenRouter provider with configuration
*/
async initialize(config) {
this.apiKey = config.apiKey;
this.model = config.model;
this.temperature = config.temperature ?? 0.7;
this.maxTokens = config.maxTokens ?? 800;
this.client = axios_1.default.create({
baseURL: 'https://openrouter.ai/api/v1',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
});
}
/**
* Translate player input into a structured action for the game engine
*/
async translateAction(request) {
try {
const prompt = this.buildActionPrompt(request);
const response = await this.client.post('/chat/completions', {
model: this.model,
messages: [
{
role: 'system',
content: prompt.system
},
{
role: 'user',
content: prompt.user
}
],
temperature: 0.2, // Lower temperature for more deterministic outputs
max_tokens: 150,
response_format: { type: 'json_object' }
});
const content = response.data.choices[0].message.content;
const parsedResponse = JSON.parse(content);
return this.validateActionResponse(parsedResponse);
}
catch (error) {
console.error('Error translating action:', error);
// Fallback to a simple "look" action when errors occur
return {
action: 'look',
confidence: 0.5
};
}
}
/**
* Generate narrative prose based on game events
*/
async generateNarrative(request) {
try {
const prompt = this.buildNarrativePrompt(request);
const response = await this.client.post('/chat/completions', {
model: this.model,
messages: [
{
role: 'system',
content: prompt.system
},
{
role: 'user',
content: prompt.user
}
],
temperature: this.temperature,
max_tokens: this.maxTokens
});
const content = response.data.choices[0].message.content;
// Check if response is JSON format or plain text
try {
const parsedResponse = JSON.parse(content);
return {
text: parsedResponse.text,
suggestions: parsedResponse.suggestions || []
};
}
catch {
// Plain text response, just use the content directly
return {
text: content
};
}
}
catch (error) {
console.error('Error generating narrative:', error);
return {
text: `Something happened, but the narrator is at a loss for words. (Error: ${error instanceof Error ? error.message : String(error)})`
};
}
}
/**
* Build the system and user prompts for action translation
*/
buildActionPrompt(request) {
const systemPrompt = `You are an AI assistant that translates natural language input into structured action commands for an interactive fiction game.
Your task is to convert player input into a JSON object representing an action that can be understood by the game engine.
The player is currently in the "${request.currentRoom}" room.
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}
Inventory: ${request.inventory.join(', ')}
Available actions: ${request.possibleActions.join(', ')}
Game context: ${request.gameContext}
Respond ONLY with a JSON object that follows this structure:
{
"action": "string", // Name of the action (e.g., "take", "examine", "go", "talk", etc.)
"object": "string", // Optional: Primary object of the action
"target": "string", // Optional: Secondary object/target of the action
"parameters": {}, // Optional: Additional parameters as key-value pairs
"confidence": number // How confident you are in this interpretation (0.0-1.0)
}
Choose the action from the list of available actions. If the player's input is ambiguous or doesn't map well to an available action, choose the closest match and set a lower confidence score.`;
const userPrompt = request.playerInput;
return {
system: systemPrompt,
user: userPrompt
};
}
/**
* Build the system and user prompts for narrative generation
*/
buildNarrativePrompt(request) {
const tone = request.tone || 'descriptive';
const systemPrompt = `You are an AI assistant that generates engaging narrative prose for an interactive fiction game.
Your task is to describe what happens when a player performs an action in the game world.
Craft a vivid, ${tone} description that tells the player what happened as a result of their action. Make your prose engaging and atmospheric.
Current room description: "${request.roomDescription}"
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}
${request.previousContext ? `Previous context: ${request.previousContext}` : ''}
Respond with engaging prose that describes the outcome of the player's action.
You can optionally include 1-3 subtle hints about interesting things to try next.`;
const userPrompt = `The player has performed this action: "${request.action}".
The result of the action is: "${request.result}".
Please describe what happens in an engaging, narrative way.`;
return {
system: systemPrompt,
user: userPrompt
};
}
/**
* Validate and normalize the action response
*/
validateActionResponse(response) {
const validatedResponse = {
action: typeof response.action === 'string' ? response.action : 'look',
confidence: typeof response.confidence === 'number' ? response.confidence : 0.5
};
if (typeof response.object === 'string') {
validatedResponse.object = response.object;
}
if (typeof response.target === 'string') {
validatedResponse.target = response.target;
}
if (response.parameters && typeof response.parameters === 'object') {
validatedResponse.parameters = response.parameters;
}
return validatedResponse;
}
}
exports.OpenRouterProvider = OpenRouterProvider;
//# sourceMappingURL=openrouter-provider.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"openrouter-provider.js","sourceRoot":"","sources":["../../src/llm/openrouter-provider.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,kDAA6C;AAU7C,MAAa,kBAAkB;IAA/B;QACU,WAAM,GAAW,EAAE,CAAC;QACpB,UAAK,GAAW,EAAE,CAAC;QAEnB,gBAAW,GAAW,GAAG,CAAC;QAC1B,cAAS,GAAW,GAAG,CAAC;IA+LlC,CAAC;IA7LC;;OAEG;IACI,KAAK,CAAC,UAAU,CAAC,MAAiB;QACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,GAAG,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,GAAG,CAAC;QAEzC,IAAI,CAAC,MAAM,GAAG,eAAK,CAAC,MAAM,CAAC;YACzB,OAAO,EAAE,8BAA8B;YACvC,OAAO,EAAE;gBACP,eAAe,EAAE,UAAU,IAAI,CAAC,MAAM,EAAE;gBACxC,cAAc,EAAE,kBAAkB;aACnC;SACF,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,eAAe,CAAC,OAAsB;QACjD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;YAE/C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mBAAmB,EAAE;gBAC3D,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,MAAM,CAAC,MAAM;qBACvB;oBACD;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE,MAAM,CAAC,IAAI;qBACrB;iBACF;gBACD,WAAW,EAAE,GAAG,EAAE,mDAAmD;gBACrE,UAAU,EAAE,GAAG;gBACf,eAAe,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;aACzC,CAAC,CAAC;YAEH,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;YACzD,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAE3C,OAAO,IAAI,CAAC,sBAAsB,CAAC,cAAc,CAAC,CAAC;QACrD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;YAClD,uDAAuD;YACvD,OAAO;gBACL,MAAM,EAAE,MAAM;gBACd,UAAU,EAAE,GAAG;aAChB,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,iBAAiB,CAAC,OAAyB;QACtD,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;YAElD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mBAAmB,EAAE;gBAC3D,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,QAAQ;wBACd,OAAO,EAAE,MAAM,CAAC,MAAM;qBACvB;oBACD;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE,MAAM,CAAC,IAAI;qBACrB;iBACF;gBACD,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,UAAU,EAAE,IAAI,CAAC,SAAS;aAC3B,CAAC,CAAC;YAEH,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;YAEzD,iDAAiD;YACjD,IAAI,CAAC;gBACH,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBAC3C,OAAO;oBACL,IAAI,EAAE,cAAc,CAAC,IAAI;oBACzB,WAAW,EAAE,cAAc,CAAC,WAAW,IAAI,EAAE;iBAC9C,CAAC;YACJ,CAAC;YAAC,MAAM,CAAC;gBACP,qDAAqD;gBACrD,OAAO;oBACL,IAAI,EAAE,OAAO;iBACd,CAAC;YACJ,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;YACpD,OAAO;gBACL,IAAI,EAAE,wEAAwE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG;aACxI,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;OAEG;IACK,iBAAiB,CAAC,OAAsB;QAC9C,MAAM,YAAY,GAAG;;;kCAGS,OAAO,CAAC,WAAW;mBAClC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC;sBAC9B,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC;aAC7C,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;qBACpB,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC;;gBAEvC,OAAO,CAAC,WAAW;;;;;;;;;;;gMAW6J,CAAC;QAE7L,MAAM,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;QAEvC,OAAO;YACL,MAAM,EAAE,YAAY;YACpB,IAAI,EAAE,UAAU;SACjB,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,oBAAoB,CAAC,OAAyB;QACpD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,aAAa,CAAC;QAE3C,MAAM,YAAY,GAAG;;;iBAGR,IAAI;;6BAEQ,OAAO,CAAC,eAAe;mBACjC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC;sBAC9B,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC;;EAExD,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,qBAAqB,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE;;;kFAGG,CAAC;QAE/E,MAAM,UAAU,GAAG,0CAA0C,OAAO,CAAC,MAAM;gCAC/C,OAAO,CAAC,MAAM;4DACc,CAAC;QAEzD,OAAO;YACL,MAAM,EAAE,YAAY;YACpB,IAAI,EAAE,UAAU;SACjB,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,sBAAsB,CAAC,QAAiC;QAC9D,MAAM,iBAAiB,GAAmB;YACxC,MAAM,EAAE,OAAO,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM;YACtE,UAAU,EAAE,OAAO,QAAQ,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG;SAChF,CAAC;QAEF,IAAI,OAAO,QAAQ,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACxC,iBAAiB,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;QAC7C,CAAC;QAED,IAAI,OAAO,QAAQ,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACxC,iBAAiB,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;QAC7C,CAAC;QAED,IAAI,QAAQ,CAAC,UAAU,IAAI,OAAO,QAAQ,CAAC,UAAU,KAAK,QAAQ,EAAE,CAAC;YACnE,iBAAiB,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAoC,CAAC;QAC/E,CAAC;QAED,OAAO,iBAAiB,CAAC;IAC3B,CAAC;CACF;AApMD,gDAoMC"}
-13
View File
@@ -1,13 +0,0 @@
/**
* Ink Engine Server
*
* Serves the shared client UI and runs a compiled Ink JSON story through the
* unified TurnResult socket protocol.
*/
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
declare const app: import("express-serve-static-core").Express;
declare const server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
declare const io: SocketIOServer<import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, any>;
export declare function startServer(initialPort: number, range: number): Promise<void>;
export { app, server, io };
-293
View File
@@ -1,293 +0,0 @@
"use strict";
/**
* Ink Engine Server
*
* Serves the shared client UI and runs a compiled Ink JSON story through the
* unified TurnResult socket protocol.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.io = exports.server = exports.app = void 0;
exports.startServer = startServer;
const path_1 = __importDefault(require("path"));
const http_1 = __importDefault(require("http"));
const express_1 = __importDefault(require("express"));
const socket_io_1 = require("socket.io");
const dotenv = __importStar(require("dotenv"));
const fs_1 = require("fs");
const ink_engine_1 = require("./engine/ink-engine");
const game_config_1 = require("./config/game-config");
dotenv.config();
const app = (0, express_1.default)();
exports.app = app;
const server = http_1.default.createServer(app);
exports.server = server;
const io = new socket_io_1.Server(server);
exports.io = io;
const DEFAULT_PORT = 3003;
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : DEFAULT_PORT;
const PORT_RANGE = 300;
const engineConfig = (0, game_config_1.loadGameConfig)(process.env.INK_CONFIG_FILE || './config/engines/ink.json', 'ink');
app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
},
}));
app.get('/api/game-config', (_req, res) => {
res.json((0, game_config_1.clientGameConfig)(engineConfig));
});
const sessions = new Map();
const saveSlots = new Map();
function normalizeSaveSlot(slot) {
const n = Number(slot);
return Number.isInteger(n) && n > 0 ? n : 1;
}
function getStoryPath() {
return (0, game_config_1.projectPath)(process.env.INK_STORY_FILE ||
engineConfig.paths.inkCompiled ||
engineConfig.paths.mainGameFile);
}
function getSourcePath() {
return (0, game_config_1.projectPath)(process.env.INK_SOURCE_FILE || engineConfig.paths.inkSource || '');
}
function compileConfiguredStory() {
const sourcePath = getSourcePath();
const outputPath = getStoryPath();
const result = (0, ink_engine_1.compileInkSource)(sourcePath, outputPath);
console.log(`[ink] Compiled ${result.sourcePath} -> ${result.outputPath}` +
(result.warningCount > 0 ? ` (${result.warningCount} warnings)` : ''));
}
function getSlots(socketId) {
let slots = saveSlots.get(socketId);
if (!slots) {
slots = new Map();
saveSlots.set(socketId, slots);
}
return slots;
}
function getOrCreateEngine(socketId) {
let engine = sessions.get(socketId);
if (!engine) {
engine = new ink_engine_1.InkEngine(getStoryPath());
sessions.set(socketId, engine);
}
return engine;
}
async function handleGameApi(socket, method, args) {
const slots = getSlots(socket.id);
switch (method) {
case 'newGame':
case 'newGame()': {
const engine = new ink_engine_1.InkEngine(getStoryPath());
sessions.set(socket.id, engine);
socket.emit('narrativeResponse', engine.newGame());
return { success: true, result: true, running: true, canLoad: slots.size > 0 };
}
case 'chooseChoice':
case 'chooseChoice()': {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
return { success: false, error: 'game_not_running', result: false };
}
const choiceIndex = Number(args[0]);
if (!Number.isInteger(choiceIndex)) {
return { success: false, error: 'invalid_choice', result: false };
}
socket.emit('narrativeResponse', engine.chooseChoice(choiceIndex));
return { success: true, result: true };
}
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
const browserSave = typeof args[1] === 'string' ? args[1] : null;
if (!browserSave && !slots.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
const engine = getOrCreateEngine(socket.id);
socket.emit('narrativeResponse', engine.loadGame(browserSave || slots.get(slot)));
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'resumeGame':
case 'resumeGame()': {
const browserSave = typeof args[0] === 'string' ? args[0] : null;
if (!browserSave) {
return { success: false, error: 'missing_state', result: false };
}
const engine = new ink_engine_1.InkEngine(getStoryPath());
engine.resumeGame(browserSave);
sessions.set(socket.id, engine);
return { success: true, result: true, running: engine.isRunning() };
}
case 'exportGameState':
case 'exportGameState()': {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
return { success: false, error: 'game_not_running', result: false };
}
return { success: true, result: true, savedState: engine.saveGame() };
}
case 'saveGame':
case 'saveGame()': {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
const savedState = engine.saveGame();
slots.set(slot, savedState);
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot, savedState };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: slots.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return { success: true, result: Array.from(slots.keys()).sort((a, b) => a - b) };
case 'isGameRunning':
case 'isGameRunning()':
return { success: true, result: sessions.get(socket.id)?.isRunning() ?? false };
default:
return { success: false, error: `unknown_method:${method}` };
}
}
io.on('connection', (socket) => {
console.log(`[ink] Client connected: ${socket.id}`);
socket.emit('gameConfig', (0, game_config_1.clientGameConfig)(engineConfig));
socket.on('gameApi', async (request, respond) => {
try {
const result = await handleGameApi(socket, String(request?.method ?? ''), Array.isArray(request?.args) ? request.args : []);
if (typeof respond === 'function')
respond(result);
}
catch (error) {
console.error('[ink] gameApi error:', error);
if (typeof respond === 'function') {
respond({
success: false,
error: error instanceof Error ? error.message : String(error),
});
}
}
});
socket.on('disconnect', () => {
console.log(`[ink] Client disconnected: ${socket.id}`);
sessions.delete(socket.id);
saveSlots.delete(socket.id);
});
});
function ensureDirectories() {
const dirs = [
path_1.default.join(__dirname, '../public'),
path_1.default.join(__dirname, '../public/js'),
path_1.default.join(__dirname, '../public/css'),
path_1.default.join(__dirname, '../public/images'),
path_1.default.join(__dirname, '../public/music'),
path_1.default.join(__dirname, '../public/sounds'),
path_1.default.join(__dirname, '../public/fonts'),
];
for (const dir of dirs) {
if (!(0, fs_1.existsSync)(dir))
(0, fs_1.mkdirSync)(dir, { recursive: true });
}
(0, game_config_1.ensureConfiguredAssetDirectories)(engineConfig);
}
function ensureKokoroJs() {
const source = path_1.default.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const destination = path_1.default.join(__dirname, '../public/js/kokoro-js.js');
if ((0, fs_1.existsSync)(source) && !(0, fs_1.existsSync)(destination)) {
(0, fs_1.copyFileSync)(source, destination);
}
}
async function startServer(initialPort, range) {
ensureDirectories();
try {
ensureKokoroJs();
}
catch { /* optional */ }
compileConfiguredStory();
if (!(0, fs_1.existsSync)(getStoryPath())) {
console.error(`[ink] Story file missing: ${getStoryPath()}`);
console.error('[ink] Set INK_SOURCE_FILE or configure paths.inkSource in config/engines/ink.json.');
}
let port = initialPort;
while (port < initialPort + range) {
try {
await new Promise((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`[ink] Ink server running on http://localhost:${port}`);
resolve();
});
server.once('error', (error) => {
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${port} unavailable (${error.code}), trying ${port + 1}...`);
server.close();
port++;
reject();
}
else {
reject(error);
}
});
server.listen(port);
});
return;
}
catch {
if (port >= initialPort + range - 1) {
throw new Error(`Failed to start server on ports ${initialPort} to ${initialPort + range - 1}`);
}
}
}
}
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch((error) => {
console.error('[ink] Failed to start:', error);
process.exit(1);
});
}
//# sourceMappingURL=server-ink.js.map
-1
View File
File diff suppressed because one or more lines are too long
-11
View File
@@ -1,11 +0,0 @@
/**
* AI Interactive Fiction - Web Server
* Serves the web UI and handles WebSocket communication
*/
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
declare const app: import("express-serve-static-core").Express;
declare const server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
declare const io: SocketIOServer<import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, any>;
export declare function startServer(initialPort: number, range: number): Promise<void>;
export { app, server, io };
-308
View File
@@ -1,308 +0,0 @@
"use strict";
/**
* AI Interactive Fiction - Web Server
* Serves the web UI and handles WebSocket communication
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.io = exports.server = exports.app = void 0;
exports.startServer = startServer;
const path_1 = __importDefault(require("path"));
const express_1 = __importDefault(require("express"));
const http_1 = __importDefault(require("http"));
const socket_io_1 = require("socket.io");
const dotenv = __importStar(require("dotenv"));
const game_runner_1 = require("./cli/game-runner");
const fs_1 = require("fs");
const turn_result_1 = require("./interfaces/turn-result");
const game_config_1 = require("./config/game-config");
// Load environment variables
dotenv.config();
// Create Express application
const app = (0, express_1.default)();
exports.app = app;
const server = http_1.default.createServer(app);
exports.server = server;
const io = new socket_io_1.Server(server);
exports.io = io;
// Get port from environment variables or use default
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
const engineConfig = (0, game_config_1.loadGameConfig)(process.env.YAML_CONFIG_FILE || './config/engines/yaml.json', 'yaml');
// Serve static files from the public directory. During local development the
// browser must not keep stale ES modules, otherwise UI fixes appear to do
// nothing until a hard cache clear.
app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
app.get('/api/game-config', (_req, res) => {
res.json((0, game_config_1.clientGameConfig)(engineConfig));
});
// Set up game sessions
const gameSessions = new Map();
const nextTurnIds = new Map();
function nextTurnId(socketId) {
const current = nextTurnIds.get(socketId) || 1;
nextTurnIds.set(socketId, current + 1);
return current;
}
function createTextTurn(socketId, text, gameState = {}, suggestions) {
const paragraphs = (0, turn_result_1.textToParagraphs)(text);
return {
turnId: nextTurnId(socketId),
paragraphs,
choices: [],
inputMode: 'text',
gameState,
suggestions,
};
}
function normalizeSaveSlot(slot) {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
}
async function startDemoGameForSocket(socket) {
nextTurnIds.set(socket.id, 1);
const gameRunner = new game_runner_1.GameRunner();
const worldFile = (0, game_config_1.projectPath)(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
await gameRunner.initialize(worldFile);
gameSessions.set(socket.id, gameRunner);
const gameState = gameRunner.getGameState();
const paragraphs = [
...(0, turn_result_1.textToParagraphs)(gameState.world.introduction),
...(0, turn_result_1.textToParagraphs)(gameRunner.getCurrentRoomDescription()),
];
socket.emit('narrativeResponse', {
turnId: nextTurnId(socket.id),
paragraphs,
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: gameState.currentRoomId,
},
});
return gameRunner;
}
async function handleGameApi(socket, method, args = []) {
const saveGames = socket.data.saveGames || new Map();
socket.data.saveGames = saveGames;
switch (method) {
case 'newGame':
case 'newGame()':
await startDemoGameForSocket(socket);
return { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!saveGames.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
await startDemoGameForSocket(socket);
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'saveGame':
case 'saveGame()': {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
saveGames.set(slot, gameRunner.getGameState());
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: saveGames.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return { success: true, result: Array.from(saveGames.keys()).sort((a, b) => a - b) };
case 'isGameRunning':
case 'isGameRunning()':
return { success: true, result: gameSessions.has(socket.id) };
default:
return { success: false, error: `unknown_method:${method}` };
}
}
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
socket.emit('gameConfig', (0, game_config_1.clientGameConfig)(engineConfig));
socket.data.saveGames = new Map();
socket.on('gameApi', async (request, respond) => {
try {
const response = await handleGameApi(socket, String(request?.method || ''), Array.isArray(request?.args) ? request.args : []);
if (typeof respond === 'function') {
respond(response);
}
}
catch (error) {
console.error('Game API error:', error);
if (typeof respond === 'function') {
respond({ success: false, error: error instanceof Error ? error.message : String(error) });
}
}
});
// Process player command
socket.on('playerCommand', async (data) => {
try {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
socket.emit('error', { message: 'Game session not found. Please start a new game.' });
return;
}
const command = String(data?.command || '').trim();
// During typography and animation work, mirror the command back through
// the real socket path so the UI pipeline can be tested end to end.
socket.emit('narrativeResponse', createTextTurn(socket.id, command, {
currentRoomId: gameRunner.getGameState().currentRoomId
}, gameRunner.getSuggestions()));
}
catch (error) {
console.error('Error processing command:', error);
socket.emit('error', { message: 'Failed to process command. Please try again.' });
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
// Clean up game session
if (gameSessions.has(socket.id)) {
gameSessions.delete(socket.id);
}
nextTurnIds.delete(socket.id);
});
});
// Ensure required asset folders exist
function ensureDirectories() {
const dirs = [
path_1.default.join(__dirname, '../public'),
path_1.default.join(__dirname, '../public/js'),
path_1.default.join(__dirname, '../public/css'),
path_1.default.join(__dirname, '../public/images'),
path_1.default.join(__dirname, '../public/music'),
path_1.default.join(__dirname, '../public/sounds'),
path_1.default.join(__dirname, '../public/fonts')
];
for (const dir of dirs) {
if (!(0, fs_1.existsSync)(dir)) {
(0, fs_1.mkdirSync)(dir, { recursive: true });
}
}
(0, game_config_1.ensureConfiguredAssetDirectories)(engineConfig);
}
// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
const source = path_1.default.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const destination = path_1.default.join(__dirname, '../public/js/kokoro-js.js');
if ((0, fs_1.existsSync)(source) && !(0, fs_1.existsSync)(destination)) {
(0, fs_1.copyFileSync)(source, destination);
console.log(`Copied kokoro-js from ${source} to ${destination}`);
}
}
// Start the server with port fallback
async function startServer(initialPort, range) {
let currentPort = initialPort;
const maxPort = initialPort + range;
// Try ports in the specified range
while (currentPort < maxPort) {
try {
// Ensure directories exist
ensureDirectories();
// Ensure kokoro-js is copied
try {
ensureKokoroJs();
}
catch (error) {
console.error('Error copying kokoro-js:', error);
}
// Try to start the server on the current port
await new Promise((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`AI Interactive Fiction web server running on http://localhost:${currentPort}`);
resolve();
});
server.once('error', (error) => {
// If port is in use, try next port
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
server.close();
currentPort++;
reject();
}
else {
// For other errors, log and reject
console.error('Server error:', error);
reject(error);
}
});
server.listen(currentPort);
});
// If we reach here, server started successfully
return;
}
catch (error) {
// If we reach the max port and still fail, throw an error
if (currentPort >= maxPort - 1) {
throw new Error(`Failed to start server on ports ${initialPort} to ${maxPort - 1}`);
}
// Otherwise try the next port
// The loop continues as the rejection above increments currentPort
}
}
}
// Start the server when this module is run directly
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch(error => {
console.error('Failed to start server:', error);
process.exit(1);
});
}
//# sourceMappingURL=server-yaml.js.map
-1
View File
File diff suppressed because one or more lines are too long
-16
View File
@@ -1,16 +0,0 @@
/**
* Z-code LLM Server
*
* Starts an Express + Socket.IO server that runs Zork I through the
* ZcodeLlmEngine and serves the same shared client UI as the YAML engine.
*
* Usage:
* npm run dev:zcode (development, with file watching)
* npm run start:zcode (production, from compiled dist/)
*
* Environment variables:
* PORT HTTP port (default: 3002)
* ZCODE_STORY_FILE path to the story file (default: ./data/z-code/zork1.bin)
* OPENROUTER_API_KEY, OPENROUTER_MODEL required
*/
export {};
-353
View File
@@ -1,353 +0,0 @@
"use strict";
/**
* Z-code LLM Server
*
* Starts an Express + Socket.IO server that runs Zork I through the
* ZcodeLlmEngine and serves the same shared client UI as the YAML engine.
*
* Usage:
* npm run dev:zcode (development, with file watching)
* npm run start:zcode (production, from compiled dist/)
*
* Environment variables:
* PORT HTTP port (default: 3002)
* ZCODE_STORY_FILE path to the story file (default: ./data/z-code/zork1.bin)
* OPENROUTER_API_KEY, OPENROUTER_MODEL required
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const http_1 = __importDefault(require("http"));
const express_1 = __importDefault(require("express"));
const socket_io_1 = require("socket.io");
const dotenv = __importStar(require("dotenv"));
const fs_1 = require("fs");
const zcode_llm_engine_1 = require("./engine/zcode-llm-engine");
const game_config_1 = require("./config/game-config");
dotenv.config();
const app = (0, express_1.default)();
const server = http_1.default.createServer(app);
const io = new socket_io_1.Server(server);
const DEFAULT_PORT = 3002;
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : DEFAULT_PORT;
const PORT_RANGE = 300;
const DEBUG_ENABLED = /^(1|true|yes|on)$/i.test(process.env.ZCODE_DEBUG ?? '');
const engineConfig = (0, game_config_1.loadGameConfig)(process.env.ZCODE_CONFIG_FILE || './config/engines/zcode.json', 'zcode');
function debugLog(message, details) {
if (!DEBUG_ENABLED)
return;
if (typeof details === 'undefined') {
console.log(`[zcode:debug] ${message}`);
return;
}
console.log(`[zcode:debug] ${message}`, details);
}
// Serve the same shared client UI
app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
},
}));
app.get('/api/game-config', (_req, res) => {
res.json((0, game_config_1.clientGameConfig)(engineConfig));
});
// One engine instance per connected socket
const sessions = new Map();
// Save-game slot maps: socketId → Map<slotNumber, serialisedJson>
const saveSlots = new Map();
function toClientTurn(turn) {
return {
...turn,
gameState: {
...turn.gameState,
currentRoomId: turn.gameState?.statusLine,
statusLine: turn.gameState?.statusLine,
},
};
}
function normalizeSaveSlot(slot) {
const n = Number(slot);
return Number.isInteger(n) && n > 0 ? n : 1;
}
function getOrCreateEngine(socketId) {
let engine = sessions.get(socketId);
if (!engine) {
engine = new zcode_llm_engine_1.ZcodeLlmEngine({
storyPath: (0, game_config_1.projectPath)(process.env.ZCODE_STORY_FILE || engineConfig.paths.mainGameFile),
promptDir: (0, game_config_1.projectPath)(engineConfig.paths.promptDir || 'data/zcode-prompts'),
});
sessions.set(socketId, engine);
}
return engine;
}
function getSlots(socketId) {
let slots = saveSlots.get(socketId);
if (!slots) {
slots = new Map();
saveSlots.set(socketId, slots);
}
return slots;
}
async function handleGameApi(socket, method, args) {
const slots = getSlots(socket.id);
debugLog(`gameApi request from ${socket.id}: ${method}`, { args });
switch (method) {
case 'newGame':
case 'newGame()': {
const engine = getOrCreateEngine(socket.id);
const turn = await engine.newGame();
socket.emit('narrativeResponse', toClientTurn(turn));
return {
success: true,
result: true,
running: true,
canLoad: slots.size > 0,
};
}
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!slots.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
const engine = getOrCreateEngine(socket.id);
const turn = await engine.loadGame(slots.get(slot));
socket.emit('narrativeResponse', toClientTurn(turn));
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'saveGame':
case 'saveGame()': {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
const savedJson = await engine.saveGame();
slots.set(slot, savedJson);
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: slots.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return {
success: true,
result: Array.from(slots.keys()).sort((a, b) => a - b),
};
case 'isGameRunning':
case 'isGameRunning()':
return {
success: true,
result: sessions.get(socket.id)?.isRunning() ?? false,
};
default:
return { success: false, error: `unknown_method:${method}` };
}
}
function checkRuntimeConfiguration() {
const storyPath = (0, game_config_1.projectPath)(process.env.ZCODE_STORY_FILE ?? engineConfig.paths.mainGameFile);
const promptDir = (0, game_config_1.projectPath)(engineConfig.paths.promptDir || 'data/zcode-prompts');
const promptFiles = [
'character-generation.yml',
'text-rewriter.yml',
'command-translator.yml',
'output-evaluator.yml',
];
const missingPrompts = promptFiles
.map((file) => path_1.default.join(promptDir, file))
.filter((filePath) => !(0, fs_1.existsSync)(filePath));
if (!process.env.OPENROUTER_API_KEY) {
console.error('[zcode] Missing OPENROUTER_API_KEY in environment.');
}
if (!process.env.OPENROUTER_MODEL) {
console.error('[zcode] Missing OPENROUTER_MODEL in environment.');
}
if (!(0, fs_1.existsSync)(storyPath)) {
console.error(`[zcode] Story file missing: ${storyPath}`);
console.error('[zcode] Place zork1.bin in ./data/z-code/ or set ZCODE_STORY_FILE.');
}
if (missingPrompts.length > 0) {
console.error('[zcode] Missing prompt files:');
for (const filePath of missingPrompts) {
console.error(` - ${filePath}`);
}
}
debugLog('runtime configuration', {
storyPath,
promptDir,
debug: DEBUG_ENABLED,
hasApiKey: Boolean(process.env.OPENROUTER_API_KEY),
model: process.env.OPENROUTER_MODEL ?? null,
});
}
io.on('connection', (socket) => {
console.log(`[zcode] Client connected: ${socket.id}`);
socket.emit('gameConfig', (0, game_config_1.clientGameConfig)(engineConfig));
socket.on('gameApi', async (request, respond) => {
try {
const result = await handleGameApi(socket, String(request?.method ?? ''), Array.isArray(request?.args) ? request.args : []);
debugLog(`gameApi response to ${socket.id}`, result);
if (typeof respond === 'function')
respond(result);
}
catch (error) {
console.error('[zcode] gameApi error:', error);
if (typeof respond === 'function') {
respond({
success: false,
error: error instanceof Error ? error.message : String(error),
});
}
}
});
socket.on('playerCommand', async (data) => {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
socket.emit('error', {
message: 'No active game. Start or load a game first.',
});
return;
}
const input = String(data?.command ?? '').trim();
if (!input)
return;
debugLog(`playerCommand from ${socket.id}: ${input}`);
try {
const turn = await engine.processInput(input);
debugLog(`narrativeResponse to ${socket.id}`, {
inputMode: turn.inputMode,
paragraphs: turn.paragraphs.length,
statusLine: turn.gameState?.statusLine,
});
socket.emit('narrativeResponse', toClientTurn(turn));
}
catch (error) {
console.error('[zcode] playerCommand error:', error);
socket.emit('error', {
message: error instanceof Error ? error.message : 'An error occurred.',
});
}
});
socket.on('disconnect', () => {
console.log(`[zcode] Client disconnected: ${socket.id}`);
sessions.delete(socket.id);
saveSlots.delete(socket.id);
});
});
// ---------------------------------------------------------------------------
// Startup helpers
// ---------------------------------------------------------------------------
function ensureDirectories() {
const dirs = [
path_1.default.join(__dirname, '../public'),
path_1.default.join(__dirname, '../public/js'),
path_1.default.join(__dirname, '../public/css'),
path_1.default.join(__dirname, '../public/images'),
path_1.default.join(__dirname, '../public/music'),
path_1.default.join(__dirname, '../public/sounds'),
path_1.default.join(__dirname, '../public/fonts'),
path_1.default.join(__dirname, '../data/z-code'),
path_1.default.join(__dirname, '../data/zcode-prompts'),
];
for (const dir of dirs) {
if (!(0, fs_1.existsSync)(dir))
(0, fs_1.mkdirSync)(dir, { recursive: true });
}
(0, game_config_1.ensureConfiguredAssetDirectories)(engineConfig);
}
function ensureKokoroJs() {
const src = path_1.default.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const dst = path_1.default.join(__dirname, '../public/js/kokoro-js.js');
if ((0, fs_1.existsSync)(src) && !(0, fs_1.existsSync)(dst))
(0, fs_1.copyFileSync)(src, dst);
}
async function startServer(initialPort, range) {
ensureDirectories();
try {
ensureKokoroJs();
}
catch { /* optional */ }
checkRuntimeConfiguration();
let port = initialPort;
while (port < initialPort + range) {
try {
await new Promise((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`[zcode] Z-code Narrator server running on http://localhost:${port}`);
resolve();
});
server.once('error', (err) => {
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
console.log(`Port ${port} unavailable (${err.code}), trying ${port + 1}...`);
server.close();
port++;
reject();
}
else {
reject(err);
}
});
server.listen(port);
});
return;
}
catch {
if (port >= initialPort + range - 1) {
throw new Error(`Failed to start server on ports ${initialPort}${initialPort + range - 1}`);
}
}
}
}
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch((err) => {
console.error('[zcode] Failed to start:', err);
process.exit(1);
});
}
//# sourceMappingURL=server-zcode.js.map
-1
View File
File diff suppressed because one or more lines are too long
-10
View File
@@ -1,10 +0,0 @@
/**
* Test Server for AI Interactive Fiction
* Simplified version that sends test paragraphs instead of using LLM
*/
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
declare const app: import("express-serve-static-core").Express;
declare const server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
declare const io: SocketIOServer<import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, import("socket.io").DefaultEventsMap, any>;
export { app, server, io };
-282
View File
@@ -1,282 +0,0 @@
"use strict";
/**
* Test Server for AI Interactive Fiction
* Simplified version that sends test paragraphs instead of using LLM
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.io = exports.server = exports.app = void 0;
const path_1 = __importDefault(require("path"));
const express_1 = __importDefault(require("express"));
const http_1 = __importDefault(require("http"));
const socket_io_1 = require("socket.io");
const dotenv = __importStar(require("dotenv"));
const fs_1 = require("fs");
const turn_result_1 = require("./interfaces/turn-result");
// Load environment variables
dotenv.config();
// Create Express application
const app = (0, express_1.default)();
exports.app = app;
const server = http_1.default.createServer(app);
exports.server = server;
const io = new socket_io_1.Server(server);
exports.io = io;
// Get port from environment variables or use default
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
// Serve static files from the public directory. Keep browser modules uncached
// during local development so fixes are visible without a hard cache clear.
app.use(express_1.default.static(path_1.default.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
// Test paragraphs to send to the client
const TEST_PARAGRAPHS = [
"You stand at the entrance of a mysterious cave. The air is cool and damp, carrying the scent of earth and ancient stone. Shadows dance on the walls as your torch flickers in the gentle breeze.",
"As you venture deeper, the passage narrows. Stalactites hang from the ceiling like stone daggers, their surfaces glistening with moisture. The sound of dripping water echoes through the silence.",
"Suddenly, the passage opens into a vast chamber. Crystal formations catch the light of your torch, sending rainbow reflections across the walls. In the center of the room stands an ancient stone pedestal, its surface carved with symbols from a forgotten language."
];
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
let currentParagraphIndex = 0;
let gameRunning = false;
let nextTurnId = 1;
const saveGames = new Set();
const startDemoGame = () => {
gameRunning = true;
nextTurnId = 1;
currentParagraphIndex = 0;
socket.emit('narrativeResponse', {
turnId: nextTurnId++,
paragraphs: [
...(0, turn_result_1.textToParagraphs)("#chapter[Interactive Fiction Test]\n\nWelcome to the Interactive Fiction Test. This is a simplified version that sends predefined paragraphs instead of using an LLM."),
...(0, turn_result_1.textToParagraphs)(TEST_PARAGRAPHS[0]),
],
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: 'test-room',
},
});
};
const normalizeSaveSlot = (slot) => {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
};
socket.on('gameApi', (request, respond) => {
try {
const method = String(request?.method || '');
const args = Array.isArray(request?.args) ? request.args : [];
let response;
switch (method) {
case 'newGame':
case 'newGame()':
startDemoGame();
response = { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
break;
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!saveGames.has(slot)) {
response = { success: false, error: 'missing_save', result: false };
break;
}
startDemoGame();
socket.emit('gameLoaded', { slot });
response = { success: true, result: true, running: true, slot };
break;
}
case 'saveGame':
case 'saveGame()': {
if (!gameRunning) {
response = { success: false, error: 'game_not_running', result: false };
break;
}
const slot = normalizeSaveSlot(args[0]);
saveGames.add(slot);
socket.emit('gameSaved', { slot });
response = { success: true, result: true, slot };
break;
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
response = { success: true, result: saveGames.has(slot), slot };
break;
}
case 'getSaveGames':
case 'getSaveGames()':
response = { success: true, result: Array.from(saveGames).sort((a, b) => a - b) };
break;
case 'isGameRunning':
case 'isGameRunning()':
response = { success: true, result: gameRunning };
break;
default:
response = { success: false, error: `unknown_method:${method}` };
}
if (typeof respond === 'function')
respond(response);
}
catch (error) {
if (typeof respond === 'function') {
respond({ success: false, error: error instanceof Error ? error.message : String(error) });
}
}
});
// Process player command
socket.on('playerCommand', async (data) => {
try {
console.log(`Received command: ${data.command}`);
// Send narrative response to client
socket.emit('narrativeResponse', {
turnId: nextTurnId++,
paragraphs: (0, turn_result_1.textToParagraphs)(String(data.command || '')),
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: "test-room"
},
suggestions: ["look around", "examine pedestal", "touch crystals"]
});
}
catch (error) {
console.error('Error processing command:', error);
socket.emit('error', { message: 'Failed to process command. Please try again.' });
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
// Ensure required asset folders exist
function ensureDirectories() {
const dirs = [
path_1.default.join(__dirname, '../public'),
path_1.default.join(__dirname, '../public/js'),
path_1.default.join(__dirname, '../public/css'),
path_1.default.join(__dirname, '../public/images'),
path_1.default.join(__dirname, '../public/music'),
path_1.default.join(__dirname, '../public/sounds'),
path_1.default.join(__dirname, '../public/fonts')
];
for (const dir of dirs) {
if (!(0, fs_1.existsSync)(dir)) {
(0, fs_1.mkdirSync)(dir, { recursive: true });
}
}
}
// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
const source = path_1.default.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const destination = path_1.default.join(__dirname, '../public/js/kokoro-js.js');
if ((0, fs_1.existsSync)(source) && !(0, fs_1.existsSync)(destination)) {
(0, fs_1.copyFileSync)(source, destination);
console.log(`Copied kokoro-js from ${source} to ${destination}`);
}
}
// Start the server with port fallback
async function startServer(initialPort, range) {
let currentPort = initialPort;
const maxPort = initialPort + range;
// Try ports in the specified range
while (currentPort < maxPort) {
try {
// Ensure directories exist
ensureDirectories();
// Ensure kokoro-js is copied
try {
ensureKokoroJs();
}
catch (error) {
console.error('Error copying kokoro-js:', error);
}
// Try to start the server on the current port
await new Promise((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`AI Interactive Fiction TEST SERVER running on http://localhost:${currentPort}`);
console.log('This server is sending predefined test paragraphs instead of using an LLM');
resolve();
});
server.once('error', (error) => {
// If port is in use, try next port
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
server.close();
currentPort++;
reject();
}
else {
// For other errors, log and reject
console.error('Server error:', error);
reject(error);
}
});
server.listen(currentPort);
});
// If we reach here, server started successfully
return;
}
catch (error) {
// If we reach the max port and still fail, throw an error
if (currentPort >= maxPort - 1) {
throw new Error(`Failed to start server on ports ${initialPort} to ${maxPort - 1}`);
}
// Otherwise try the next port
}
}
}
// Start the server when this module is run directly
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch(error => {
console.error('Failed to start server:', error);
process.exit(1);
});
}
//# sourceMappingURL=test-server-yaml.js.map
-1
View File
File diff suppressed because one or more lines are too long
-4
View File
@@ -1,4 +0,0 @@
import type { StoryTag } from '../interfaces/turn-result';
export declare function parseTag(raw: string): StoryTag | null;
export declare function parseTags(rawTags: unknown[] | undefined): StoryTag[];
export declare function getTagValue(tags: StoryTag[], key: string): string | undefined;
-53
View File
@@ -1,53 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTag = parseTag;
exports.parseTags = parseTags;
exports.getTagValue = getTagValue;
const LEGACY_TAG_ALIASES = {
audio: 'sfx',
audioloop: 'music',
separator: 'section',
};
function normalizeKey(key) {
const normalized = key.trim().toLowerCase().replace(/[^a-z0-9_-]+/g, '-');
return LEGACY_TAG_ALIASES[normalized] || normalized;
}
function parseTag(raw) {
const text = String(raw || '').trim().replace(/^#\s*/, '');
if (!text)
return null;
const bracketMatch = text.match(/^([A-Za-z][\w-]*)(?:\[([^\]]*)\])?(?:\(([^)]*)\))?$/);
if (bracketMatch) {
const tag = { key: normalizeKey(bracketMatch[1]) };
if (typeof bracketMatch[2] !== 'undefined')
tag.value = bracketMatch[2].trim();
if (typeof bracketMatch[3] !== 'undefined')
tag.param = bracketMatch[3].trim();
return tag;
}
const colonMatch = text.match(/^([A-Za-z][\w-]*)\s*:\s*(.*?)\s*(?:\(([^)]*)\))?$/);
if (colonMatch) {
const tag = { key: normalizeKey(colonMatch[1]) };
tag.value = colonMatch[2].trim();
if (typeof colonMatch[3] !== 'undefined')
tag.param = colonMatch[3].trim();
return tag;
}
const bareMatch = text.match(/^[A-Za-z][\w-]*$/);
if (bareMatch) {
return { key: normalizeKey(text) };
}
return null;
}
function parseTags(rawTags) {
if (!Array.isArray(rawTags))
return [];
return rawTags
.map((raw) => parseTag(String(raw ?? '')))
.filter((tag) => Boolean(tag));
}
function getTagValue(tags, key) {
const normalizedKey = normalizeKey(key);
return tags.find((tag) => tag.key === normalizedKey)?.value;
}
//# sourceMappingURL=tag-parser.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"tag-parser.js","sourceRoot":"","sources":["../../src/utils/tag-parser.ts"],"names":[],"mappings":";;AAaA,4BA0BC;AAED,8BAMC;AAED,kCAGC;AAlDD,MAAM,kBAAkB,GAA2B;IACjD,KAAK,EAAE,KAAK;IACZ,SAAS,EAAE,OAAO;IAClB,SAAS,EAAE,SAAS;CACrB,CAAC;AAEF,SAAS,YAAY,CAAC,GAAW;IAC/B,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;IAC1E,OAAO,kBAAkB,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC;AACtD,CAAC;AAED,SAAgB,QAAQ,CAAC,GAAW;IAClC,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC3D,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAEvB,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACvF,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,GAAG,GAAa,EAAE,GAAG,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC7D,IAAI,OAAO,YAAY,CAAC,CAAC,CAAC,KAAK,WAAW;YAAE,GAAG,CAAC,KAAK,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC/E,IAAI,OAAO,YAAY,CAAC,CAAC,CAAC,KAAK,WAAW;YAAE,GAAG,CAAC,KAAK,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC/E,OAAO,GAAG,CAAC;IACb,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACnF,IAAI,UAAU,EAAE,CAAC;QACf,MAAM,GAAG,GAAa,EAAE,GAAG,EAAE,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3D,GAAG,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACjC,IAAI,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,WAAW;YAAE,GAAG,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC3E,OAAO,GAAG,CAAC;IACb,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACjD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,EAAE,GAAG,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;IACrC,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,SAAS,CAAC,OAA8B;IACtD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QAAE,OAAO,EAAE,CAAC;IAEvC,OAAO,OAAO;SACX,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC;SACzC,MAAM,CAAC,CAAC,GAAG,EAAmB,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,SAAgB,WAAW,CAAC,IAAgB,EAAE,GAAW;IACvD,MAAM,aAAa,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;IACxC,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,KAAK,aAAa,CAAC,EAAE,KAAK,CAAC;AAC9D,CAAC"}
-71
View File
@@ -1,71 +0,0 @@
/**
* YAML World Model Parser
* Loads and validates world definitions from YAML files
*/
import { WorldModel } from '../interfaces/world-model';
export declare class YamlWorldParser {
/**
* Load a world model from a YAML file
*/
static loadFromFile(filePath: string): Promise<WorldModel>;
/**
* Validate the loaded YAML data and transform it into a WorldModel
*/
private static validateAndTransform;
/**
* Validate that an object has all required fields
*/
private static validateRequiredFields;
/**
* Validate that a value is a string
*/
private static validateString;
/**
* Validate room definitions
*/
private static validateRooms;
/**
* Validate exit definitions
*/
private static validateExits;
/**
* Validate object definitions
*/
private static validateObjects;
/**
* Validate character definitions
*/
private static validateCharacters;
/**
* Validate action definitions
*/
private static validateActions;
/**
* Validate initial game state
*/
private static validateInitialState;
/**
* Validate object states (record of boolean values)
*/
private static validateObjectStates;
/**
* Validate dialogue (record of string values)
*/
private static validateDialogue;
/**
* Validate flags (record of boolean values)
*/
private static validateFlags;
/**
* Validate counters (record of number values)
*/
private static validateCounters;
/**
* Validate that an array of strings is valid
*/
private static validateStringArray;
/**
* Validate references between entities
*/
private static validateReferences;
}
-399
View File
@@ -1,399 +0,0 @@
"use strict";
/**
* YAML World Model Parser
* Loads and validates world definitions from YAML files
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.YamlWorldParser = void 0;
const fs = __importStar(require("fs/promises"));
const yaml = __importStar(require("js-yaml"));
class YamlWorldParser {
/**
* Load a world model from a YAML file
*/
static async loadFromFile(filePath) {
try {
const fileContents = await fs.readFile(filePath, 'utf8');
const worldData = yaml.load(fileContents);
return this.validateAndTransform(worldData);
}
catch (error) {
console.error(`Error loading world from ${filePath}:`, error);
throw new Error(`Failed to load world from ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Validate the loaded YAML data and transform it into a WorldModel
*/
static validateAndTransform(data) {
if (!data || typeof data !== 'object') {
throw new Error('Invalid world data: must be an object');
}
const worldData = data;
// Validate required top-level fields
this.validateRequiredFields(worldData, ['title', 'author', 'version', 'introduction', 'rooms', 'initialState']);
// Transform and validate the world model
const worldModel = {
title: this.validateString(worldData.title, 'title'),
author: this.validateString(worldData.author, 'author'),
version: this.validateString(worldData.version, 'version'),
introduction: this.validateString(worldData.introduction, 'introduction'),
rooms: this.validateRooms(worldData.rooms),
objects: this.validateObjects(worldData.objects),
characters: this.validateCharacters(worldData.characters),
actions: this.validateActions(worldData.actions),
initialState: this.validateInitialState(worldData.initialState)
};
// Validate references between entities
this.validateReferences(worldModel);
return worldModel;
}
/**
* Validate that an object has all required fields
*/
static validateRequiredFields(data, requiredFields) {
for (const field of requiredFields) {
if (!(field in data)) {
throw new Error(`Missing required field: ${field}`);
}
}
}
/**
* Validate that a value is a string
*/
static validateString(value, fieldName) {
if (typeof value !== 'string') {
throw new Error(`Field ${fieldName} must be a string`);
}
return value;
}
/**
* Validate room definitions
*/
static validateRooms(rooms) {
if (!rooms || typeof rooms !== 'object') {
throw new Error('Rooms must be an object mapping room IDs to room definitions');
}
const roomsData = rooms;
const validatedRooms = {};
for (const [roomId, roomData] of Object.entries(roomsData)) {
if (!roomData || typeof roomData !== 'object') {
throw new Error(`Room ${roomId} must be an object`);
}
const room = roomData;
this.validateRequiredFields(room, ['name', 'description', 'exits']);
validatedRooms[roomId] = {
id: roomId,
name: this.validateString(room.name, `rooms.${roomId}.name`),
description: this.validateString(room.description, `rooms.${roomId}.description`),
exits: this.validateExits(room.exits, roomId),
objects: this.validateStringArray(room.objects || [], `rooms.${roomId}.objects`),
characters: this.validateStringArray(room.characters || [], `rooms.${roomId}.characters`)
};
}
return validatedRooms;
}
/**
* Validate exit definitions
*/
static validateExits(exits, roomId) {
if (!Array.isArray(exits)) {
throw new Error(`Exits for room ${roomId} must be an array`);
}
return exits.map((exit, index) => {
if (!exit || typeof exit !== 'object') {
throw new Error(`Exit ${index} in room ${roomId} must be an object`);
}
const exitData = exit;
this.validateRequiredFields(exitData, ['direction', 'targetRoomId']);
return {
direction: this.validateString(exitData.direction, `rooms.${roomId}.exits[${index}].direction`),
targetRoomId: this.validateString(exitData.targetRoomId, `rooms.${roomId}.exits[${index}].targetRoomId`),
description: exitData.description ? this.validateString(exitData.description, `rooms.${roomId}.exits[${index}].description`) : undefined,
isLocked: typeof exitData.isLocked === 'boolean' ? exitData.isLocked : false,
keyId: exitData.keyId ? this.validateString(exitData.keyId, `rooms.${roomId}.exits[${index}].keyId`) : undefined
};
});
}
/**
* Validate object definitions
*/
static validateObjects(objects) {
if (!objects)
return {}; // Objects are optional
if (typeof objects !== 'object') {
throw new Error('Objects must be an object mapping object IDs to object definitions');
}
const objectsData = objects;
const validatedObjects = {};
for (const [objectId, objectData] of Object.entries(objectsData)) {
if (!objectData || typeof objectData !== 'object') {
throw new Error(`Object ${objectId} must be an object`);
}
const obj = objectData;
this.validateRequiredFields(obj, ['name', 'description', 'traits', 'allowedActions']);
validatedObjects[objectId] = {
id: objectId,
name: this.validateString(obj.name, `objects.${objectId}.name`),
description: this.validateString(obj.description, `objects.${objectId}.description`),
traits: this.validateStringArray(obj.traits, `objects.${objectId}.traits`),
states: this.validateObjectStates(obj.states, objectId),
allowedActions: this.validateStringArray(obj.allowedActions, `objects.${objectId}.allowedActions`),
containedObjects: obj.containedObjects ? this.validateStringArray(obj.containedObjects, `objects.${objectId}.containedObjects`) : []
};
}
return validatedObjects;
}
/**
* Validate character definitions
*/
static validateCharacters(characters) {
if (!characters)
return {}; // Characters are optional
if (typeof characters !== 'object') {
throw new Error('Characters must be an object mapping character IDs to character definitions');
}
const charactersData = characters;
const validatedCharacters = {};
for (const [characterId, characterData] of Object.entries(charactersData)) {
if (!characterData || typeof characterData !== 'object') {
throw new Error(`Character ${characterId} must be an object`);
}
const character = characterData;
this.validateRequiredFields(character, ['name', 'description', 'dialogue', 'defaultResponse']);
validatedCharacters[characterId] = {
id: characterId,
name: this.validateString(character.name, `characters.${characterId}.name`),
description: this.validateString(character.description, `characters.${characterId}.description`),
dialogue: this.validateDialogue(character.dialogue, characterId),
inventory: this.validateStringArray(character.inventory || [], `characters.${characterId}.inventory`),
defaultResponse: this.validateString(character.defaultResponse, `characters.${characterId}.defaultResponse`),
mood: character.mood ? this.validateString(character.mood, `characters.${characterId}.mood`) : undefined
};
}
return validatedCharacters;
}
/**
* Validate action definitions
*/
static validateActions(actions) {
if (!actions)
return {}; // Actions are optional
if (typeof actions !== 'object') {
throw new Error('Actions must be an object mapping action names to action definitions');
}
const actionsData = actions;
const validatedActions = {};
for (const [actionName, actionData] of Object.entries(actionsData)) {
if (!actionData || typeof actionData !== 'object') {
throw new Error(`Action ${actionName} must be an object`);
}
const action = actionData;
this.validateRequiredFields(action, ['patterns', 'handler']);
validatedActions[actionName] = {
name: actionName,
patterns: this.validateStringArray(action.patterns, `actions.${actionName}.patterns`),
requiresObject: typeof action.requiresObject === 'boolean' ? action.requiresObject : false,
requiresTarget: typeof action.requiresTarget === 'boolean' ? action.requiresTarget : false,
handler: this.validateString(action.handler, `actions.${actionName}.handler`)
};
}
return validatedActions;
}
/**
* Validate initial game state
*/
static validateInitialState(initialState) {
if (!initialState || typeof initialState !== 'object') {
throw new Error('Initial state must be an object');
}
const stateData = initialState;
this.validateRequiredFields(stateData, ['currentRoomId']);
return {
currentRoomId: this.validateString(stateData.currentRoomId, 'initialState.currentRoomId'),
inventory: this.validateStringArray(stateData.inventory || [], 'initialState.inventory'),
visitedRooms: this.validateStringArray(stateData.visitedRooms || [], 'initialState.visitedRooms'),
flags: this.validateFlags(stateData.flags),
counters: this.validateCounters(stateData.counters)
};
}
/**
* Validate object states (record of boolean values)
*/
static validateObjectStates(states, objectId) {
if (!states)
return {};
if (typeof states !== 'object') {
throw new Error(`States for object ${objectId} must be an object`);
}
const statesData = states;
const validatedStates = {};
for (const [stateName, stateValue] of Object.entries(statesData)) {
if (typeof stateValue !== 'boolean') {
throw new Error(`State ${stateName} for object ${objectId} must be a boolean value`);
}
validatedStates[stateName] = stateValue;
}
return validatedStates;
}
/**
* Validate dialogue (record of string values)
*/
static validateDialogue(dialogue, characterId) {
if (!dialogue || typeof dialogue !== 'object') {
throw new Error(`Dialogue for character ${characterId} must be an object`);
}
const dialogueData = dialogue;
const validatedDialogue = {};
for (const [topic, response] of Object.entries(dialogueData)) {
validatedDialogue[topic] = this.validateString(response, `characters.${characterId}.dialogue.${topic}`);
}
return validatedDialogue;
}
/**
* Validate flags (record of boolean values)
*/
static validateFlags(flags) {
if (!flags)
return {};
if (typeof flags !== 'object') {
throw new Error('Flags must be an object');
}
const flagsData = flags;
const validatedFlags = {};
for (const [flagName, flagValue] of Object.entries(flagsData)) {
if (typeof flagValue !== 'boolean') {
throw new Error(`Flag ${flagName} must be a boolean value`);
}
validatedFlags[flagName] = flagValue;
}
return validatedFlags;
}
/**
* Validate counters (record of number values)
*/
static validateCounters(counters) {
if (!counters)
return {};
if (typeof counters !== 'object') {
throw new Error('Counters must be an object');
}
const countersData = counters;
const validatedCounters = {};
for (const [counterName, counterValue] of Object.entries(countersData)) {
if (typeof counterValue !== 'number') {
throw new Error(`Counter ${counterName} must be a numeric value`);
}
validatedCounters[counterName] = counterValue;
}
return validatedCounters;
}
/**
* Validate that an array of strings is valid
*/
static validateStringArray(arr, fieldName) {
if (!arr)
return [];
if (!Array.isArray(arr)) {
throw new Error(`Field ${fieldName} must be an array`);
}
return arr.map((item, index) => {
if (typeof item !== 'string') {
throw new Error(`Item at index ${index} in ${fieldName} must be a string`);
}
return item;
});
}
/**
* Validate references between entities
*/
static validateReferences(worldModel) {
const { rooms, objects, characters, initialState } = worldModel;
// Check that the initial room exists
if (!rooms[initialState.currentRoomId]) {
throw new Error(`Initial room ${initialState.currentRoomId} does not exist`);
}
// Check room exits
for (const [roomId, room] of Object.entries(rooms)) {
for (const exit of room.exits) {
if (!rooms[exit.targetRoomId]) {
throw new Error(`Room ${roomId} has an exit to non-existent room ${exit.targetRoomId}`);
}
if (exit.keyId && !objects[exit.keyId]) {
throw new Error(`Room ${roomId} has an exit requiring non-existent key ${exit.keyId}`);
}
}
// Check room objects
for (const objectId of room.objects) {
if (!objects[objectId]) {
throw new Error(`Room ${roomId} contains non-existent object ${objectId}`);
}
}
// Check room characters
for (const characterId of room.characters) {
if (!characters[characterId]) {
throw new Error(`Room ${roomId} contains non-existent character ${characterId}`);
}
}
}
// Check object containment
for (const [objectId, object] of Object.entries(objects)) {
if (object.containedObjects) {
for (const containedId of object.containedObjects) {
if (!objects[containedId]) {
throw new Error(`Object ${objectId} contains non-existent object ${containedId}`);
}
}
}
}
// Check character inventory
for (const [characterId, character] of Object.entries(characters)) {
for (const objectId of character.inventory) {
if (!objects[objectId]) {
throw new Error(`Character ${characterId} has non-existent object ${objectId} in inventory`);
}
}
}
// Check player inventory
for (const objectId of initialState.inventory) {
if (!objects[objectId]) {
throw new Error(`Initial inventory contains non-existent object ${objectId}`);
}
}
}
}
exports.YamlWorldParser = YamlWorldParser;
//# sourceMappingURL=yaml-parser.js.map
File diff suppressed because one or more lines are too long
-12
View File
@@ -1,12 +0,0 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
collectCoverage: true,
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts'],
};
+9 -36
View File
@@ -6,43 +6,16 @@
"scripts": { "scripts": {
"check:node": "node scripts/check-node-version.js", "check:node": "node scripts/check-node-version.js",
"prestart": "npm run check:node", "prestart": "npm run check:node",
"start": "node scripts/run-engine.js start", "start": "node dist/server-ink.js",
"prestart:cli": "npm run check:node",
"start:cli": "node dist/index.js --cli",
"predev": "npm run check:node", "predev": "npm run check:node",
"dev": "node scripts/run-engine.js dev", "dev": "nodemon --watch src --watch data/ink-src --watch config/engines/ink.json --ext ts,json,ink --exec \"ts-node src/server-ink.ts\"",
"predev:yaml": "npm run check:node", "dev:debug": "node -e \"process.env.INK_DEBUG='1'; require('child_process').spawn('npm', ['run', 'dev'], { stdio: 'inherit', shell: true, env: process.env })\"",
"dev:yaml": "nodemon --watch src --watch data/worlds --watch config/engines/yaml.json --ext ts,json,yml --exec \"ts-node src/server-yaml.ts\"", "dev:inspect": "nodemon --watch src --watch data/ink-src --watch config/engines/ink.json --ext ts,json,ink --exec \"node --inspect=0.0.0.0:9231 -r ts-node/register src/server-ink.ts\"",
"dev:yaml:debug": "powershell -NoProfile -Command \"$env:YAML_DEBUG='1'; npm run dev:yaml\"", "prestart:debug": "npm run check:node",
"dev:yaml:inspect": "powershell -NoProfile -Command \"$env:YAML_DEBUG='1'; nodemon --watch src --watch data/worlds --watch config/engines/yaml.json --ext ts,json,yml --exec \\\"node --inspect=127.0.0.1:9230 -r ts-node/register src/server-yaml.ts\\\"\"", "start:debug": "node -e \"process.env.INK_DEBUG='1'; require('./dist/server-ink.js')\"",
"predev:cli": "npm run check:node", "prestart:inspect": "npm run check:node",
"dev:cli": "nodemon --watch src --ext ts,json --exec \"ts-node src/index.ts --cli\"", "start:inspect": "node --inspect=0.0.0.0:9231 dist/server-ink.js",
"predev:zcode": "npm run check:node", "build": "tsc"
"dev:zcode": "nodemon --watch src --watch data/zcode-prompts --watch config/engines/zcode.json --ext ts,json,yml --exec \"ts-node src/server-zcode.ts\"",
"dev:zcode:debug": "powershell -NoProfile -Command \"$env:ZCODE_DEBUG='1'; npm run dev:zcode\"",
"dev:zcode:inspect": "powershell -NoProfile -Command \"$env:ZCODE_DEBUG='1'; nodemon --watch src --watch data/zcode-prompts --watch config/engines/zcode.json --ext ts,json,yml --exec \\\"node --inspect=127.0.0.1:9229 -r ts-node/register src/server-zcode.ts\\\"\"",
"predev:ink": "npm run check:node",
"dev:ink": "nodemon --watch src --watch data/ink-src --watch config/engines/ink.json --ext ts,json,ink --exec \"ts-node src/server-ink.ts\"",
"dev:ink:debug": "powershell -NoProfile -Command \"$env:INK_DEBUG='1'; npm run dev:ink\"",
"dev:ink:inspect": "powershell -NoProfile -Command \"$env:INK_DEBUG='1'; nodemon --watch src --watch data/ink-src --watch config/engines/ink.json --ext ts,json,ink --exec \\\"node --inspect=127.0.0.1:9231 -r ts-node/register src/server-ink.ts\\\"\"",
"prestart:yaml": "npm run check:node && npm run build",
"start:yaml": "node dist/server-yaml.js",
"start:yaml:debug": "powershell -NoProfile -Command \"$env:YAML_DEBUG='1'; npm run start:yaml\"",
"start:yaml:inspect": "powershell -NoProfile -Command \"$env:YAML_DEBUG='1'; node --inspect=127.0.0.1:9230 dist/server-yaml.js\"",
"prestart:zcode": "npm run check:node && npm run build",
"start:zcode": "node dist/server-zcode.js",
"start:zcode:debug": "powershell -NoProfile -Command \"$env:ZCODE_DEBUG='1'; npm run start:zcode\"",
"start:zcode:inspect": "powershell -NoProfile -Command \"$env:ZCODE_DEBUG='1'; node --inspect=127.0.0.1:9229 dist/server-zcode.js\"",
"prestart:ink": "npm run check:node && npm run build",
"start:ink": "node dist/server-ink.js",
"start:ink:debug": "powershell -NoProfile -Command \"$env:INK_DEBUG='1'; npm run start:ink\"",
"start:ink:inspect": "powershell -NoProfile -Command \"$env:INK_DEBUG='1'; node --inspect=127.0.0.1:9231 dist/server-ink.js\"",
"pretest-server": "npm run check:node",
"test-server": "ts-node src/test-server-yaml.ts",
"build": "tsc",
"test": "jest",
"lint": "eslint --ext .ts src/",
"lint:fix": "eslint --ext .ts src/ --fix"
}, },
"engines": { "engines": {
"node": ">=18.17" "node": ">=18.17"
+1 -19
View File
@@ -245,6 +245,7 @@
border: 1px none rgba(255,255,255,0); border: 1px none rgba(255,255,255,0);
} }
</style> </style>
<script src="/js/logger.js?v=20260519-logger"></script>
</head> </head>
<body> <body>
<!-- Debug output area --> <!-- Debug output area -->
@@ -279,25 +280,6 @@
console.log(message); console.log(message);
}; };
</script> </script>
<script>
// Redefine console.log to expose browser logs to model
const originalLog = console.log;
console.log = function(...args) {
if (typeof debug !== 'undefined' && debug) {
const debugContent = document.getElementById('debug-content');
if (debugContent) {
const logMsg = document.createElement('div');
// Convert all arguments to string and join them
logMsg.textContent = args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ');
debugContent.appendChild(logMsg);
}
}
// Pass all arguments to the original console.log
originalLog.apply(console, args);
};
</script>
<script type="module" src="/js/loader.js?v=20260516-scroll-window"></script> <script type="module" src="/js/loader.js?v=20260516-scroll-window"></script>
</body> </body>
</html> </html>
+29 -7
View File
@@ -43,6 +43,8 @@ class GameLoopModule extends BaseModule {
'queueUnrenderedHistoryBlocks', 'queueUnrenderedHistoryBlocks',
'autoSaveCurrentSession', 'autoSaveCurrentSession',
'restoreBrowserSave', 'restoreBrowserSave',
'restoreInputStateFromSave',
'hasUnrenderedHistory',
'resumeAutosaveIfAvailable', 'resumeAutosaveIfAvailable',
'requestStartGame', 'requestStartGame',
'requestSaveGame', 'requestSaveGame',
@@ -214,17 +216,14 @@ class GameLoopModule extends BaseModule {
return false; return false;
} }
await this.restoreBrowserSave(browserSave, 'autosave-resume', { resetDisplay: true });
this.gameState.started = Boolean(response.running); this.gameState.started = Boolean(response.running);
this.gameState.startedOnce = true; this.gameState.startedOnce = true;
this.gameState.ended = !response.running && browserSave.inputMode === 'end'; this.gameState.ended = !response.running && browserSave.inputMode === 'end';
this.gameState.canSave = this.gameState.started; this.gameState.canSave = this.gameState.started;
this.gameState.canLoad = true; this.gameState.canLoad = true;
this.currentChoices = Array.isArray(browserSave.choices) ? browserSave.choices : [];
this.currentInputMode = browserSave.inputMode || 'none';
document.dispatchEvent(new CustomEvent('story:choices', { detail: this.currentChoices }));
document.dispatchEvent(new CustomEvent('story:input-mode', { detail: this.currentInputMode }));
this.updateUIState(); this.updateUIState();
await this.restoreBrowserSave(browserSave, 'autosave-resume', { resetDisplay: true });
this.restoreInputStateFromSave(browserSave, 'autosave-resume');
return true; return true;
} }
@@ -393,8 +392,7 @@ class GameLoopModule extends BaseModule {
if (browserSave?.musicState && audioManager?.restoreMusicState) { if (browserSave?.musicState && audioManager?.restoreMusicState) {
await audioManager.restoreMusicState(browserSave.musicState); await audioManager.restoreMusicState(browserSave.musicState);
} }
const hasUnrenderedHistory = browserSave && const hasUnrenderedHistory = this.hasUnrenderedHistory(browserSave);
Number(browserSave.latestBlockId || 0) > Number(browserSave.latestRenderedBlockId || 0);
if (hasUnrenderedHistory) { if (hasUnrenderedHistory) {
const sentenceQueue = this.getModule('sentence-queue'); const sentenceQueue = this.getModule('sentence-queue');
sentenceQueue?.pauseBeforeNext?.('load-resume'); sentenceQueue?.pauseBeforeNext?.('load-resume');
@@ -420,6 +418,30 @@ class GameLoopModule extends BaseModule {
} }
} }
restoreInputStateFromSave(browserSave, reason = 'load-game') {
const choices = Array.isArray(browserSave?.choices) ? browserSave.choices : [];
const savedMode = ['text', 'choice', 'end', 'none'].includes(browserSave?.inputMode)
? browserSave.inputMode
: null;
const inputMode = savedMode || (choices.length > 0 ? 'choice' : 'none');
this.currentChoices = choices;
this.currentInputMode = inputMode;
document.dispatchEvent(new CustomEvent('story:choices', { detail: choices }));
document.dispatchEvent(new CustomEvent('story:input-mode', { detail: inputMode }));
if (!this.hasUnrenderedHistory(browserSave)) {
document.dispatchEvent(new CustomEvent('story:process-state', {
detail: { state: 'ready', reason: `${reason}-input-restored`, inputMode }
}));
}
}
hasUnrenderedHistory(browserSave) {
return Boolean(browserSave) &&
Number(browserSave.latestBlockId || 0) > Number(browserSave.latestRenderedBlockId || 0);
}
async hasSaveGame(slot = 1) { async hasSaveGame(slot = 1) {
const storyHistory = this.getModule('story-history'); const storyHistory = this.getModule('story-history');
if (storyHistory && typeof storyHistory.hasSaveSlot === 'function') { if (storyHistory && typeof storyHistory.hasSaveSlot === 'function') {
+85
View File
@@ -0,0 +1,85 @@
(function () {
const originalConsole = {
debug: console.debug.bind(console),
log: console.log.bind(console),
info: console.info.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console)
};
const levels = {
silent: 0,
error: 1,
warn: 2,
info: 3,
debug: 4
};
function readLevel() {
const params = new URLSearchParams(window.location.search);
const queryLevel = params.get('log') || params.get('debug');
if (queryLevel === '1' || queryLevel === 'true') return 'debug';
if (queryLevel && levels[queryLevel]) return queryLevel;
const savedLevel = localStorage.getItem('ai-if-log-level');
if (savedLevel && Object.prototype.hasOwnProperty.call(levels, savedLevel)) {
return savedLevel;
}
return 'warn';
}
const logger = {
level: readLevel(),
levels,
originalConsole,
setLevel(level) {
if (!Object.prototype.hasOwnProperty.call(levels, level)) {
originalConsole.warn(`Unknown log level "${level}". Use silent, error, warn, info, or debug.`);
return;
}
this.level = level;
localStorage.setItem('ai-if-log-level', level);
originalConsole.info(`AI IF log level set to ${level}`);
},
shouldPrint(level) {
return levels[this.level] >= levels[level];
},
write(level, args) {
if (!this.shouldPrint(level)) return;
const writer = originalConsole[level] || originalConsole.log;
writer(...args);
},
debug(...args) {
this.write('debug', args);
},
log(...args) {
this.write('debug', args);
},
info(...args) {
this.write('info', args);
},
warn(...args) {
this.write('warn', args);
},
error(...args) {
this.write('error', args);
}
};
console.debug = (...args) => logger.debug(...args);
console.log = (...args) => logger.log(...args);
console.info = (...args) => logger.info(...args);
console.warn = (...args) => logger.warn(...args);
console.error = (...args) => logger.error(...args);
window.AppLogger = logger;
})();
+56 -5
View File
@@ -19,9 +19,11 @@ class SocketClientModule extends BaseModule {
this.storyHistory = null; this.storyHistory = null;
this.isConnected = false; this.isConnected = false;
this.reconnectAttempts = 0; this.reconnectAttempts = 0;
this.maxReconnectAttempts = Infinity; this.maxReconnectAttempts = 12;
this.reconnectDelay = 2000; this.reconnectDelay = 5000;
this.maxReconnectDelay = 30000; this.maxReconnectDelay = 5000;
this.reconnectTimer = null;
this.reconnectAlerted = false;
this.url = null; this.url = null;
this.eventListeners = {}; this.eventListeners = {};
this.defaultHost = 'localhost:3000'; this.defaultHost = 'localhost:3000';
@@ -77,6 +79,8 @@ class SocketClientModule extends BaseModule {
'resolveAssetUrl', 'resolveAssetUrl',
'looksLikeAssetPath', 'looksLikeAssetPath',
'attemptReconnect', 'attemptReconnect',
'stopReconnectLoop',
'notifyReconnectFailed',
'getConnectionStatus', 'getConnectionStatus',
'loadSocketIO' 'loadSocketIO'
]); ]);
@@ -152,6 +156,12 @@ class SocketClientModule extends BaseModule {
try { try {
console.log(`Socket Client: Connecting to ${socketUrl}`); console.log(`Socket Client: Connecting to ${socketUrl}`);
if (this.socket) {
this.socket.removeAllListeners();
this.socket.close();
this.socket = null;
}
// Create Socket.IO connection (will automatically use /socket.io endpoint) // Create Socket.IO connection (will automatically use /socket.io endpoint)
this.socket = window.io(socketUrl, { this.socket = window.io(socketUrl, {
reconnection: false, // We handle reconnection ourselves reconnection: false, // We handle reconnection ourselves
@@ -162,6 +172,8 @@ class SocketClientModule extends BaseModule {
console.log('Socket Client: Connected to server with ID:', this.socket.id); console.log('Socket Client: Connected to server with ID:', this.socket.id);
this.isConnected = true; this.isConnected = true;
this.reconnectAttempts = 0; this.reconnectAttempts = 0;
this.stopReconnectLoop();
this.reconnectAlerted = false;
this.emitEvent('connect'); this.emitEvent('connect');
resolve(true); resolve(true);
}); });
@@ -177,6 +189,9 @@ class SocketClientModule extends BaseModule {
this.socket.on('connect_error', (error) => { this.socket.on('connect_error', (error) => {
console.error('Socket Client: Connection error:', error); console.error('Socket Client: Connection error:', error);
this.emitEvent('connect_error', error); this.emitEvent('connect_error', error);
if (!this.isConnected) {
this.attemptReconnect();
}
resolve(false); resolve(false);
}); });
@@ -659,7 +674,11 @@ class SocketClientModule extends BaseModule {
*/ */
attemptReconnect() { attemptReconnect() {
if (this.reconnectAttempts >= this.maxReconnectAttempts) { if (this.reconnectAttempts >= this.maxReconnectAttempts) {
console.error('Socket Client: Max reconnect attempts reached'); this.notifyReconnectFailed();
return;
}
if (this.reconnectTimer) {
return; return;
} }
@@ -667,14 +686,46 @@ class SocketClientModule extends BaseModule {
const delay = Math.min(this.maxReconnectDelay, this.reconnectDelay * this.reconnectAttempts); const delay = Math.min(this.maxReconnectDelay, this.reconnectDelay * this.reconnectAttempts);
console.log(`Socket Client: Attempting to reconnect in ${delay}ms (attempt ${this.reconnectAttempts})`); console.log(`Socket Client: Attempting to reconnect in ${delay}ms (attempt ${this.reconnectAttempts})`);
document.dispatchEvent(new CustomEvent('story:process-state', {
detail: { state: 'waiting-generating', reason: 'socket-reconnecting', attempt: this.reconnectAttempts }
}));
setTimeout(() => { this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
if (!this.isConnected) { if (!this.isConnected) {
this.connect(); this.connect();
} }
}, delay); }, delay);
} }
stopReconnectLoop() {
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
}
}
notifyReconnectFailed() {
if (this.reconnectAlerted) return;
this.reconnectAlerted = true;
this.stopReconnectLoop();
const message = this.translate(
'popup.serverUnavailable',
'The game server is currently unavailable. The client tried to reconnect for one minute. Please reload the page after the server is running again.'
);
console.error('Socket Client: Reconnect failed after one minute');
document.dispatchEvent(new CustomEvent('story:tag', {
detail: {
key: 'error',
value: message,
source: 'socket-reconnect'
}
}));
document.dispatchEvent(new CustomEvent('story:process-state', {
detail: { state: 'ready', reason: 'socket-reconnect-failed' }
}));
}
/** /**
* Disconnect from the server * Disconnect from the server
*/ */
+2 -1
View File
@@ -1199,7 +1199,8 @@ class TTSFactoryModule extends BaseModule {
} else if (handler && typeof handler.configure === 'function') { } else if (handler && typeof handler.configure === 'function') {
handler.configure(voiceOptions); handler.configure(voiceOptions);
} }
if (voiceOptions.language && !voiceOptions.voice && handler && typeof handler.selectVoiceForLocale === 'function') { const handlerHasVoice = Boolean(this.getEffectiveVoiceId(handler));
if (voiceOptions.language && !voiceOptions.voice && !handlerHasVoice && handler && typeof handler.selectVoiceForLocale === 'function') {
handler.selectVoiceForLocale(voiceOptions.language); handler.selectVoiceForLocale(voiceOptions.language);
} }
} }
+2 -1
View File
@@ -64,5 +64,6 @@
"popup.defaultError": "Das Spiel wurde wegen eines nicht behebbaren Fehlers beendet.", "popup.defaultError": "Das Spiel wurde wegen eines nicht behebbaren Fehlers beendet.",
"popup.defaultAchievement": "Errungenschaft freigeschaltet.", "popup.defaultAchievement": "Errungenschaft freigeschaltet.",
"popup.defaultAlert": "Hinweis", "popup.defaultAlert": "Hinweis",
"popup.commandTimeout": "Der Spielserver hat nicht rechtzeitig geantwortet. Du kannst es noch einmal versuchen." "popup.commandTimeout": "Der Spielserver hat nicht rechtzeitig geantwortet. Du kannst es noch einmal versuchen.",
"popup.serverUnavailable": "Der Spielserver ist im Moment nicht erreichbar. Der Client hat eine Minute lang versucht, die Verbindung wiederherzustellen. Bitte lade die Seite neu, sobald der Server wieder läuft."
} }
+2 -1
View File
@@ -64,5 +64,6 @@
"popup.defaultError": "The game ended because of an unrecoverable error.", "popup.defaultError": "The game ended because of an unrecoverable error.",
"popup.defaultAchievement": "Achievement unlocked.", "popup.defaultAchievement": "Achievement unlocked.",
"popup.defaultAlert": "Hint", "popup.defaultAlert": "Hint",
"popup.commandTimeout": "The game server did not answer in time. You can try again." "popup.commandTimeout": "The game server did not answer in time. You can try again.",
"popup.serverUnavailable": "The game server is currently unavailable. The client tried to reconnect for one minute. Please reload the page after the server is running again."
} }
-51
View File
@@ -1,51 +0,0 @@
#!/usr/bin/env node
const { spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
function loadDotEnv(filePath) {
if (!fs.existsSync(filePath)) return;
const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const match = trimmed.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
if (!match) continue;
const [, key, rawValue] = match;
if (process.env[key] != null) continue;
process.env[key] = rawValue.replace(/^["']|["']$/g, '');
}
}
const projectRoot = path.resolve(__dirname, '..');
loadDotEnv(path.join(projectRoot, '.env'));
const mode = process.argv[2] || 'dev';
const engine = String(process.env.DEFAULT_GAME_ENGINE || process.env.GAME_ENGINE || 'ink')
.trim()
.toLowerCase();
const allowedModes = new Set(['dev', 'start']);
const allowedEngines = new Set(['ink', 'yaml', 'zcode']);
if (!allowedModes.has(mode)) {
console.error(`Unsupported run mode "${mode}". Use "dev" or "start".`);
process.exit(1);
}
if (!allowedEngines.has(engine)) {
console.error(`Unsupported DEFAULT_GAME_ENGINE "${engine}". Use "ink", "yaml", or "zcode".`);
process.exit(1);
}
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const script = `${mode}:${engine}`;
console.log(`[run-engine] DEFAULT_GAME_ENGINE=${engine}; running npm run ${script}`);
const result = spawnSync(npmCommand, ['run', script], {
cwd: projectRoot,
env: process.env,
stdio: 'inherit',
});
process.exit(result.status == null ? 1 : result.status);
-265
View File
@@ -1,265 +0,0 @@
/**
* Command-line interface for running the interactive fiction game
*/
import * as readline from 'readline';
import * as path from 'path';
import * as dotenv from 'dotenv';
import { TextAdventureEngine } from '../engine/game-engine';
import { OpenRouterProvider } from '../llm/openrouter-provider';
import { ActionRequest, NarrativeRequest } from '../interfaces/llm';
// Load environment variables
dotenv.config();
export class GameRunner {
private engine: TextAdventureEngine;
private llmProvider: OpenRouterProvider;
private rl: readline.Interface | null = null;
private gameContext: string = '';
private gameHistory: string[] = [];
private suggestedCommands: string[] = [];
constructor() {
this.engine = new TextAdventureEngine();
this.llmProvider = new OpenRouterProvider();
}
/**
* Initialize the game
*/
public async initialize(worldPath: string): Promise<void> {
console.log('Initializing game...');
// Initialize LLM provider
const apiKey = process.env.OPENROUTER_API_KEY;
const model = process.env.OPENROUTER_MODEL;
if (!apiKey || !model) {
throw new Error('Missing required environment variables: OPENROUTER_API_KEY and/or OPENROUTER_MODEL');
}
await this.llmProvider.initialize({
apiKey,
model,
temperature: 0.7,
maxTokens: 800
});
// Load the world
const resolvedPath = path.resolve(worldPath);
console.log(`Loading world from ${resolvedPath}...`);
await this.engine.loadWorld(resolvedPath);
console.log('Game initialized successfully!');
}
/**
* Start the game in CLI mode
*/
public async start(): Promise<void> {
// Create readline interface for CLI mode
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
try {
// Display introduction
const introText = await this.engine.start();
console.log('\n' + introText + '\n');
// Look at initial room
const initialLook = this.engine.processAction({ action: 'look', confidence: 1 });
// Generate narrative description
const narrativeRequest: NarrativeRequest = {
action: 'look',
result: initialLook.message,
roomDescription: this.engine.getCurrentRoomDescription(),
visibleObjects: this.engine.getVisibleObjects(),
visibleCharacters: this.engine.getVisibleCharacters(),
tone: 'descriptive'
};
const narrative = await this.llmProvider.generateNarrative(narrativeRequest);
console.log('\n' + narrative.text + '\n');
// Store suggestions if available
if (narrative.suggestions && narrative.suggestions.length > 0) {
this.suggestedCommands = narrative.suggestions;
}
// Update game context
this.updateGameContext(narrative.text);
// Start the game loop
this.gameLoop();
} catch (error) {
console.error('Error starting game:', error);
this.end();
}
}
/**
* The main game loop for CLI mode
*/
private gameLoop(): void {
if (!this.rl) return;
this.rl.question('> ', async (input) => {
if (input.toLowerCase() === 'quit' || input.toLowerCase() === 'exit') {
this.end();
return;
}
const response = await this.processCommand(input);
console.log('\n' + response + '\n');
// Continue the game loop
this.gameLoop();
});
}
/**
* Process a player command and return the narrative response
* Used by both CLI and web interfaces
*/
public async processCommand(input: string): Promise<string> {
try {
// Process player input
const actionRequest: ActionRequest = {
playerInput: input,
currentRoom: this.engine.getWorldModel().rooms[this.engine.getCurrentState().currentRoomId].name,
visibleObjects: this.engine.getVisibleObjects().map(id => this.engine.getWorldModel().objects[id].name),
visibleCharacters: this.engine.getVisibleCharacters().map(id => this.engine.getWorldModel().characters[id].name),
possibleActions: this.engine.getAvailableActions(),
inventory: this.engine.getCurrentState().inventory.map(id => this.engine.getWorldModel().objects[id].name),
gameContext: this.gameContext
};
if (this.rl) {
console.log('Thinking...');
}
// Translate player input to action
const action = await this.llmProvider.translateAction(actionRequest);
// Process the action in the game engine
const actionResult = this.engine.processAction(action);
// If state changed, update it
if (actionResult.stateChanged && actionResult.newState) {
this.engine.getCurrentState().currentRoomId = actionResult.newState.currentRoomId;
this.engine.getCurrentState().inventory = actionResult.newState.inventory;
this.engine.getCurrentState().visitedRooms = actionResult.newState.visitedRooms;
this.engine.getCurrentState().flags = actionResult.newState.flags;
this.engine.getCurrentState().counters = actionResult.newState.counters;
}
// Generate narrative description
const narrativeRequest: NarrativeRequest = {
action: `${action.action}${action.object ? ' ' + action.object : ''}${action.target ? ' on ' + action.target : ''}`,
result: actionResult.message,
roomDescription: this.engine.getCurrentRoomDescription(),
visibleObjects: this.engine.getVisibleObjects().map(id => this.engine.getWorldModel().objects[id].name),
visibleCharacters: this.engine.getVisibleCharacters().map(id => this.engine.getWorldModel().characters[id].name),
previousContext: this.gameHistory.slice(-3).join('\n'),
tone: 'descriptive'
};
const narrative = await this.llmProvider.generateNarrative(narrativeRequest);
// Store suggestions if available
if (narrative.suggestions && narrative.suggestions.length > 0) {
this.suggestedCommands = narrative.suggestions;
}
// Update game context with the new narrative
this.updateGameContext(narrative.text);
// Return the narrative text
return narrative.text;
} catch (error) {
console.error('Error processing input:', error);
return 'Something went wrong. Please try again.';
}
}
/**
* End the game
*/
public end(): void {
console.log('\nThanks for playing!');
if (this.rl) {
this.rl.close();
this.rl = null;
}
this.engine.end();
if (process.env.NODE_ENV !== 'production') {
process.exit(0);
}
}
/**
* Update the game context with new narrative
*/
private updateGameContext(narrative: string): void {
// Add to history
this.gameHistory.push(narrative);
// Keep history limited to last 10 entries
if (this.gameHistory.length > 10) {
this.gameHistory.shift();
}
// Update current context (last 5 entries)
this.gameContext = this.gameHistory.slice(-5).join('\n');
}
/**
* Get the current game state
* Used by web interface
*/
public getGameState() {
return {
world: this.engine.getWorldModel(),
currentRoomId: this.engine.getCurrentState().currentRoomId,
inventory: this.engine.getCurrentState().inventory,
visitedRooms: this.engine.getCurrentState().visitedRooms,
flags: this.engine.getCurrentState().flags,
counters: this.engine.getCurrentState().counters
};
}
/**
* Get the current room description
* Used by web interface
*/
public getCurrentRoomDescription(): string {
const roomId = this.engine.getCurrentState().currentRoomId;
return this.engine.getWorldModel().rooms[roomId].description;
}
/**
* Get suggested actions for the current game state
* Used by web interface
*/
public getSuggestions(): string[] {
return this.suggestedCommands;
}
/**
* Load a saved game state
* Used by web interface
*/
public loadGameState(savedState: any): void {
// Set the current state to match the saved state
this.engine.getCurrentState().currentRoomId = savedState.currentRoomId;
this.engine.getCurrentState().inventory = savedState.inventory;
this.engine.getCurrentState().visitedRooms = savedState.visitedRooms;
this.engine.getCurrentState().flags = savedState.flags;
this.engine.getCurrentState().counters = savedState.counters;
}
}
+4 -7
View File
@@ -1,7 +1,7 @@
import path from 'path'; import path from 'path';
import { existsSync, mkdirSync, readFileSync } from 'fs'; import { existsSync, mkdirSync, readFileSync } from 'fs';
export type EngineName = 'yaml' | 'ink' | 'zcode' | string; export type EngineName = 'ink' | string;
export interface GameMetadata { export interface GameMetadata {
title: string; title: string;
@@ -37,12 +37,9 @@ function fallbackConfig(engine: EngineName): GameEngineConfig {
engine, engine,
locale: 'en_US', locale: 'en_US',
paths: { paths: {
mainGameFile: mainGameFile: 'data/ink/eibenreith.ink.json',
engine === 'ink' inkSource: 'data/ink-src/eibenreith.ink',
? 'data/ink/story.ink.json' inkCompiled: 'data/ink/eibenreith.ink.json',
: engine === 'zcode'
? 'data/z-code/zork1.bin'
: 'data/worlds/example_world.yml',
music: 'public/music', music: 'public/music',
sfx: 'public/sounds', sfx: 'public/sounds',
images: 'public/images', images: 'public/images',
-661
View File
@@ -1,661 +0,0 @@
/**
* Core Game Engine
* Manages game state and processes actions
*/
import * as fs from 'fs/promises';
import { GameEngine, ActionResult } from '../interfaces/engine';
import { WorldModel, GameState, Room, GameObject, Character } from '../interfaces/world-model';
import { ActionResponse } from '../interfaces/llm';
import { YamlWorldParser } from '../world-model/yaml-parser';
export class TextAdventureEngine implements GameEngine {
private worldModel: WorldModel | null = null;
private gameState: GameState | null = null;
private actionHandlers: Record<string, (state: GameState, world: WorldModel, action: ActionResponse) => ActionResult> = {};
constructor() {
this.registerDefaultActionHandlers();
}
/**
* Load a world model from a file
*/
public async loadWorld(worldModelPath: string): Promise<void> {
try {
this.worldModel = await YamlWorldParser.loadFromFile(worldModelPath);
this.gameState = { ...this.worldModel.initialState };
// Mark the initial room as visited
if (!this.gameState.visitedRooms.includes(this.gameState.currentRoomId)) {
this.gameState.visitedRooms.push(this.gameState.currentRoomId);
}
} catch (error) {
console.error(`Failed to load world from ${worldModelPath}:`, error);
throw new Error(`Could not load world: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get the current game state
*/
public getCurrentState(): GameState {
if (!this.gameState) {
throw new Error('Game state not initialized. Please load a world first.');
}
return { ...this.gameState };
}
/**
* Get the world model
*/
public getWorldModel(): WorldModel {
if (!this.worldModel) {
throw new Error('World model not initialized. Please load a world first.');
}
return this.worldModel;
}
/**
* Process an action from the player
*/
public processAction(action: ActionResponse): ActionResult {
if (!this.worldModel || !this.gameState) {
return {
success: false,
message: 'Game not initialized',
stateChanged: false
};
}
const handler = this.actionHandlers[action.action.toLowerCase()];
if (!handler) {
return {
success: false,
message: `I don't know how to "${action.action}"`,
stateChanged: false
};
}
return handler(this.gameState, this.worldModel, action);
}
/**
* Save the current game state to a file
*/
public async saveGame(filename: string): Promise<void> {
if (!this.gameState || !this.worldModel) {
throw new Error('Cannot save: game not initialized');
}
const saveData = {
worldModelName: this.worldModel.title,
worldModelVersion: this.worldModel.version,
timestamp: new Date().toISOString(),
gameState: this.gameState
};
try {
await fs.writeFile(filename, JSON.stringify(saveData, null, 2), 'utf8');
} catch (error) {
console.error(`Failed to save game to ${filename}:`, error);
throw new Error(`Could not save game: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Load a game state from a save file
*/
public async loadGame(filename: string): Promise<void> {
try {
const fileContents = await fs.readFile(filename, 'utf8');
const saveData = JSON.parse(fileContents);
// Check if the save file matches the current world model
if (!this.worldModel) {
throw new Error('World model not loaded');
}
if (saveData.worldModelName !== this.worldModel.title ||
saveData.worldModelVersion !== this.worldModel.version) {
throw new Error('Save file is for a different world or version');
}
// Load the game state
this.gameState = saveData.gameState;
} catch (error) {
console.error(`Failed to load game from ${filename}:`, error);
throw new Error(`Could not load save file: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Get a list of available actions in the current context
*/
public getAvailableActions(): string[] {
if (!this.worldModel) return [];
// Common actions always available
const availableActions = ['look', 'inventory', 'help'];
// Add movement actions based on current room exits
const currentRoom = this.getCurrentRoom();
if (currentRoom) {
currentRoom.exits.forEach(exit => {
availableActions.push(`go ${exit.direction.toLowerCase()}`);
});
}
// Add object interactions based on visible objects
const visibleObjects = this.getVisibleObjects();
const objects = this.worldModel.objects;
visibleObjects.forEach(objId => {
const obj = objects[objId];
if (obj) {
obj.allowedActions.forEach(action => {
availableActions.push(`${action} ${obj.name.toLowerCase()}`);
});
}
});
// Add character interactions
const visibleCharacters = this.getVisibleCharacters();
visibleCharacters.forEach(charId => {
availableActions.push(`talk to ${this.worldModel!.characters[charId].name.toLowerCase()}`);
});
// Add inventory object actions
this.gameState!.inventory.forEach(objId => {
const obj = objects[objId];
if (obj) {
obj.allowedActions.forEach(action => {
availableActions.push(`${action} ${obj.name.toLowerCase()}`);
});
}
});
return Array.from(new Set(availableActions)); // Remove duplicates
}
/**
* Get a list of visible objects in the current room
*/
public getVisibleObjects(): string[] {
if (!this.worldModel || !this.gameState) return [];
const currentRoom = this.getCurrentRoom();
if (!currentRoom) return [];
const visibleObjects: string[] = [...currentRoom.objects];
// Add objects from open containers
currentRoom.objects.forEach(objId => {
const obj = this.worldModel!.objects[objId];
if (obj && obj.traits.includes('container') && obj.states?.open && obj.containedObjects) {
visibleObjects.push(...obj.containedObjects);
}
});
return visibleObjects;
}
/**
* Get a list of visible characters in the current room
*/
public getVisibleCharacters(): string[] {
if (!this.worldModel || !this.gameState) return [];
const currentRoom = this.getCurrentRoom();
return currentRoom ? currentRoom.characters : [];
}
/**
* Get the description of the current room
*/
public getCurrentRoomDescription(): string {
const currentRoom = this.getCurrentRoom();
if (!currentRoom) return 'You are in a void. Something has gone wrong.';
return currentRoom.description;
}
/**
* Start the game and return the introduction text
*/
public async start(): Promise<string> {
if (!this.worldModel) {
throw new Error('World not loaded. Please load a world before starting.');
}
// Reset game state to initial state
this.gameState = { ...this.worldModel.initialState };
return this.worldModel.introduction;
}
/**
* End the game (cleanup resources if needed)
*/
public end(): void {
// Cleanup could happen here if needed
console.log('Game ended');
}
/**
* Get the current room object
*/
private getCurrentRoom(): Room | null {
if (!this.worldModel || !this.gameState) return null;
const roomId = this.gameState.currentRoomId;
return this.worldModel.rooms[roomId] || null;
}
/**
* Register default action handlers
*/
private registerDefaultActionHandlers(): void {
// Look action
this.actionHandlers['look'] = (state, world, action): ActionResult => {
const room = world.rooms[state.currentRoomId];
// If an object is specified, look at that object
if (action.object) {
// Try to find the object in the room or inventory
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, [...visibleObjects, ...state.inventory]);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
return {
success: true,
message: obj.description,
stateChanged: false
};
}
// Look at the room
const objectDescriptions = room.objects
.map(id => world.objects[id])
.map(obj => `You can see ${obj.name.toLowerCase()} here.`);
const characterDescriptions = room.characters
.map(id => world.characters[id])
.map(char => `${char.name} is here.`);
const exitDescriptions = room.exits
.map(exit => `There is an exit ${exit.direction.toLowerCase()}${exit.description ? ` (${exit.description})` : ''}.`);
const fullDescription = [
room.description,
...objectDescriptions,
...characterDescriptions,
...exitDescriptions
].join('\n');
return {
success: true,
message: fullDescription,
stateChanged: false
};
};
// Go action
this.actionHandlers['go'] = (state, world, action): ActionResult => {
const room = world.rooms[state.currentRoomId];
if (!action.object) {
return {
success: false,
message: 'Go where?',
stateChanged: false
};
}
// Find the exit that matches the direction
const direction = action.object.toLowerCase();
const exit = room.exits.find(e => e.direction.toLowerCase() === direction);
if (!exit) {
return {
success: false,
message: `You can't go ${direction} from here.`,
stateChanged: false
};
}
if (exit.isLocked) {
if (!exit.keyId) {
return {
success: false,
message: `The way ${direction} is locked.`,
stateChanged: false
};
}
if (!state.inventory.includes(exit.keyId)) {
return {
success: false,
message: `The way ${direction} is locked and you don't have the key.`,
stateChanged: false
};
}
// Player has the key, unlock the exit
exit.isLocked = false;
return {
success: true,
message: `You unlock the way ${direction} and proceed.`,
stateChanged: true,
newState: {
...state,
currentRoomId: exit.targetRoomId,
visitedRooms: state.visitedRooms.includes(exit.targetRoomId)
? state.visitedRooms
: [...state.visitedRooms, exit.targetRoomId]
}
};
}
// Exit is not locked, just move
return {
success: true,
message: `You go ${direction}.`,
stateChanged: true,
newState: {
...state,
currentRoomId: exit.targetRoomId,
visitedRooms: state.visitedRooms.includes(exit.targetRoomId)
? state.visitedRooms
: [...state.visitedRooms, exit.targetRoomId]
}
};
};
// Take action
this.actionHandlers['take'] = (state, world, action): ActionResult => {
if (!action.object) {
return {
success: false,
message: 'Take what?',
stateChanged: false
};
}
// Find the object in the current room
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, visibleObjects);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Check if the object can be taken
if (!obj.traits.includes('takeable')) {
return {
success: false,
message: `You can't take the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
}
// Remove object from room and add to inventory
const room = world.rooms[state.currentRoomId];
const newRoomObjects = room.objects.filter(id => id !== objId);
room.objects = newRoomObjects;
// Update state
return {
success: true,
message: `You take the ${obj.name.toLowerCase()}.`,
stateChanged: true,
newState: {
...state,
inventory: [...state.inventory, objId]
}
};
};
// Inventory action
this.actionHandlers['inventory'] = (state, world): ActionResult => {
if (state.inventory.length === 0) {
return {
success: true,
message: 'Your inventory is empty.',
stateChanged: false
};
}
const items = state.inventory
.map(id => world.objects[id])
.map(obj => obj.name)
.join(', ');
return {
success: true,
message: `You are carrying: ${items}.`,
stateChanged: false
};
};
// Drop action
this.actionHandlers['drop'] = (state, world, action): ActionResult => {
if (!action.object) {
return {
success: false,
message: 'Drop what?',
stateChanged: false
};
}
// Find the object in the inventory
const objId = this.findObjectByName(action.object, state.inventory);
if (!objId) {
return {
success: false,
message: `You don't have any ${action.object}.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Remove object from inventory and add to room
const room = world.rooms[state.currentRoomId];
room.objects.push(objId);
// Update state
return {
success: true,
message: `You drop the ${obj.name.toLowerCase()}.`,
stateChanged: true,
newState: {
...state,
inventory: state.inventory.filter(id => id !== objId)
}
};
};
// Use action
this.actionHandlers['use'] = (state, world, action): ActionResult => {
if (!action.object) {
return {
success: false,
message: 'Use what?',
stateChanged: false
};
}
// Find the object in inventory or visible objects
const visibleObjects = this.getVisibleObjects();
const objId = this.findObjectByName(action.object, [...state.inventory, ...visibleObjects]);
if (!objId) {
return {
success: false,
message: `You don't see any ${action.object} here.`,
stateChanged: false
};
}
const obj = world.objects[objId];
// Check if the object can be used
if (!obj.allowedActions.includes('use')) {
return {
success: false,
message: `You can't use the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
}
// Check if there's a target
if (action.target) {
const targetId = this.findObjectByName(action.target, [...state.inventory, ...visibleObjects]);
if (!targetId) {
return {
success: false,
message: `You don't see any ${action.target} here.`,
stateChanged: false
};
}
const target = world.objects[targetId];
// TODO: Implement object-specific use logic (could be extended with a more sophisticated system)
return {
success: true,
message: `You use the ${obj.name.toLowerCase()} on the ${target.name.toLowerCase()}.`,
stateChanged: false
};
}
// Simple use without target
return {
success: true,
message: `You use the ${obj.name.toLowerCase()}.`,
stateChanged: false
};
};
// Talk action
this.actionHandlers['talk'] = (state, world, action): ActionResult => {
if (!action.object) {
return {
success: false,
message: 'Talk to whom?',
stateChanged: false
};
}
// Find the character in the room
const visibleCharacters = this.getVisibleCharacters();
const charId = this.findCharacterByName(action.object, visibleCharacters);
if (!charId) {
return {
success: false,
message: `You don't see anyone called ${action.object} here.`,
stateChanged: false
};
}
const character = world.characters[charId];
// If a topic is provided
if (action.parameters?.topic) {
const topic = action.parameters.topic.toLowerCase();
const response = character.dialogue[topic] || character.defaultResponse;
return {
success: true,
message: `${character.name}: "${response}"`,
stateChanged: false
};
}
// No specific topic
return {
success: true,
message: `${character.name} looks ready to talk. You could ask about: ${Object.keys(character.dialogue).join(', ')}.`,
stateChanged: false
};
};
// Help action
this.actionHandlers['help'] = (): ActionResult => {
return {
success: true,
message: [
'Available commands:',
'- look: Examine your surroundings or a specific object',
'- go [direction]: Move in a direction',
'- take [object]: Pick up an object',
'- drop [object]: Put down an object',
'- inventory: Check what you\'re carrying',
'- use [object] (on [target]): Use an object, optionally on another object',
'- talk to [character] (about [topic]): Speak with a character',
'- help: Show this help text',
'',
'You can type commands in natural language. The AI will interpret your intent.'
].join('\n'),
stateChanged: false
};
};
// Examine action (alias for look)
this.actionHandlers['examine'] = this.actionHandlers['look'];
}
/**
* Find an object by name in a list of object IDs
*/
private findObjectByName(name: string, objectIds: string[]): string | null {
if (!this.worldModel) return null;
const normalizedName = name.toLowerCase();
for (const id of objectIds) {
const obj = this.worldModel.objects[id];
if (obj && obj.name.toLowerCase() === normalizedName) {
return id;
}
}
return null;
}
/**
* Find a character by name in a list of character IDs
*/
private findCharacterByName(name: string, characterIds: string[]): string | null {
if (!this.worldModel) return null;
const normalizedName = name.toLowerCase();
for (const id of characterIds) {
const character = this.worldModel.characters[id];
if (character && character.name.toLowerCase() === normalizedName) {
return id;
}
}
return null;
}
}
File diff suppressed because it is too large Load Diff
-88
View File
@@ -1,88 +0,0 @@
/**
* Main entry point for the AI Interactive Fiction application
*/
import * as path from 'path';
import * as dotenv from 'dotenv';
import { GameRunner } from './cli/game-runner';
// YAML CLI entry point. The web default is selected by scripts/run-engine.js.
import { startServer } from './server-yaml';
import { loadGameConfig, projectPath } from './config/game-config';
// Load environment variables
console.log('Loading environment variables...');
try {
const result = dotenv.config();
if (result.error) {
console.error('Error loading .env file:', result.error);
} else {
console.log('Environment variables loaded successfully');
}
} catch (error) {
console.error('Exception when loading env:', error);
}
async function main(): Promise<void> {
try {
console.log('=== AI Interactive Fiction ===');
console.log('A modern take on classic text adventures with LLM-powered interactions');
console.log('');
// Get the world file path from the YAML engine config, with environment override.
const engineConfig = loadGameConfig(
process.env.YAML_CONFIG_FILE || './config/engines/yaml.json',
'yaml',
);
const worldFile = projectPath(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
console.log(`Using world file: ${worldFile}`);
console.log(`OpenRouter API Key: ${process.env.OPENROUTER_API_KEY ? 'Found' : 'Missing'}`);
console.log(`OpenRouter Model: ${process.env.OPENROUTER_MODEL || 'Not specified'}`);
// Check if we should run in CLI mode
const args = process.argv.slice(2);
const cliMode = args.includes('--cli') || args.includes('-c');
if (cliMode) {
// CLI mode
console.log('Starting in CLI mode...');
// Create game runner and initialize
console.log('Creating game runner...');
const gameRunner = new GameRunner();
console.log('Initializing game...');
await gameRunner.initialize(worldFile);
// Start the CLI game
console.log('Starting CLI game...');
await gameRunner.start();
} else {
// Web interface mode - explicitly start the server with port fallback
console.log('Starting in web interface mode...');
// Get port configuration
const DEFAULT_PORT = 3000;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300;
// Start the web server with port fallback
console.log('Starting web server...');
await startServer(PORT, PORT_RANGE);
}
} catch (error) {
console.error('Failed to start:', error);
if (error instanceof Error) {
console.error('Error name:', error.name);
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
}
process.exit(1);
}
}
// Start the application
console.log('Starting application...');
main().catch(error => {
console.error('Unhandled error in main:', error);
process.exit(1);
});
-56
View File
@@ -1,56 +0,0 @@
/**
* Interfaces for the game engine
*/
import { WorldModel, GameState } from './world-model';
import { ActionResponse, NarrativeResponse } from './llm';
export interface ActionResult {
success: boolean;
message: string;
stateChanged: boolean;
newState?: GameState;
}
export interface GameEngine {
loadWorld(worldModelPath: string): Promise<void>;
getCurrentState(): GameState;
getWorldModel(): WorldModel;
// Action processing
processAction(action: ActionResponse): ActionResult;
// State management
saveGame(filename: string): Promise<void>;
loadGame(filename: string): Promise<void>;
// Helper methods for world interaction
getAvailableActions(): string[];
getVisibleObjects(): string[];
getVisibleCharacters(): string[];
getCurrentRoomDescription(): string;
// Game flow
start(): Promise<string>; // Returns introduction text
end(): void;
}
export interface GameSession {
engine: GameEngine;
history: {
playerInput: string;
actionResponse: ActionResponse;
actionResult: ActionResult;
narrativeResponse: NarrativeResponse;
}[];
startTime: Date;
lastInteractionTime: Date;
}
export interface ActionHandler {
execute(
gameState: GameState,
worldModel: WorldModel,
action: ActionResponse
): ActionResult;
}
-52
View File
@@ -1,52 +0,0 @@
/**
* Interfaces for LLM integration
*/
export interface LlmConfig {
apiKey: string;
model: string;
temperature?: number;
maxTokens?: number;
topP?: number;
frequencyPenalty?: number;
presencePenalty?: number;
}
export interface ActionRequest {
playerInput: string;
currentRoom: string;
visibleObjects: string[];
visibleCharacters: string[];
possibleActions: string[];
inventory: string[];
gameContext: string;
}
export interface ActionResponse {
action: string;
object?: string;
target?: string;
parameters?: Record<string, string>;
confidence: number;
}
export interface NarrativeRequest {
action: string;
result: string;
roomDescription: string;
visibleObjects: string[];
visibleCharacters: string[];
previousContext?: string;
tone?: string; // e.g., "mysterious", "humorous", "dramatic"
}
export interface NarrativeResponse {
text: string;
suggestions?: string[]; // Optional hints for the player
}
export interface LlmProvider {
initialize(config: LlmConfig): Promise<void>;
translateAction(request: ActionRequest): Promise<ActionResponse>;
generateNarrative(request: NarrativeRequest): Promise<NarrativeResponse>;
}
-68
View File
@@ -1,68 +0,0 @@
/**
* Core interfaces for the interactive fiction world model
*/
export interface Room {
id: string;
name: string;
description: string;
exits: Exit[];
objects: string[]; // References to object IDs
characters: string[]; // References to character IDs
}
export interface Exit {
direction: string;
targetRoomId: string;
description?: string;
isLocked?: boolean;
keyId?: string; // ID of the key object needed to unlock
}
export interface GameObject {
id: string;
name: string;
description: string;
traits: string[]; // e.g., "takeable", "container", "edible"
states: Record<string, boolean>; // e.g., { "open": false, "lit": true }
containedObjects?: string[]; // IDs of objects inside if this is a container
allowedActions: string[]; // What actions can be performed on this object
}
export interface Character {
id: string;
name: string;
description: string;
dialogue: Record<string, string>; // Topic -> response mapping
inventory: string[]; // IDs of objects the character has
defaultResponse: string; // Response when topic not found
mood?: string; // Current mood affecting responses
}
export interface Action {
name: string;
patterns: string[]; // Example natural language patterns this action matches
requiresObject?: boolean;
requiresTarget?: boolean;
handler: string; // Name of method to handle this action
}
export interface GameState {
currentRoomId: string;
inventory: string[]; // IDs of objects in player's inventory
visitedRooms: string[]; // IDs of rooms the player has visited
flags: Record<string, boolean>; // Game state flags
counters: Record<string, number>; // Game state counters
}
export interface WorldModel {
title: string;
author: string;
version: string;
introduction: string;
rooms: Record<string, Room>;
objects: Record<string, GameObject>;
characters: Record<string, Character>;
actions: Record<string, Action>;
initialState: GameState;
}
-212
View File
@@ -1,212 +0,0 @@
/**
* OpenRouter LLM Provider
* Handles communication with OpenRouter API for LLM interactions
*/
import axios, { AxiosInstance } from 'axios';
import {
LlmProvider,
LlmConfig,
ActionRequest,
ActionResponse,
NarrativeRequest,
NarrativeResponse
} from '../interfaces/llm';
export class OpenRouterProvider implements LlmProvider {
private apiKey: string = '';
private model: string = '';
private client!: AxiosInstance;
private temperature: number = 0.7;
private maxTokens: number = 800;
/**
* Initialize the OpenRouter provider with configuration
*/
public async initialize(config: LlmConfig): Promise<void> {
this.apiKey = config.apiKey;
this.model = config.model;
this.temperature = config.temperature ?? 0.7;
this.maxTokens = config.maxTokens ?? 800;
this.client = axios.create({
baseURL: 'https://openrouter.ai/api/v1',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
});
}
/**
* Translate player input into a structured action for the game engine
*/
public async translateAction(request: ActionRequest): Promise<ActionResponse> {
try {
const prompt = this.buildActionPrompt(request);
const response = await this.client.post('/chat/completions', {
model: this.model,
messages: [
{
role: 'system',
content: prompt.system
},
{
role: 'user',
content: prompt.user
}
],
temperature: 0.2, // Lower temperature for more deterministic outputs
max_tokens: 150,
response_format: { type: 'json_object' }
});
const content = response.data.choices[0].message.content;
const parsedResponse = JSON.parse(content);
return this.validateActionResponse(parsedResponse);
} catch (error) {
console.error('Error translating action:', error);
// Fallback to a simple "look" action when errors occur
return {
action: 'look',
confidence: 0.5
};
}
}
/**
* Generate narrative prose based on game events
*/
public async generateNarrative(request: NarrativeRequest): Promise<NarrativeResponse> {
try {
const prompt = this.buildNarrativePrompt(request);
const response = await this.client.post('/chat/completions', {
model: this.model,
messages: [
{
role: 'system',
content: prompt.system
},
{
role: 'user',
content: prompt.user
}
],
temperature: this.temperature,
max_tokens: this.maxTokens
});
const content = response.data.choices[0].message.content;
// Check if response is JSON format or plain text
try {
const parsedResponse = JSON.parse(content);
return {
text: parsedResponse.text,
suggestions: parsedResponse.suggestions || []
};
} catch {
// Plain text response, just use the content directly
return {
text: content
};
}
} catch (error) {
console.error('Error generating narrative:', error);
return {
text: `Something happened, but the narrator is at a loss for words. (Error: ${error instanceof Error ? error.message : String(error)})`
};
}
}
/**
* Build the system and user prompts for action translation
*/
private buildActionPrompt(request: ActionRequest): { system: string; user: string } {
const systemPrompt = `You are an AI assistant that translates natural language input into structured action commands for an interactive fiction game.
Your task is to convert player input into a JSON object representing an action that can be understood by the game engine.
The player is currently in the "${request.currentRoom}" room.
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}
Inventory: ${request.inventory.join(', ')}
Available actions: ${request.possibleActions.join(', ')}
Game context: ${request.gameContext}
Respond ONLY with a JSON object that follows this structure:
{
"action": "string", // Name of the action (e.g., "take", "examine", "go", "talk", etc.)
"object": "string", // Optional: Primary object of the action
"target": "string", // Optional: Secondary object/target of the action
"parameters": {}, // Optional: Additional parameters as key-value pairs
"confidence": number // How confident you are in this interpretation (0.0-1.0)
}
Choose the action from the list of available actions. If the player's input is ambiguous or doesn't map well to an available action, choose the closest match and set a lower confidence score.`;
const userPrompt = request.playerInput;
return {
system: systemPrompt,
user: userPrompt
};
}
/**
* Build the system and user prompts for narrative generation
*/
private buildNarrativePrompt(request: NarrativeRequest): { system: string; user: string } {
const tone = request.tone || 'descriptive';
const systemPrompt = `You are an AI assistant that generates engaging narrative prose for an interactive fiction game.
Your task is to describe what happens when a player performs an action in the game world.
Craft a vivid, ${tone} description that tells the player what happened as a result of their action. Make your prose engaging and atmospheric.
Current room description: "${request.roomDescription}"
Visible objects: ${request.visibleObjects.join(', ')}
Visible characters: ${request.visibleCharacters.join(', ')}
${request.previousContext ? `Previous context: ${request.previousContext}` : ''}
Respond with engaging prose that describes the outcome of the player's action.
You can optionally include 1-3 subtle hints about interesting things to try next.`;
const userPrompt = `The player has performed this action: "${request.action}".
The result of the action is: "${request.result}".
Please describe what happens in an engaging, narrative way.`;
return {
system: systemPrompt,
user: userPrompt
};
}
/**
* Validate and normalize the action response
*/
private validateActionResponse(response: Record<string, unknown>): ActionResponse {
const validatedResponse: ActionResponse = {
action: typeof response.action === 'string' ? response.action : 'look',
confidence: typeof response.confidence === 'number' ? response.confidence : 0.5
};
if (typeof response.object === 'string') {
validatedResponse.object = response.object;
}
if (typeof response.target === 'string') {
validatedResponse.target = response.target;
}
if (response.parameters && typeof response.parameters === 'object') {
validatedResponse.parameters = response.parameters as Record<string, string>;
}
return validatedResponse;
}
}
-321
View File
@@ -1,321 +0,0 @@
/**
* AI Interactive Fiction - Web Server
* Serves the web UI and handles WebSocket communication
*/
import path from 'path';
import express from 'express';
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
import * as dotenv from 'dotenv';
import { GameRunner } from './cli/game-runner';
import { existsSync, mkdirSync, copyFileSync } from 'fs';
import {
textToParagraphs,
TurnResult,
} from './interfaces/turn-result';
import {
clientGameConfig,
ensureConfiguredAssetDirectories,
loadGameConfig,
projectPath,
} from './config/game-config';
// Load environment variables
dotenv.config();
// Create Express application
const app = express();
const server = http.createServer(app);
const io = new SocketIOServer(server);
// Get port from environment variables or use default
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
const engineConfig = loadGameConfig(
process.env.YAML_CONFIG_FILE || './config/engines/yaml.json',
'yaml',
);
// Serve static files from the public directory. During local development the
// browser must not keep stale ES modules, otherwise UI fixes appear to do
// nothing until a hard cache clear.
app.use(express.static(path.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
app.get('/api/game-config', (_req, res) => {
res.json(clientGameConfig(engineConfig));
});
// Set up game sessions
const gameSessions = new Map<string, GameRunner>();
const nextTurnIds = new Map<string, number>();
function nextTurnId(socketId: string): number {
const current = nextTurnIds.get(socketId) || 1;
nextTurnIds.set(socketId, current + 1);
return current;
}
function createTextTurn(
socketId: string,
text: string,
gameState: TurnResult['gameState'] = {},
suggestions?: string[],
): TurnResult {
const paragraphs = textToParagraphs(text);
return {
turnId: nextTurnId(socketId),
paragraphs,
choices: [],
inputMode: 'text',
gameState,
suggestions,
};
}
function normalizeSaveSlot(slot: unknown): number {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
}
async function startDemoGameForSocket(socket: any): Promise<GameRunner> {
nextTurnIds.set(socket.id, 1);
const gameRunner = new GameRunner();
const worldFile = projectPath(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);
await gameRunner.initialize(worldFile);
gameSessions.set(socket.id, gameRunner);
const gameState = gameRunner.getGameState();
const paragraphs = [
...textToParagraphs(gameState.world.introduction),
...textToParagraphs(gameRunner.getCurrentRoomDescription()),
];
socket.emit('narrativeResponse', {
turnId: nextTurnId(socket.id),
paragraphs,
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: gameState.currentRoomId,
},
});
return gameRunner;
}
async function handleGameApi(socket: any, method: string, args: unknown[] = []) {
const saveGames: Map<number, any> = socket.data.saveGames || new Map<number, any>();
socket.data.saveGames = saveGames;
switch (method) {
case 'newGame':
case 'newGame()':
await startDemoGameForSocket(socket);
return { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!saveGames.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
await startDemoGameForSocket(socket);
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'saveGame':
case 'saveGame()': {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
saveGames.set(slot, gameRunner.getGameState());
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: saveGames.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return { success: true, result: Array.from(saveGames.keys()).sort((a, b) => a - b) };
case 'isGameRunning':
case 'isGameRunning()':
return { success: true, result: gameSessions.has(socket.id) };
default:
return { success: false, error: `unknown_method:${method}` };
}
}
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
socket.emit('gameConfig', clientGameConfig(engineConfig));
socket.data.saveGames = new Map<number, any>();
socket.on('gameApi', async (request, respond) => {
try {
const response = await handleGameApi(socket, String(request?.method || ''), Array.isArray(request?.args) ? request.args : []);
if (typeof respond === 'function') {
respond(response);
}
} catch (error) {
console.error('Game API error:', error);
if (typeof respond === 'function') {
respond({ success: false, error: error instanceof Error ? error.message : String(error) });
}
}
});
// Process player command
socket.on('playerCommand', async (data) => {
try {
const gameRunner = gameSessions.get(socket.id);
if (!gameRunner) {
socket.emit('error', { message: 'Game session not found. Please start a new game.' });
return;
}
const command = String(data?.command || '').trim();
// During typography and animation work, mirror the command back through
// the real socket path so the UI pipeline can be tested end to end.
socket.emit('narrativeResponse', createTextTurn(socket.id, command, {
currentRoomId: gameRunner.getGameState().currentRoomId
}, gameRunner.getSuggestions()));
} catch (error) {
console.error('Error processing command:', error);
socket.emit('error', { message: 'Failed to process command. Please try again.' });
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
// Clean up game session
if (gameSessions.has(socket.id)) {
gameSessions.delete(socket.id);
}
nextTurnIds.delete(socket.id);
});
});
// Ensure required asset folders exist
function ensureDirectories() {
const dirs = [
path.join(__dirname, '../public'),
path.join(__dirname, '../public/js'),
path.join(__dirname, '../public/css'),
path.join(__dirname, '../public/images'),
path.join(__dirname, '../public/music'),
path.join(__dirname, '../public/sounds'),
path.join(__dirname, '../public/fonts')
];
for (const dir of dirs) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}
ensureConfiguredAssetDirectories(engineConfig);
}
// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
const source = path.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const destination = path.join(__dirname, '../public/js/kokoro-js.js');
if (existsSync(source) && !existsSync(destination)) {
copyFileSync(source, destination);
console.log(`Copied kokoro-js from ${source} to ${destination}`);
}
}
// Start the server with port fallback
export async function startServer(initialPort: number, range: number): Promise<void> {
let currentPort = initialPort;
const maxPort = initialPort + range;
// Try ports in the specified range
while (currentPort < maxPort) {
try {
// Ensure directories exist
ensureDirectories();
// Ensure kokoro-js is copied
try {
ensureKokoroJs();
} catch (error) {
console.error('Error copying kokoro-js:', error);
}
// Try to start the server on the current port
await new Promise<void>((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`AI Interactive Fiction web server running on http://localhost:${currentPort}`);
resolve();
});
server.once('error', (error: NodeJS.ErrnoException) => {
// If port is in use, try next port
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
server.close();
currentPort++;
reject();
} else {
// For other errors, log and reject
console.error('Server error:', error);
reject(error);
}
});
server.listen(currentPort);
});
// If we reach here, server started successfully
return;
} catch (error) {
// If we reach the max port and still fail, throw an error
if (currentPort >= maxPort - 1) {
throw new Error(`Failed to start server on ports ${initialPort} to ${maxPort - 1}`);
}
// Otherwise try the next port
// The loop continues as the rejection above increments currentPort
}
}
}
// Start the server when this module is run directly
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch(error => {
console.error('Failed to start server:', error);
process.exit(1);
});
}
export { app, server, io };
-375
View File
@@ -1,375 +0,0 @@
/**
* Z-code LLM Server
*
* Starts an Express + Socket.IO server that runs Zork I through the
* ZcodeLlmEngine and serves the same shared client UI as the YAML engine.
*
* Usage:
* npm run dev:zcode (development, with file watching)
* npm run start:zcode (production, from compiled dist/)
*
* Environment variables:
* PORT HTTP port (default: 3002)
* ZCODE_STORY_FILE path to the story file (default: ./data/z-code/zork1.bin)
* OPENROUTER_API_KEY, OPENROUTER_MODEL required
*/
import path from 'path';
import http from 'http';
import express from 'express';
import { Server as SocketIOServer } from 'socket.io';
import * as dotenv from 'dotenv';
import { existsSync, mkdirSync, copyFileSync } from 'fs';
import { ZcodeLlmEngine, ZcodeTurnResult } from './engine/zcode-llm-engine';
import {
clientGameConfig,
ensureConfiguredAssetDirectories,
loadGameConfig,
projectPath,
} from './config/game-config';
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = new SocketIOServer(server);
const DEFAULT_PORT = 3002;
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : DEFAULT_PORT;
const PORT_RANGE = 300;
const DEBUG_ENABLED = /^(1|true|yes|on)$/i.test(process.env.ZCODE_DEBUG ?? '');
const engineConfig = loadGameConfig(
process.env.ZCODE_CONFIG_FILE || './config/engines/zcode.json',
'zcode',
);
function debugLog(message: string, details?: unknown): void {
if (!DEBUG_ENABLED) return;
if (typeof details === 'undefined') {
console.log(`[zcode:debug] ${message}`);
return;
}
console.log(`[zcode:debug] ${message}`, details);
}
// Serve the same shared client UI
app.use(
express.static(path.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader(
'Cache-Control',
'no-store, no-cache, must-revalidate, proxy-revalidate',
);
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
},
}),
);
app.get('/api/game-config', (_req, res) => {
res.json(clientGameConfig(engineConfig));
});
// One engine instance per connected socket
const sessions = new Map<string, ZcodeLlmEngine>();
// Save-game slot maps: socketId → Map<slotNumber, serialisedJson>
const saveSlots = new Map<string, Map<number, string>>();
function toClientTurn(turn: ZcodeTurnResult): ZcodeTurnResult {
return {
...turn,
gameState: {
...turn.gameState,
currentRoomId: turn.gameState?.statusLine,
statusLine: turn.gameState?.statusLine,
},
};
}
function normalizeSaveSlot(slot: unknown): number {
const n = Number(slot);
return Number.isInteger(n) && n > 0 ? n : 1;
}
function getOrCreateEngine(socketId: string): ZcodeLlmEngine {
let engine = sessions.get(socketId);
if (!engine) {
engine = new ZcodeLlmEngine({
storyPath: projectPath(process.env.ZCODE_STORY_FILE || engineConfig.paths.mainGameFile),
promptDir: projectPath(engineConfig.paths.promptDir || 'data/zcode-prompts'),
});
sessions.set(socketId, engine);
}
return engine;
}
function getSlots(socketId: string): Map<number, string> {
let slots = saveSlots.get(socketId);
if (!slots) {
slots = new Map();
saveSlots.set(socketId, slots);
}
return slots;
}
async function handleGameApi(
socket: ReturnType<SocketIOServer['sockets']['sockets']['get']> & {
id: string;
},
method: string,
args: unknown[],
): Promise<object> {
const slots = getSlots(socket.id);
debugLog(`gameApi request from ${socket.id}: ${method}`, { args });
switch (method) {
case 'newGame':
case 'newGame()': {
const engine = getOrCreateEngine(socket.id);
const turn = await engine.newGame();
socket.emit('narrativeResponse', toClientTurn(turn));
return {
success: true,
result: true,
running: true,
canLoad: slots.size > 0,
};
}
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!slots.has(slot)) {
return { success: false, error: 'missing_save', result: false };
}
const engine = getOrCreateEngine(socket.id);
const turn = await engine.loadGame(slots.get(slot)!);
socket.emit('narrativeResponse', toClientTurn(turn));
socket.emit('gameLoaded', { slot });
return { success: true, result: true, running: true, slot };
}
case 'saveGame':
case 'saveGame()': {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
return { success: false, error: 'game_not_running', result: false };
}
const slot = normalizeSaveSlot(args[0]);
const savedJson = await engine.saveGame();
slots.set(slot, savedJson);
socket.emit('gameSaved', { slot });
return { success: true, result: true, slot };
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
return { success: true, result: slots.has(slot), slot };
}
case 'getSaveGames':
case 'getSaveGames()':
return {
success: true,
result: Array.from(slots.keys()).sort((a, b) => a - b),
};
case 'isGameRunning':
case 'isGameRunning()':
return {
success: true,
result: sessions.get(socket.id)?.isRunning() ?? false,
};
default:
return { success: false, error: `unknown_method:${method}` };
}
}
function checkRuntimeConfiguration(): void {
const storyPath = projectPath(process.env.ZCODE_STORY_FILE ?? engineConfig.paths.mainGameFile);
const promptDir = projectPath(engineConfig.paths.promptDir || 'data/zcode-prompts');
const promptFiles = [
'character-generation.yml',
'text-rewriter.yml',
'command-translator.yml',
'output-evaluator.yml',
];
const missingPrompts = promptFiles
.map((file) => path.join(promptDir, file))
.filter((filePath) => !existsSync(filePath));
if (!process.env.OPENROUTER_API_KEY) {
console.error('[zcode] Missing OPENROUTER_API_KEY in environment.');
}
if (!process.env.OPENROUTER_MODEL) {
console.error('[zcode] Missing OPENROUTER_MODEL in environment.');
}
if (!existsSync(storyPath)) {
console.error(`[zcode] Story file missing: ${storyPath}`);
console.error('[zcode] Place zork1.bin in ./data/z-code/ or set ZCODE_STORY_FILE.');
}
if (missingPrompts.length > 0) {
console.error('[zcode] Missing prompt files:');
for (const filePath of missingPrompts) {
console.error(` - ${filePath}`);
}
}
debugLog('runtime configuration', {
storyPath,
promptDir,
debug: DEBUG_ENABLED,
hasApiKey: Boolean(process.env.OPENROUTER_API_KEY),
model: process.env.OPENROUTER_MODEL ?? null,
});
}
io.on('connection', (socket) => {
console.log(`[zcode] Client connected: ${socket.id}`);
socket.emit('gameConfig', clientGameConfig(engineConfig));
socket.on(
'gameApi',
async (
request: { method?: string; args?: unknown[] },
respond: (result: object) => void,
) => {
try {
const result = await handleGameApi(
socket as Parameters<typeof handleGameApi>[0],
String(request?.method ?? ''),
Array.isArray(request?.args) ? request.args : [],
);
debugLog(`gameApi response to ${socket.id}`, result);
if (typeof respond === 'function') respond(result);
} catch (error) {
console.error('[zcode] gameApi error:', error);
if (typeof respond === 'function') {
respond({
success: false,
error: error instanceof Error ? error.message : String(error),
});
}
}
},
);
socket.on(
'playerCommand',
async (data: { command?: string }) => {
const engine = sessions.get(socket.id);
if (!engine?.isRunning()) {
socket.emit('error', {
message: 'No active game. Start or load a game first.',
});
return;
}
const input = String(data?.command ?? '').trim();
if (!input) return;
debugLog(`playerCommand from ${socket.id}: ${input}`);
try {
const turn: ZcodeTurnResult = await engine.processInput(input);
debugLog(`narrativeResponse to ${socket.id}`, {
inputMode: turn.inputMode,
paragraphs: turn.paragraphs.length,
statusLine: turn.gameState?.statusLine,
});
socket.emit('narrativeResponse', toClientTurn(turn));
} catch (error) {
console.error('[zcode] playerCommand error:', error);
socket.emit('error', {
message:
error instanceof Error ? error.message : 'An error occurred.',
});
}
},
);
socket.on('disconnect', () => {
console.log(`[zcode] Client disconnected: ${socket.id}`);
sessions.delete(socket.id);
saveSlots.delete(socket.id);
});
});
// ---------------------------------------------------------------------------
// Startup helpers
// ---------------------------------------------------------------------------
function ensureDirectories(): void {
const dirs = [
path.join(__dirname, '../public'),
path.join(__dirname, '../public/js'),
path.join(__dirname, '../public/css'),
path.join(__dirname, '../public/images'),
path.join(__dirname, '../public/music'),
path.join(__dirname, '../public/sounds'),
path.join(__dirname, '../public/fonts'),
path.join(__dirname, '../data/z-code'),
path.join(__dirname, '../data/zcode-prompts'),
];
for (const dir of dirs) {
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
}
ensureConfiguredAssetDirectories(engineConfig);
}
function ensureKokoroJs(): void {
const src = path.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const dst = path.join(__dirname, '../public/js/kokoro-js.js');
if (existsSync(src) && !existsSync(dst)) copyFileSync(src, dst);
}
async function startServer(initialPort: number, range: number): Promise<void> {
ensureDirectories();
try { ensureKokoroJs(); } catch { /* optional */ }
checkRuntimeConfiguration();
let port = initialPort;
while (port < initialPort + range) {
try {
await new Promise<void>((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(
`[zcode] Z-code Narrator server running on http://localhost:${port}`,
);
resolve();
});
server.once('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
console.log(`Port ${port} unavailable (${err.code}), trying ${port + 1}...`);
server.close();
port++;
reject();
} else {
reject(err);
}
});
server.listen(port);
});
return;
} catch {
if (port >= initialPort + range - 1) {
throw new Error(
`Failed to start server on ports ${initialPort}${initialPort + range - 1}`,
);
}
}
}
}
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch((err) => {
console.error('[zcode] Failed to start:', err);
process.exit(1);
});
}
-263
View File
@@ -1,263 +0,0 @@
/**
* Test Server for AI Interactive Fiction
* Simplified version that sends test paragraphs instead of using LLM
*/
import path from 'path';
import express from 'express';
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
import * as dotenv from 'dotenv';
import { existsSync, mkdirSync, copyFileSync } from 'fs';
import { textToParagraphs } from './interfaces/turn-result';
// Load environment variables
dotenv.config();
// Create Express application
const app = express();
const server = http.createServer(app);
const io = new SocketIOServer(server);
// Get port from environment variables or use default
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
// Serve static files from the public directory. Keep browser modules uncached
// during local development so fixes are visible without a hard cache clear.
app.use(express.static(path.join(__dirname, '../public'), {
etag: false,
lastModified: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
// Test paragraphs to send to the client
const TEST_PARAGRAPHS = [
"You stand at the entrance of a mysterious cave. The air is cool and damp, carrying the scent of earth and ancient stone. Shadows dance on the walls as your torch flickers in the gentle breeze.",
"As you venture deeper, the passage narrows. Stalactites hang from the ceiling like stone daggers, their surfaces glistening with moisture. The sound of dripping water echoes through the silence.",
"Suddenly, the passage opens into a vast chamber. Crystal formations catch the light of your torch, sending rainbow reflections across the walls. In the center of the room stands an ancient stone pedestal, its surface carved with symbols from a forgotten language."
];
// Handle socket connections
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
let currentParagraphIndex = 0;
let gameRunning = false;
let nextTurnId = 1;
const saveGames = new Set<number>();
const startDemoGame = () => {
gameRunning = true;
nextTurnId = 1;
currentParagraphIndex = 0;
socket.emit('narrativeResponse', {
turnId: nextTurnId++,
paragraphs: [
...textToParagraphs("#chapter[Interactive Fiction Test]\n\nWelcome to the Interactive Fiction Test. This is a simplified version that sends predefined paragraphs instead of using an LLM."),
...textToParagraphs(TEST_PARAGRAPHS[0]),
],
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: 'test-room',
},
});
};
const normalizeSaveSlot = (slot: unknown): number => {
const value = Number(slot);
return Number.isInteger(value) && value > 0 ? value : 1;
};
socket.on('gameApi', (request, respond) => {
try {
const method = String(request?.method || '');
const args = Array.isArray(request?.args) ? request.args : [];
let response: any;
switch (method) {
case 'newGame':
case 'newGame()':
startDemoGame();
response = { success: true, result: true, running: true, canLoad: saveGames.size > 0 };
break;
case 'loadGame':
case 'loadGame()': {
const slot = normalizeSaveSlot(args[0]);
if (!saveGames.has(slot)) {
response = { success: false, error: 'missing_save', result: false };
break;
}
startDemoGame();
socket.emit('gameLoaded', { slot });
response = { success: true, result: true, running: true, slot };
break;
}
case 'saveGame':
case 'saveGame()': {
if (!gameRunning) {
response = { success: false, error: 'game_not_running', result: false };
break;
}
const slot = normalizeSaveSlot(args[0]);
saveGames.add(slot);
socket.emit('gameSaved', { slot });
response = { success: true, result: true, slot };
break;
}
case 'hasSaveGame':
case 'hasSaveGame()': {
const slot = normalizeSaveSlot(args[0]);
response = { success: true, result: saveGames.has(slot), slot };
break;
}
case 'getSaveGames':
case 'getSaveGames()':
response = { success: true, result: Array.from(saveGames).sort((a, b) => a - b) };
break;
case 'isGameRunning':
case 'isGameRunning()':
response = { success: true, result: gameRunning };
break;
default:
response = { success: false, error: `unknown_method:${method}` };
}
if (typeof respond === 'function') respond(response);
} catch (error) {
if (typeof respond === 'function') {
respond({ success: false, error: error instanceof Error ? error.message : String(error) });
}
}
});
// Process player command
socket.on('playerCommand', async (data) => {
try {
console.log(`Received command: ${data.command}`);
// Send narrative response to client
socket.emit('narrativeResponse', {
turnId: nextTurnId++,
paragraphs: textToParagraphs(String(data.command || '')),
choices: [],
inputMode: 'text',
gameState: {
currentRoomId: "test-room"
},
suggestions: ["look around", "examine pedestal", "touch crystals"]
});
} catch (error) {
console.error('Error processing command:', error);
socket.emit('error', { message: 'Failed to process command. Please try again.' });
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
// Ensure required asset folders exist
function ensureDirectories() {
const dirs = [
path.join(__dirname, '../public'),
path.join(__dirname, '../public/js'),
path.join(__dirname, '../public/css'),
path.join(__dirname, '../public/images'),
path.join(__dirname, '../public/music'),
path.join(__dirname, '../public/sounds'),
path.join(__dirname, '../public/fonts')
];
for (const dir of dirs) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}
}
// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
const source = path.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
const destination = path.join(__dirname, '../public/js/kokoro-js.js');
if (existsSync(source) && !existsSync(destination)) {
copyFileSync(source, destination);
console.log(`Copied kokoro-js from ${source} to ${destination}`);
}
}
// Start the server with port fallback
async function startServer(initialPort: number, range: number): Promise<void> {
let currentPort = initialPort;
const maxPort = initialPort + range;
// Try ports in the specified range
while (currentPort < maxPort) {
try {
// Ensure directories exist
ensureDirectories();
// Ensure kokoro-js is copied
try {
ensureKokoroJs();
} catch (error) {
console.error('Error copying kokoro-js:', error);
}
// Try to start the server on the current port
await new Promise<void>((resolve, reject) => {
server.removeAllListeners('error');
server.removeAllListeners('listening');
server.once('listening', () => {
console.log(`AI Interactive Fiction TEST SERVER running on http://localhost:${currentPort}`);
console.log('This server is sending predefined test paragraphs instead of using an LLM');
resolve();
});
server.once('error', (error: NodeJS.ErrnoException) => {
// If port is in use, try next port
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
server.close();
currentPort++;
reject();
} else {
// For other errors, log and reject
console.error('Server error:', error);
reject(error);
}
});
server.listen(currentPort);
});
// If we reach here, server started successfully
return;
} catch (error) {
// If we reach the max port and still fail, throw an error
if (currentPort >= maxPort - 1) {
throw new Error(`Failed to start server on ports ${initialPort} to ${maxPort - 1}`);
}
// Otherwise try the next port
}
}
}
// Start the server when this module is run directly
if (require.main === module) {
startServer(PORT, PORT_RANGE).catch(error => {
console.error('Failed to start server:', error);
process.exit(1);
});
}
export { app, server, io };
-429
View File
@@ -1,429 +0,0 @@
/**
* YAML World Model Parser
* Loads and validates world definitions from YAML files
*/
import * as fs from 'fs/promises';
import * as yaml from 'js-yaml';
import { WorldModel } from '../interfaces/world-model';
export class YamlWorldParser {
/**
* Load a world model from a YAML file
*/
public static async loadFromFile(filePath: string): Promise<WorldModel> {
try {
const fileContents = await fs.readFile(filePath, 'utf8');
const worldData = yaml.load(fileContents) as unknown;
return this.validateAndTransform(worldData);
} catch (error) {
console.error(`Error loading world from ${filePath}:`, error);
throw new Error(`Failed to load world from ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Validate the loaded YAML data and transform it into a WorldModel
*/
private static validateAndTransform(data: unknown): WorldModel {
if (!data || typeof data !== 'object') {
throw new Error('Invalid world data: must be an object');
}
const worldData = data as Record<string, unknown>;
// Validate required top-level fields
this.validateRequiredFields(worldData, ['title', 'author', 'version', 'introduction', 'rooms', 'initialState']);
// Transform and validate the world model
const worldModel: WorldModel = {
title: this.validateString(worldData.title, 'title'),
author: this.validateString(worldData.author, 'author'),
version: this.validateString(worldData.version, 'version'),
introduction: this.validateString(worldData.introduction, 'introduction'),
rooms: this.validateRooms(worldData.rooms),
objects: this.validateObjects(worldData.objects),
characters: this.validateCharacters(worldData.characters),
actions: this.validateActions(worldData.actions),
initialState: this.validateInitialState(worldData.initialState)
};
// Validate references between entities
this.validateReferences(worldModel);
return worldModel;
}
/**
* Validate that an object has all required fields
*/
private static validateRequiredFields(data: Record<string, unknown>, requiredFields: string[]): void {
for (const field of requiredFields) {
if (!(field in data)) {
throw new Error(`Missing required field: ${field}`);
}
}
}
/**
* Validate that a value is a string
*/
private static validateString(value: unknown, fieldName: string): string {
if (typeof value !== 'string') {
throw new Error(`Field ${fieldName} must be a string`);
}
return value;
}
/**
* Validate room definitions
*/
private static validateRooms(rooms: unknown): WorldModel['rooms'] {
if (!rooms || typeof rooms !== 'object') {
throw new Error('Rooms must be an object mapping room IDs to room definitions');
}
const roomsData = rooms as Record<string, unknown>;
const validatedRooms: WorldModel['rooms'] = {};
for (const [roomId, roomData] of Object.entries(roomsData)) {
if (!roomData || typeof roomData !== 'object') {
throw new Error(`Room ${roomId} must be an object`);
}
const room = roomData as Record<string, unknown>;
this.validateRequiredFields(room, ['name', 'description', 'exits']);
validatedRooms[roomId] = {
id: roomId,
name: this.validateString(room.name, `rooms.${roomId}.name`),
description: this.validateString(room.description, `rooms.${roomId}.description`),
exits: this.validateExits(room.exits, roomId),
objects: this.validateStringArray(room.objects || [], `rooms.${roomId}.objects`),
characters: this.validateStringArray(room.characters || [], `rooms.${roomId}.characters`)
};
}
return validatedRooms;
}
/**
* Validate exit definitions
*/
private static validateExits(exits: unknown, roomId: string): WorldModel['rooms'][string]['exits'] {
if (!Array.isArray(exits)) {
throw new Error(`Exits for room ${roomId} must be an array`);
}
return exits.map((exit, index) => {
if (!exit || typeof exit !== 'object') {
throw new Error(`Exit ${index} in room ${roomId} must be an object`);
}
const exitData = exit as Record<string, unknown>;
this.validateRequiredFields(exitData, ['direction', 'targetRoomId']);
return {
direction: this.validateString(exitData.direction, `rooms.${roomId}.exits[${index}].direction`),
targetRoomId: this.validateString(exitData.targetRoomId, `rooms.${roomId}.exits[${index}].targetRoomId`),
description: exitData.description ? this.validateString(exitData.description, `rooms.${roomId}.exits[${index}].description`) : undefined,
isLocked: typeof exitData.isLocked === 'boolean' ? exitData.isLocked : false,
keyId: exitData.keyId ? this.validateString(exitData.keyId, `rooms.${roomId}.exits[${index}].keyId`) : undefined
};
});
}
/**
* Validate object definitions
*/
private static validateObjects(objects: unknown): WorldModel['objects'] {
if (!objects) return {}; // Objects are optional
if (typeof objects !== 'object') {
throw new Error('Objects must be an object mapping object IDs to object definitions');
}
const objectsData = objects as Record<string, unknown>;
const validatedObjects: WorldModel['objects'] = {};
for (const [objectId, objectData] of Object.entries(objectsData)) {
if (!objectData || typeof objectData !== 'object') {
throw new Error(`Object ${objectId} must be an object`);
}
const obj = objectData as Record<string, unknown>;
this.validateRequiredFields(obj, ['name', 'description', 'traits', 'allowedActions']);
validatedObjects[objectId] = {
id: objectId,
name: this.validateString(obj.name, `objects.${objectId}.name`),
description: this.validateString(obj.description, `objects.${objectId}.description`),
traits: this.validateStringArray(obj.traits, `objects.${objectId}.traits`),
states: this.validateObjectStates(obj.states, objectId),
allowedActions: this.validateStringArray(obj.allowedActions, `objects.${objectId}.allowedActions`),
containedObjects: obj.containedObjects ? this.validateStringArray(obj.containedObjects, `objects.${objectId}.containedObjects`) : []
};
}
return validatedObjects;
}
/**
* Validate character definitions
*/
private static validateCharacters(characters: unknown): WorldModel['characters'] {
if (!characters) return {}; // Characters are optional
if (typeof characters !== 'object') {
throw new Error('Characters must be an object mapping character IDs to character definitions');
}
const charactersData = characters as Record<string, unknown>;
const validatedCharacters: WorldModel['characters'] = {};
for (const [characterId, characterData] of Object.entries(charactersData)) {
if (!characterData || typeof characterData !== 'object') {
throw new Error(`Character ${characterId} must be an object`);
}
const character = characterData as Record<string, unknown>;
this.validateRequiredFields(character, ['name', 'description', 'dialogue', 'defaultResponse']);
validatedCharacters[characterId] = {
id: characterId,
name: this.validateString(character.name, `characters.${characterId}.name`),
description: this.validateString(character.description, `characters.${characterId}.description`),
dialogue: this.validateDialogue(character.dialogue, characterId),
inventory: this.validateStringArray(character.inventory || [], `characters.${characterId}.inventory`),
defaultResponse: this.validateString(character.defaultResponse, `characters.${characterId}.defaultResponse`),
mood: character.mood ? this.validateString(character.mood, `characters.${characterId}.mood`) : undefined
};
}
return validatedCharacters;
}
/**
* Validate action definitions
*/
private static validateActions(actions: unknown): WorldModel['actions'] {
if (!actions) return {}; // Actions are optional
if (typeof actions !== 'object') {
throw new Error('Actions must be an object mapping action names to action definitions');
}
const actionsData = actions as Record<string, unknown>;
const validatedActions: WorldModel['actions'] = {};
for (const [actionName, actionData] of Object.entries(actionsData)) {
if (!actionData || typeof actionData !== 'object') {
throw new Error(`Action ${actionName} must be an object`);
}
const action = actionData as Record<string, unknown>;
this.validateRequiredFields(action, ['patterns', 'handler']);
validatedActions[actionName] = {
name: actionName,
patterns: this.validateStringArray(action.patterns, `actions.${actionName}.patterns`),
requiresObject: typeof action.requiresObject === 'boolean' ? action.requiresObject : false,
requiresTarget: typeof action.requiresTarget === 'boolean' ? action.requiresTarget : false,
handler: this.validateString(action.handler, `actions.${actionName}.handler`)
};
}
return validatedActions;
}
/**
* Validate initial game state
*/
private static validateInitialState(initialState: unknown): WorldModel['initialState'] {
if (!initialState || typeof initialState !== 'object') {
throw new Error('Initial state must be an object');
}
const stateData = initialState as Record<string, unknown>;
this.validateRequiredFields(stateData, ['currentRoomId']);
return {
currentRoomId: this.validateString(stateData.currentRoomId, 'initialState.currentRoomId'),
inventory: this.validateStringArray(stateData.inventory || [], 'initialState.inventory'),
visitedRooms: this.validateStringArray(stateData.visitedRooms || [], 'initialState.visitedRooms'),
flags: this.validateFlags(stateData.flags),
counters: this.validateCounters(stateData.counters)
};
}
/**
* Validate object states (record of boolean values)
*/
private static validateObjectStates(states: unknown, objectId: string): Record<string, boolean> {
if (!states) return {};
if (typeof states !== 'object') {
throw new Error(`States for object ${objectId} must be an object`);
}
const statesData = states as Record<string, unknown>;
const validatedStates: Record<string, boolean> = {};
for (const [stateName, stateValue] of Object.entries(statesData)) {
if (typeof stateValue !== 'boolean') {
throw new Error(`State ${stateName} for object ${objectId} must be a boolean value`);
}
validatedStates[stateName] = stateValue;
}
return validatedStates;
}
/**
* Validate dialogue (record of string values)
*/
private static validateDialogue(dialogue: unknown, characterId: string): Record<string, string> {
if (!dialogue || typeof dialogue !== 'object') {
throw new Error(`Dialogue for character ${characterId} must be an object`);
}
const dialogueData = dialogue as Record<string, unknown>;
const validatedDialogue: Record<string, string> = {};
for (const [topic, response] of Object.entries(dialogueData)) {
validatedDialogue[topic] = this.validateString(response, `characters.${characterId}.dialogue.${topic}`);
}
return validatedDialogue;
}
/**
* Validate flags (record of boolean values)
*/
private static validateFlags(flags: unknown): Record<string, boolean> {
if (!flags) return {};
if (typeof flags !== 'object') {
throw new Error('Flags must be an object');
}
const flagsData = flags as Record<string, unknown>;
const validatedFlags: Record<string, boolean> = {};
for (const [flagName, flagValue] of Object.entries(flagsData)) {
if (typeof flagValue !== 'boolean') {
throw new Error(`Flag ${flagName} must be a boolean value`);
}
validatedFlags[flagName] = flagValue;
}
return validatedFlags;
}
/**
* Validate counters (record of number values)
*/
private static validateCounters(counters: unknown): Record<string, number> {
if (!counters) return {};
if (typeof counters !== 'object') {
throw new Error('Counters must be an object');
}
const countersData = counters as Record<string, unknown>;
const validatedCounters: Record<string, number> = {};
for (const [counterName, counterValue] of Object.entries(countersData)) {
if (typeof counterValue !== 'number') {
throw new Error(`Counter ${counterName} must be a numeric value`);
}
validatedCounters[counterName] = counterValue;
}
return validatedCounters;
}
/**
* Validate that an array of strings is valid
*/
private static validateStringArray(arr: unknown, fieldName: string): string[] {
if (!arr) return [];
if (!Array.isArray(arr)) {
throw new Error(`Field ${fieldName} must be an array`);
}
return arr.map((item, index) => {
if (typeof item !== 'string') {
throw new Error(`Item at index ${index} in ${fieldName} must be a string`);
}
return item;
});
}
/**
* Validate references between entities
*/
private static validateReferences(worldModel: WorldModel): void {
const { rooms, objects, characters, initialState } = worldModel;
// Check that the initial room exists
if (!rooms[initialState.currentRoomId]) {
throw new Error(`Initial room ${initialState.currentRoomId} does not exist`);
}
// Check room exits
for (const [roomId, room] of Object.entries(rooms)) {
for (const exit of room.exits) {
if (!rooms[exit.targetRoomId]) {
throw new Error(`Room ${roomId} has an exit to non-existent room ${exit.targetRoomId}`);
}
if (exit.keyId && !objects[exit.keyId]) {
throw new Error(`Room ${roomId} has an exit requiring non-existent key ${exit.keyId}`);
}
}
// Check room objects
for (const objectId of room.objects) {
if (!objects[objectId]) {
throw new Error(`Room ${roomId} contains non-existent object ${objectId}`);
}
}
// Check room characters
for (const characterId of room.characters) {
if (!characters[characterId]) {
throw new Error(`Room ${roomId} contains non-existent character ${characterId}`);
}
}
}
// Check object containment
for (const [objectId, object] of Object.entries(objects)) {
if (object.containedObjects) {
for (const containedId of object.containedObjects) {
if (!objects[containedId]) {
throw new Error(`Object ${objectId} contains non-existent object ${containedId}`);
}
}
}
}
// Check character inventory
for (const [characterId, character] of Object.entries(characters)) {
for (const objectId of character.inventory) {
if (!objects[objectId]) {
throw new Error(`Character ${characterId} has non-existent object ${objectId} in inventory`);
}
}
}
// Check player inventory
for (const objectId of initialState.inventory) {
if (!objects[objectId]) {
throw new Error(`Initial inventory contains non-existent object ${objectId}`);
}
}
}
}