TypeScript plugins
The primary plugin language: project layout, entry point, sandbox, CLI and limits.
TypeScript is the primary plugin language. Java remains the advanced option for raw android.view.View objects, native threading and the unstable internal facade. A hybrid package combines both: TypeScript is the primary entry point and orchestrates the plugin; an optional Java module exposes explicitly registered native methods.
1. Kinds#
kind | Payload | Entry point | Trust |
|---|---|---|---|
ts (script) | script/main.js | default export assigned to globalThis.__ngp_plugin__ | QuickJS sandbox |
java (code) | android/dex/classes.dex | entrypoints.android implements NextGramPlugin | Trusted code |
hybrid | both | TypeScript is primary; the class implements NextGramNativeModule | Trusted code, TypeScript part sandboxed |
A package has one id, one signature, one storage namespace and one consent screen. Hybrid packages show both the TypeScript sandbox badge and the trusted-code warning.
2. Project layout#
my-plugin/
plugin.json
package.json
tsconfig.json
src/
index.ts
assets/
icon.png
fonts/
sounds/
i18n/
en.json
ru.json
README.md@nextgram/plugin provides the types and runtime helpers. @nextgram/cli bundles, validates, packs and signs the package.
3. Entry point#
import { definePlugin, column, text, button } from "@nextgram/plugin";
export default definePlugin({
async onLoad(ctx) {
ctx.contributions.addSettingsEntry({
id: "admin",
title: "Chat admin",
screen: () => column({
style: { padding: 16, gap: 8 },
children: [
text("Admin tools"),
button("Open members", { onPress: "members" }),
],
}),
});
},
});definePlugin assigns the plugin object to globalThis.__ngp_plugin__ when the bundle is evaluated. The host reads it once, validates the lifecycle functions and calls onLoad(ctx) with a 1 s budget. A rejection, timeout, stack overflow or sandbox violation is fatal and disables the plugin immediately.
The bundle is a single IIFE. No CommonJS module.exports, no runtime ESM import, no node:* built-ins. The CLI rejects those at build time.
4. Lifecycle#
onLoad may return a promise and is awaited up to 1 s. Other callbacks run on the plugin's JavaScript thread, never the UI thread:
onStart, onStop, onUnload, onAccountStart, onAccountStop, onAppEvent, onPermissionRevoked.
Ordinary callback, factory and interceptor exceptions count toward a three-failure budget. Fatal failures disable immediately with a clear log.
5. Sandbox#
Each TypeScript plugin gets its own QuickJS context on a dedicated thread with a 32 MB heap and a 6 MB stack. There is no Java, Android, filesystem, socket, module loader or reflection binding. Only the __ngp bridge is visible:
interface NgpBridge {
call(method: string, payload: string): Promise<string>;
emit(event: string, payload: string): void;
log(level: "debug" | "info" | "warn" | "error", message: string): void;
ready(): void;
}Payloads are UTF-8 JSON, max 1 MB per request and 4 MB per response. Errors are { code, message, data }. No host object, function, file handle or thread crosses the bridge; binary fields are base64 with an explicit MIME type and a size limit.
Host event listeners are delivered through the same bridge. A slow listener is queued and counted; an overflowing queue drops events and logs a warning. Timers are host-managed and cancelled on unload.
6. Context#
interface PluginContext {
readonly api: 1;
readonly plugin: PluginInfo;
readonly storage: Storage;
readonly scheduler: Scheduler;
readonly logger: Logger;
readonly contributions: Contributions;
readonly navigation: Navigation;
readonly ui: Ui;
readonly screens: Screens;
readonly appearance: Appearance;
readonly resources: Resources;
readonly events: Events;
readonly interceptors: Interceptors;
readonly http: Http;
readonly media: Media;
readonly notifications: Notifications;
readonly native: NativeBridge;
messages(): Promise<Messages>;
chats(): Promise<Chats>;
chatAdmin(): Promise<ChatAdmin>;
automation(): Promise<Automation>;
require<T>(token: HostToken<T>): T;
}See capabilities.md for messages, chats, admin, HTTP, media and notifications; resources.md for resource overrides; declarative-ui.md for the UI model.
7. Hybrid native calls#
A hybrid package has exactly one native module and no module argument:
const result = await ctx.native.call("applyPatch", { value: 1 });The Java module registers method names and JSON schemas. The manifest nativeMethods array and methods() must intersect; a method present in only one list is unavailable. Arguments and returns are JSON values with size, depth and count limits. Calls time out after 5 s; a timeout is fatal for the hybrid plugin. ctx.native.call grants no extra permissions.
public final class MyNativeModule implements NextGramNativeModule {
@Override public void onLoad(NativeModuleContext context) {}
@Override public void onUnload() {}
@Override public Map<String, NativeMethod> methods() {
return Collections.singletonMap("applyPatch", args -> patch(args));
}
}8. CLI#
nextgram-plugin build # typecheck, esbuild IIFE, manifest validation
nextgram-plugin pack # build + deterministic .ngp + signature
nextgram-plugin verify # same checks as the host verifier
nextgram-plugin dev # watch, rebuild, optional adb pushbuild enforces the 8 MB bundle limit and rejects Node built-ins and dynamic imports. pack writes plugin.json, assets/**, script/main.js, optional android/dex/classes.dex for hybrid, LICENSE, README.md, CHANGELOG.md and i18n/**, then signs with the provided key. verify checks roots, sizes, prohibited extensions, SHA-256 signature entries and the manifest.
Templates live under nextgram-plugin-ts/templates/; examples under nextgram-plugin-ts/examples/.
9. Limits#
| Item | Limit |
|---|---|
| Bundle | 8 MB |
| Package | 64 MB |
| Uncompressed total | 192 MB |
| QuickJS heap | 32 MB per plugin |
| Concurrent contexts | 10, 128 MB total script heap |
| Bridge request / response | 1 MB / 4 MB |
| Queued bridge calls | 1,000 |
| UI callback | 250 ms |
| Background callback | 2 s |
onLoad | 1 s |
10. Compatibility#
formatVersion and api stay 1. ts and hybrid packages must declare minHost because the runtime is new. Older hosts reject hybrid with E_UNSUPPORTED_KIND or E_UNSUPPORTED_API. A host without the native runtime keeps Java plugins working and rejects TypeScript execution with E_UNSUPPORTED_API and the documented copy.
TypeScript-only plugins reload without a process restart in developer mode. Hybrid and Java plugins need a restart when their DEX changes.