Run a Yumina card in your own app with @yumina.io/engine
@yumina.io/engine is the part of Yumina that runs a card. You give it the card and what the player said. It gives you the messages to send to a chat model, then reads the model's reply and updates the card's values. It is plain TypeScript with two dependencies (zod and js-tiktoken), and it runs in Node, in a browser, or inside a game. There is no server in it.
npm install @yumina.io/engineIts source is in packages/engine.
One turn, with no server
This script loads a small card, builds the prompt for one turn, and parses a reply. The reply is typed in by hand where your model call goes, so it runs as it is with node turn.mjs.
import { gameCard, startSession, buildTurnPrompt, finishTurn } from "@yumina.io/engine";
// 1. Load a card. A card is JSON; gameCard fills in everything a short one leaves out.
const world = gameCard({
name: "Mira's cafe",
variables: [{ id: "trust", name: "trust", type: "number", defaultValue: 0, min: 0, max: 10 }],
entries: [
{ id: "mira", content: "You are Mira, who runs a harbor cafe. Your trust in the player is {{trust}} out of 10." },
{ id: "hello", role: "greeting", content: "Morning. The usual?" },
{ id: "storm", keywords: ["storm"], content: "Last winter a storm sank two fishing boats. Mira's brother was on one." },
],
});
// 2. Open a session: the starting values and the opening line.
const session = startSession({ world });
console.log(session.greeting); // Morning. The usual?
// 3. Build the prompt for one turn.
const userText = "Were you here during the storm?";
const prompt = buildTurnPrompt({
world,
state: session.state,
history: [{ role: "assistant", content: session.greeting ?? "" }],
userText,
});
console.log(prompt.matchedEntryIds); // [ 'mira', 'storm' ]: the word "storm" pulled the lore in
console.log(prompt.messages); // role and content pairs, ready for a chat model
// 4. Send prompt.messages to a model yourself. This reply is typed in by hand.
const rawText = 'She puts the cup down. "I was. I don\'t talk about it much." [trust: +1]';
// 5. Parse the reply and apply what it changed.
const turn = finishTurn({ world, state: prompt.state, userText, rawText });
console.log(turn.cleanText); // She puts the cup down. "I was. I don't talk about it much."
console.log(turn.changes); // [ { variableId: 'trust', oldValue: 0, newValue: 1 } ]
console.log(turn.state.variables.trust); // 1The prompt has seven messages: Mira's entry with filled in as 0, the instructions that tell the model how to write a change ([trust: +1]), the storm entry, a marker where the conversation starts, the greeting, the player's line, and the current values.
For the next turn, pass turn.state as state and add both lines to history. state is plain JSON, so you can put it in a save file.
If your code can await, runTurn does steps 3 to 5 around a function you pass in:
import { runTurn } from "@yumina.io/engine";
const turn = await runTurn({
world, state: session.state, history: [], userText: "Morning!",
llm: async (messages, { maxTokens, temperature }) => callYourModel(messages, maxTokens, temperature),
});llm returns the model's text as a string. maxTokens and temperature come from the card's settings, or 1024 and 0.8 when the card sets none.
What it exports
The functions most apps need:
| Export | What it does |
|---|---|
startSession({ world, variables?, greetingIndex? }) | The starting state and the card's greeting. |
buildTurnPrompt({ world, state, history, userText, maxContext? }) | The messages for one turn. It matches lore against the last lines, fills , and drops the oldest history when the conversation is longer than maxContext tokens (the card's setting, or 32,000). |
finishTurn({ world, state, userText, rawText }) | Removes <think> blocks and [variable: change] directives from the reply, applies the changes the card lets the AI make, and runs the card's behaviors. |
runTurn({ ...same as buildTurnPrompt, llm }) | Both halves around your model call. |
applyVariables(world, state, values) | Your app writes values into the state between turns: gold, health, the time of day. |
gameCard(json) | Turns a short card ({ name, entries: [{ content }] }, up to 256 KB) into a whole one. The format is on Characters your game builds. |
worldDefinitionSchema, migrateWorldDefinition, validateWorld | For a whole card: check its shape, bring an older card up to the current format, and list warnings. |
The classes those functions are built from are exported too, for when you want one piece: GameStateManager (values and the nine operations on them), LorebookMatcher (which entries a line of text triggers), PromptBuilder, ResponseParser and StructuredResponseParser, and ReactionEvaluator with runReactionChain (behaviors).
prepareGameTurn, finishGameTurn and runGameTurn run a game character's turn the way the turn API does: your game's manifest, the checks on the action the character asked for, and the save's memory. openWorld and runWorldTurn do the same for a card with several AIs.
The package also ships dist/yumina-runtime.js. It is one file with no imports that puts the same functions on a global named Yumina. Use it where there is no module loader, such as a JavaScript interpreter embedded in a C# game or a WebView.
TypeScript types are included. WorldDefinition is a card and GameState is a session's state.
What it does not include
- No model call. You send
prompt.messagesto the model you choose and pay for it, or run a local one. - No storage. You keep
stateand the history. The engine holds nothing between calls. - No UI. It returns text and values. Drawing them is your app's job.
- No sign-in and no accounts.
Licence
@yumina.io/engine is Apache-2.0. You can bundle it in a closed-source game or app. Keep its LICENSE and NOTICE files with it. @yumina.io/games is Apache-2.0 as well.
The rest of the Yumina repository (the app, the server, the editor) is AGPL-3.0-only. "Yumina" and the logo are trademarks, and neither licence covers them.
Engine or @yumina.io/games
Use @yumina.io/games when you want Yumina to run the character. The player signs in with their Yumina account and their own balance pays for the AI, so your build has no model key and you get no AI bill. Yumina has free models. Memory is kept per save, and published cards made for your game work in it.
Use @yumina.io/engine when you run everything yourself: your own model key or a local model, your own save format, no Yumina account, no network beyond your model call. You get the prompt builder and reply parser that yumina.io runs on, and you write the rest.
Both read the same cards, so a character written for one works with the other.
