Open beta. Rin Chat is under active development. Expect rough edges, and please tell us what breaks.

Skip to content
Extension guide contents

Hooks

A hook is a function Rin Chat calls when something happens. Register hooks at the script's top level:

rin.on("onChatOpen", async () => {
  rin.log("opened");
});

rin.on("onCommand", async ({ name, args }) => {
  if (name === "roll") await rin.chat.send(`Rolled ${args}`);
});

rin.on(name, fn) registers one function per hook name; a later call for the same name replaces the earlier one. A name Rin Chat does not call is simply never called. A hook may be async; Rin Chat waits for its promise, up to the hook's deadline. Its return value must be JSON; it is ignored except by onMacro, onRender and onTest.

When the script runs

  • Load. When a chat the extension is turned on for opens, Rin Chat starts a fresh interpreter and runs the script's top level (500 ms of CPU, 3 seconds of wall clock). Then it calls onChatOpen.
  • Hooks. After that, the script runs only inside hook calls, one at a time per extension. A promise that a reply settles between calls continues at the next call. There are no timers and no background work.
  • Stop. When the chat closes, the user switches chats, or the window leaves the screen, onChatClose runs (300 ms) and the interpreter is discarded. Everything in memory is gone. Keep what must last in chat variables (rin.chat.vars, per chat) or storage (rin.storage, per extension).
  • Restart. The extension restarts (top level, then onChatOpen) when the user changes anything about extensions (turns one on or off, installs, updates, removes, flips a trust switch), and, for a developer folder, when its code or manifest changed. After a fatal error (timeout, cpu, memory, crashed) the interpreter is discarded and the next hook call loads the script again (top level only, no onChatOpen).
  • Several extensions. For onBeforeAssemble they run in parallel. For onAssembled, onReplyDone, onRender and onMacro they run one after another, each seeing the result of the one before, inside one shared deadline.

User-started and turn hooks

Some abilities depend on whether the user just did something, or a turn is being built.

  • User-started hooks: onCommand, onWidget, onPanel. The user clicked or typed something for this extension. Only these may wait on the user: a network review dialog (rin.net.request to a host not yet allowed) or an rin.open.url confirmation. While such a dialog is open, the hook's deadline is paused. Only these may call rin.chat.generate and rin.composer.draft. With model.run in the manifest, their deadline is 60 seconds instead of 1000 ms, long enough for a completion.
  • Turn hooks: onBeforeAssemble, onAssembled, onReply, onReplyDone, onMacro, onRender. A turn cannot wait on a dialog, so a network request to a host the user has not allowed fails at once with net_needs_approval, and the deadline never moves. rin.model.run inside one is cut off at the hook's deadline.
  • Neither: onChatOpen, onChatClose, onTest.

rin.chat.send works from onCommand, onWidget, onChatOpen and onReplyDone; anywhere else it resolves null and posts nothing.

The hooks

Times are wall clock, measured by Rin Chat. Every hook call also has 100 ms of interpreter CPU time; time spent waiting for Rin Chat to answer a rin.* call does not count toward it.

HookFiresArgumentDeadlineNeeds
onChatOpenAfter the script loads in a chat{}1000 ms
onChatCloseThe chat closes or leaves the screen{}300 ms
onBeforeAssembleBefore each turn's prompt is built{ continuation, asUser }1000 ms
onAssembledAfter the prompt is assembled, before it is sent{ messages }1000 ms for all extensionsprompt.rewrite
onReplyWhile a reply streams, at most every 500 ms{ text }1000 ms, never waited onchat.read or reply.transform
onReplyDoneA reply finished, before it is saved{ text, continuation, asUser }1000 ms for all extensionschat.read or reply.transform
onCommandThe user ran one of its slash commands{ name, args }1000 ms, or 60 s with model.runui.command (to register)
onWidgetA click on a widget in one of its posts{ post, kind, value }1000 ms, or 60 s with model.runchat.send and ui.widget (to post one)
onPanelA click on a button or toggle in its panel{ id } or { id, on }1000 ms, or 60 s with model.runui.panel (to draw one)
onMacroA {{ext.<name>}} it registered is in this turn's prompt{ name, continuation, asUser }200 ms each, 1000 ms for allprompt.macro
onRenderA message is about to be shown{ text, role }300 msrender.transform
onTestSettings > Extensions > Run test hook{}1000 ms

A hook whose permission is missing from the manifest is never called.

onChatOpen

The chat opened, the extension was turned on in it, or it restarted. The place to draw a panel, set stat items, or offer a theme.

