Example: relationship-tracker
What ships is the extension/ folder: manifest.json and main.js. The test sits beside it, outside the package. Back to all examples.
extension/manifest.json
{
"id": "com.example.relationship-tracker",
"name": "Relationship Tracker",
"version": "1.0.0",
"entry": "main.js",
"description": "Tracks how warmly the character feels toward you: a sidebar panel with Warmer and Colder buttons, a stat in the Adventure strip, and a line for the model each turn.",
"permissions": ["chat.vars", "prompt.inject", "ui.panel", "ui.hud"],
"author": { "name": "Example Author", "key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }
}
extension/main.js
// @ts-check
/// <reference types="@rin-chat/extension-api" />
// Relationship Tracker: the value lives in a chat variable (`affection`), so it is per chat, shows in the
// chat's variables, survives restarts and follows the chat into backups. The panel and the stat item are
// drawn by Rin Chat from plain records; the extension never writes markup.
const KEY = "affection";
const START = 50;
const STEP = 10;
const clamp = (n) => Math.max(0, Math.min(100, Math.round(n)));
const mood = (n) => (n >= 80 ? "devoted" : n >= 60 ? "friendly" : n >= 40 ? "neutral" : n >= 20 ? "wary" : "hostile");
async function current() {
const vars = await rin.chat.vars.get();
const n = Number(vars[KEY]);
return vars[KEY] !== undefined && Number.isFinite(n) ? clamp(n) : START;
}
async function draw(n) {
await rin.ui.panel({
title: "Relationship",
items: [
{ type: "meter", label: "Affection", value: n, max: 100 },
{ type: "text", text: `They feel ${mood(n)} toward you.` },
{ type: "button", id: "warmer", label: "Warmer" },
{ type: "button", id: "colder", label: "Colder" },
],
});
await rin.ui.hud([{ key: "affection", label: "Affection", icon: "heart", value: `${n}/100`, bar: { value: n, max: 100 } }]);
}
rin.on("onChatOpen", async () => {
await draw(await current());
});
// A click on a panel button. User-started, like a command.
rin.on("onPanel", async ({ id }) => {
const delta = id === "warmer" ? STEP : id === "colder" ? -STEP : 0;
const n = clamp((await current()) + delta);
// Assign, never add in place: a write is the whole new value.
await rin.chat.vars.set({ [KEY]: n });
await draw(n);
});
rin.on("onBeforeAssemble", async () => {
const n = await current();
await rin.prompt.inject({
text: `Relationship: the character's affection toward the user is ${n}/100 (${mood(n)}). Let it shape their tone, not the plot.`,
depth: 2,
});
});
relationship-tracker.test.mjs
Run it from the example's folder: rinext test extension relationship-tracker.test.mjs.
// rinext test extension relationship-tracker.test.mjs
import assert from "node:assert/strict";
export async function drawsThePanelAndTheStat({ open }) {
const chat = await open();
const ui = chat.ui();
assert.equal(ui.panels.length, 1);
const { panel } = ui.panels[0];
assert.equal(panel.title, "Relationship");
assert.deepEqual(panel.items[0], { type: "meter", label: "Affection", value: 50, max: 100 });
assert.equal(ui.hud[0].value, "50/100");
}
export async function buttonsChangeTheVariable({ open }) {
const chat = await open();
assert.equal(await chat.panel("warmer"), true);
assert.equal(chat.vars.affection, "60");
assert.equal(chat.ui().hud[0].value, "60/100");
await chat.panel("colder");
await chat.panel("colder");
assert.equal(chat.vars.affection, "40");
}
export async function staysInsideZeroToOneHundred({ open }) {
const chat = await open({ vars: { affection: 95 } });
await chat.panel("warmer");
assert.equal(chat.vars.affection, "100");
}
export async function tellsTheModelEachTurn({ open }) {
const chat = await open({ vars: { affection: 85 } });
const [line] = await chat.turn();
assert.match(line.text, /affection toward the user is 85\/100 \(devoted\)/);
assert.equal(line.depth, 2);
assert.equal(line.role, "system");
}