Plugin authoring
A plugin is an npm package that an app lists in tynd.config plugins. Write one when the same backend methods, client helpers or native code should be shared between apps, or when you need OS access that no official plugin covers. Code that belongs to a single app stays in its own backend.
| Shape | Use it for | Files |
|---|---|---|
| TypeScript | Logic that runs on Bun: wrappers, formatters, routers, state | backend.ts + client.ts |
| Native | OS access not covered by Tynd, heavy CPU work, FFI | client.ts + <name>.gen.ts + native/ Rust crate |
A native plugin’s crate is compiled into the app’s host, so it adds Rust code to every app that installs it. A TypeScript plugin adds none.
1. Scaffold
Section titled “1. Scaffold”tynd plugin new plugins/echo # TypeScripttynd plugin new plugins/echo --native # TypeScript + Rusttynd plugin new plugins/echo --scope acme # package @acme/plugin-echoThe last path component names the plugin: plugins/echo creates the package plugin-echo for the plugin echo. -f, --force replaces an existing directory. See tynd plugin new for the full output.
TypeScript plugin
Section titled “TypeScript plugin”backend.ts holds the methods, which run on Bun:
import { definePlugin } from "@tynd/core/plugin"
export const backend = definePlugin({ name: "echo", methods: { async ping(): Promise<string> { return "pong" }, },})export default backendclient.ts builds the typed frontend client from the backend’s type:
import { createPluginClient } from "@tynd/core/client"import type backend from "./backend"
export const plugin = createPluginClient<typeof backend>("echo")export default pluginCalls, streaming generators and argument types work as in backend RPC.
Native plugin
Section titled “Native plugin”client.ts is a typed client over the generated command catalog:
import { defineTypedPlugin } from "@tynd/plugin-sdk"import type { CommandsCatalog } from "./echo.gen"
export const plugin = defineTypedPlugin<CommandsCatalog>("echo")export default pluginnative/src/lib.rs implements the commands with the tynd-plugin-sdk proc-macros:
use tynd_plugin_sdk::{ tynd_plugin::{plugin, plugin_impl}, PluginResult,};
#[plugin(name = "echo")]#[derive(Debug, Default)]pub struct Plugin;
#[plugin_impl]impl Plugin { #[command] pub fn ping(&self) -> PluginResult<String> { Ok("pong".to_owned()) }}native/src/bin/gen-types.rs prints the TypeScript catalog (gen_types_bin!), and echo.gen.ts holds its output. Native commands are called from the webview only: the Bun backend cannot reach the crate. The Plugin SDK README documents command arguments, PluginCx, background work and capability permissions.
2. The tynd manifest
Section titled “2. The tynd manifest”package.json exports . and ./client to client.ts; a TypeScript plugin also exports ./backend. Its tynd object accepts these keys only:
| Key | Value |
|---|---|
backend |
true when the package exports ./backend, which the app’s backend loads |
native |
{ path, crateName, features? }: the crate directory inside the package, its Cargo package name and optional Cargo features |
apis |
Host APIs the plugin needs; they are compiled into the host of every app that lists the plugin |
kind |
Reserved for official plugins ("host" or "standalone"); community plugins omit it |
{ "tynd": { "native": { "path": "./native", "crateName": "plugin-echo" }, "apis": ["fs"] }}3. Check, generate types and run
Section titled “3. Check, generate types and run”tynd plugin check --cwd plugins/echo # manifest, exports and native crate layouttynd plugin types plugins/echo # native: regenerate echo.gen.ts from Rusttynd plugin types --check # CI: fail when a catalog is staletynd plugin dev plugins/echo # run the plugin in a scratch apptynd plugin checkexits non-zero on a failed check;--jsonprints the issues.tynd plugin typesruns the crate’sgen-typesbinary with cargo. Run it after changing a command’s signature.tynd plugin devgenerates an app under~/.tynd/plugin-dev/<name>that depends on the plugin, then startstynd devthere.--no-watchbuilds it once instead;--scratch <dir>picks another directory.
4. Events
Section titled “4. Events”A native plugin emits events through its context (cx.emit(name, payload)). The client listens with onHostEvent, under the exact name emitted. Prefix event names with the plugin name.
import { onHostEvent } from "@tynd/plugin-sdk"
const off = onHostEvent<{ n: number }>("echo:tick", (payload) => { console.log(payload.n)})off() // unsubscribeOutside a Tynd runtime, onHostEvent returns a no-op.
5. Test
Section titled “5. Test”Test a TypeScript plugin’s methods directly with bun test; they need no host:
import { expect, test } from "bun:test"import backend from "./backend"
test("ping", async () => { expect(await backend.methods.ping()).toBe("pong")})Test a client with installTyndMock from @tynd/core/testing. Native commands are OS calls on the plugin API, keyed "plugin.<name>:<command>"; emitOs delivers an event to onHostEvent listeners:
import { afterEach, beforeEach, expect, test } from "bun:test"import { installTyndMock, type TyndMock } from "@tynd/core/testing"import { onHostEvent } from "@tynd/plugin-sdk"import { plugin } from "./client"
let bridge: TyndMockbeforeEach(() => { bridge = installTyndMock({ os: { "plugin.echo:ping": () => "pong" } })})afterEach(() => bridge.restore())
test("ping", async () => { expect(await plugin.ping(null)).toBe("pong") expect(bridge.calls).toEqual([ { kind: "os", api: "plugin", method: "echo:ping", args: null }, ])})
test("tick event", () => { const seen: unknown[] = [] const off = onHostEvent("echo:tick", (payload) => seen.push(payload)) bridge.emitOs("echo:tick", { n: 1 }) off() expect(seen).toEqual([{ n: 1 }])})Test the Rust side with PluginCx::test() and test_cx() from tynd-plugin-sdk (see the SDK README’s Testing section).
6. Publish
Section titled “6. Publish”A plugin published by anyone other than the Tynd team is a community plugin.
- Remove
"private": truefrompackage.json(the scaffold sets it) and give the package a realversion. - Keep
filescomplete: a native plugin shipsclient.ts,<name>.gen.tsand thenative/sources, since apps compile the crate into their host. - Run
tynd plugin checkand, for a native plugin,tynd plugin types --check. - Publish to npm as usual.
Apps install it by package name:
tynd add @acme/plugin-echotynd add installs the package and lists it in plugins. Community plugins are not in the plugin catalog.
What can go wrong
Section titled “What can go wrong”tynd plugin checkfails on an unknowntyndkey: onlykind,apis,nativeandbackendare allowed.- A native call rejects outside a Tynd webview: the client needs the host bridge. Mock it with
installTyndMockin tests. generated plugin types are stale: runtynd plugin typesand commit the.gen.tsfile.