# JavaScript SDK

`@yumina/games` runs in browsers (web games, itch.io, Electron renderers, game engines that export to the web) and in Node 18+ (Electron main, multiplayer servers, tools). No dependencies.

## Set up

```js
import { Yumina, Card } from "@yumina/games";

const yumina = new Yumina({ clientId: "ym_…", language: "en" });
```

In a browser the refresh token is kept in `localStorage`; elsewhere in memory unless you pass a `tokenStore` (`{ load(), save(token) }`).

## Sign in

```js
// Web game: send the page to yumina.io and back. Add this page's URL to your game's redirect URIs.
if (!(await yumina.isSignedIn())) await yumina.signInWithRedirect(location.origin + "/play");
// …and on that page, before anything else:
await yumina.handleRedirect();

// Anywhere (kiosk, TV, Electron, Node): scan to sign in.
const link = await yumina.startQrSignIn();
drawQr(link.qr.rows);                 // rows[r][c] === "1" is a dark square
await link.wait();

// Node / Electron main: the system browser and a loopback port.
await yumina.signInWithLoopback((url) => shell.openExternal(url));
```

## Talk

```js
const mira = yumina.character("mira", Card.hub(cardId));   // or Card.inline(cardObject)

const turn = await mira.say("Morning! Anything warm?", {
  inputs: { hearts: 3, weather: "foggy" },
  allow: [{ id: "give_item", params: { item: { enum: npc.carrying } } }],   // or plain ids: ["give_item"]
  blocked: { invite: "You are working until five." },                     // why an action is out right now
  period: "spring-3",
  onText: (line) => (bubble.textContent = line),            // streams, clean
  // onLine: (line) => showDialogue(line),                   // or: the whole line once final, before the turn resolves
});

if (turn.action?.accepted && turn.action.id === "give_item") {
  const ok = inventory.add(turn.action.params.item);
  mira.report(turn, ok, ok ? "the player got it" : "backpack full");
}
```

`turn.choices` holds three replies in the player's voice. `mira.endConversation()` when the player walks away.

A refused action has `accepted: false` and a `code` (`limit`, `not_allowed`, `invalid_params`, `unknown_action`, `said_not_done` with `said`): never apply it, and add a word of narration, since the character's line already promised it. `turn.raw` is the turn as the server sent it. See [actions](./concepts#actions-the-ai-proposes-your-game-decides).

## Save and load

```js
save.yuminaMemory = yumina.memory.head;     // when the game saves
yumina.memory.load(save.yuminaMemory);      // when a save is loaded (null for a new game)
```

Call `memory.load` whenever a save is loaded, before the next line; until then the client still holds the last head played.

## Errors

Calls throw `YuminaError` with `status`, `code`, `needsSignIn` and `needsMushies`.
