Example: summarizer
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.summarizer",
"name": "Story Summarizer",
"version": "1.0.0",
"entry": "main.js",
"description": "Adds /summarize: asks the model you pick for it to summarize the chat, keeps the summary in the chat, and reminds the model of it each turn.",
"permissions": ["chat.read", "model.run", "chat.vars", "prompt.inject", "ui.command", "ui.notify"],
"author": { "name": "Example Author", "key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }
}
extension/main.js
// @ts-check
/// <reference types="@rin-chat/extension-api" />
// Story Summarizer: model.run from a command. The call goes to the model the user picked for THIS
// extension in Settings > Extensions, under its daily budget; it is never the chat's own model.
const PROMPT =
"Summarize this roleplay chat in at most five sentences. Keep names, places, open plot threads and how the characters feel about each other.";
const WHY = {
no_model: "pick a model for it in Settings > Extensions first",
budget: "it has used today's budget",
model_failed: "the model did not answer",
};
rin.ui.command("summarize", "Summarize the chat so far; the model is reminded of it each turn");
rin.on("onCommand", async ({ name }) => {
if (name !== "summarize") return;
const chat = await rin.chat.read();
const transcript = chat.messages
.slice(-40)
.map((m) => `${m.name}: ${m.text}`)
.join("\n")
.slice(-60000);
if (!transcript) {
await rin.ui.notify("There is nothing to summarize yet.");
return;
}
let reply;
try {
// From onCommand an extension with model.run has 60 seconds, enough for a completion.
reply = await rin.model.run({
messages: [
{ role: "system", content: PROMPT },
{ role: "user", content: transcript },
],
maxTokens: 300,
temperature: 0.3,
});
} catch (e) {
const code = e instanceof Error ? e.message : String(e);
await rin.ui.notify(`No summary: ${WHY[code] ?? code}.`);
return;
}
// A chat variable keeps the summary with this chat (4000 characters at most).
await rin.chat.vars.set({ summary: reply.text.trim().slice(0, 4000) });
await rin.ui.notify("Summary saved. The model is reminded of it each turn.");
});
rin.on("onBeforeAssemble", async () => {
const { summary } = await rin.chat.vars.get();
if (summary) await rin.prompt.inject({ text: `Story so far: ${summary}`, depth: 4 });
});
summarizer.test.mjs
Run it from the example's folder: rinext test extension summarizer.test.mjs.
// rinext test extension summarizer.test.mjs
import assert from "node:assert/strict";
const messages = [
{ role: "assistant", name: "Mira", text: "The gate is sealed. We need the bronze key." },
{ role: "user", name: "Tom", text: "Then we go back to the market and find the smith." },
];
export async function summarizesAndRemindsTheModel({ open, model }) {
model.reply = () => "Mira and Tom found the gate sealed and set off to find the smith for a bronze key.";
const chat = await open({ messages });
await chat.command("/summarize");
// What the provider received: the system prompt, then the transcript.
assert.equal(model.calls.length, 1);
assert.match(model.calls[0].messages[1].content, /Tom: Then we go back to the market/);
assert.equal(model.calls[0].maxTokens, 300);
assert.match(chat.vars.summary, /bronze key/);
const [line] = await chat.turn();
assert.match(line.text, /Story so far: Mira and Tom/);
assert.equal(line.depth, 4);
}
export async function saysWhenNoModelIsPicked({ open, model }) {
model.picked = false;
const chat = await open({ messages });
await chat.command("/summarize");
assert.equal(model.calls.length, 0);
assert.match(chat.notices.join("\n"), /pick a model for it in Settings > Extensions/);
}
export async function saysWhenTheBudgetIsUsed({ open, model }) {
model.budget = { requests: 1, tokens: 20000 };
const chat = await open({ messages });
await chat.command("/summarize");
await chat.command("/summarize");
assert.equal(model.calls.length, 1);
assert.match(chat.notices.join("\n"), /used today's budget/);
}
export async function injectsNothingWithoutASummary({ open }) {
const chat = await open({ messages });
assert.deepEqual(await chat.turn(), []);
}