onChatClose

The chat closed, the user switched to another chat, or the window left the screen. You have 300 ms, then the interpreter is stopped. By the time it runs the user may already see another chat: calls that read or write the chat are refused with denied. Storage still works.

onBeforeAssemble

{ continuation: boolean; asUser: boolean }

Before each turn's prompt is built: a send, regenerate, swipe, continue (continuation: true), impersonate (asUser: true) or group turn. Not for background work such as summaries. rin.prompt.inject during this hook goes into THIS turn; injections from any other hook wait for the next turn. A retry of the same request (an automatic retry or a regenerate of that reply) gets the same queued injections again. A hook that fails or misses its deadline adds nothing, and the turn goes on without it.

onAssembled

{ messages: Array<{ role: "system" | "user" | "assistant"; content: string }> }

Needs prompt.rewrite. The final messages of this request, after lorebook entries, memory and every injection. Call rin.prompt.rewrite(messages) to send something else: reorder, drop, replace or add. A message you return unchanged (same role and text) stays the app's own, images included; new or changed ones are labeled in the prompt inspector as rewritten by your extension, and drops and reorders are noted. Images appear to the hook as empty content. If the prompt is too large to hand over (near the 1 Mi character payload cap), the hook is skipped for that request.

onReply

{ text: string }

Needs chat.read or reply.transform. The reply so far while it streams, at most every 500 ms. Read only: Rin Chat never waits for it, and while one call runs the chunks in between are skipped.

onReplyDone

{ text: string; continuation: boolean; asUser: boolean }

Needs chat.read or reply.transform. The finished reply, before it is saved. Not called for a Continue, which extends a reply already saved. With reply.transform, rin.reply.transform(text) during this hook changes what is saved; the next extension sees your text. A changed reply carries a "Changed by" mark naming your extension, with the original available.

onCommand

{ name: string; args: string }

The user sent /name rest of the line for a command this extension registered with rin.ui.command. name has no slash; args is the rest of the line, trimmed. The command line is not sent to the model.

onWidget

{ post: string; kind: "choice" | "ask" | "pick" | "form"; value: string }

A click (or Enter) on a widget in one of this extension's own posts. post is the id rin.chat.send returned. value is the choice's label, the ask's answer, the picked items (comma-separated) or the form's message. The click sends nothing and writes no variable by itself.

onPanel

{ id: string } | { id: string; on: boolean }

A click on a button ({ id }) or a toggle ({ id, on }, the state it asks for) in this extension's sidebar panel. The panel does not change by itself: call rin.ui.panel again to show the new state.

onMacro

{ name: string; continuation: boolean; asUser: boolean }

Needs prompt.macro. Called once per registered {{ext.<name>}} per turn. Return the value as a string (at most 4000 characters; anything longer is cut, anything that is not a string is empty). The hook is pure: inside it every rin.* call except rin.log is refused with denied, so compute the value from what you already hold. The value reaches the model inert: macros and directives in it are text.

onRender

{ text: string; role: "user" | "assistant" }

Needs render.transform. Return the text to show instead; the saved message never changes, and the message offers "Show original". Results are cached per message text, and at most 20 new messages are handled per pass. Your text still goes through the app's message sanitizer and image rules, and it is discarded (the original is shown) when it adds anything active. See rin.reply.transform in the API reference for what counts.

onTest

Called by Run test hook on the extension's row in Settings > Extensions, outside any chat. Its return value is shown there. Only rin.log, rin.chat.read (an empty chat), rin.storage.* and rin.settings.get work inside it; anything else is refused with denied.

Failures and the pause

A hook call fails when it throws or rejects (threw), misses its deadline (timeout), runs out of CPU (cpu) or memory (memory), returns something that is not plain JSON (bad_value) or too large (too_large), or the interpreter crashes (crashed). A rejected rin.* call that your code catches is not a failure; one you do not catch is (threw).

Three failures in a row of the same hook pause the extension in that chat. The user sees one notice: "<name> failed three times in a row, so it is paused in this chat. Resume it from the chat menu." While paused, it runs no hooks there, and its queued injections are dropped. The user resumes it from the chat's Extensions dialog. A pause lasts until resumed or until Rin Chat restarts; reopening the chat keeps it. A hook the extension did not register, and a stop the app caused (the chat closed), neither count nor reset the run. A success resets the count for that hook.

timeout, cpu, memory and crashed also discard the interpreter; it loads again on the next call.

If Rin Chat closes unexpectedly twice in a row while an extension is running, it starts with all extensions off (safe mode) for that launch.