// APP HEADER: the platform provides one app name (no app icon), palette-matched Workshop
// credit, and an owner Manage or eligible visitor Remix control. It stays visible when installed.
// Keep date/count subtitles in the body. The first h1 in .n-header (or first app h1) is adopted.
// Mark ONE existing primary button data-header-action and include a meaningful fallback icon:
//
// Keep the original button's listener and hidden/disabled state; the SDK mirrors them in the host.
// Other controls remain in the body. The action label collapses first, then Manage/Remix;
// finally the app name truncates with an ellipsis. The action never wraps to another row.
// Always supply an accessible label (button text or aria-label) and an icon from the library.
// Do not add another title, app icon, Workshop link, or owner/remix controls in app code.
// Existing .n-header primary .n-btn buttons are adopted too; mark custom headers explicitly.
// Standalone previews show the action and attribution; privileged links belong to the live host.
// Nerd Out Workshop SDK 2.0: the `nerd` global inside an app frame.
// Guide: /workshop/sdk/guide.md
export type NerdTheme = "light" | "dark";
export type NerdScope = "device" | "account" | "app";
/** local: saved in this browser (device scope, previews). live: connected, changes arrive as they happen. connecting / offline: not receiving changes; writes fail with "offline" while offline. */
export type NerdLiveStatus = "local" | "connecting" | "live" | "offline";
export interface NerdViewer {
signedIn: boolean;
/** The viewer's user id when signed in; matches meta.createdBy and members[].id. */
id: string | null;
/** The viewer's display name when signed in. */
name: string | null;
/** True when the viewer may read but not change the app's data (a non-member of an open app). The same flag as data.readOnly. */
readOnly: boolean;
}
/**
* role: the person's level, under the name the owner sees for it (Owner, Admin, Manager, Participant, Viewer). An admin
* helps the owner run the app and, like a manager, changes any shared record. A participant adds records to collections with "contribute": true and changes only the
* ones they added; everywhere else they are a viewer. Decide what to show from ctx.data.writable and
* nerd.data.canChange, not from the role's name: "manager" was called "editor" before November 2026.
*/
export interface NerdMember { id: string; name: string; role: "owner" | "admin" | "manager" | "participant" | "viewer" }
export interface NerdRoute {
/** Query parameters from the app's address, excluding Workshop's reserved parameters (open, made, source). */
query: Record;
/** The URL fragment after "#", without the leading "#" ("" when absent). */
hash: string;
}
export interface NerdRouteInput {
/** Query parameters to set (null, undefined or "" omits a key). Omit to keep the current query in set(); pass {} or null to clear. */
query?: Record | null;
/** Hash fragment (with or without leading "#"). Pass "" or null to clear. Omit to keep the current hash in set(). */
hash?: string | null;
}
export interface NerdRouteSetOptions {
/** true adds a browser history entry so Back/Forward returns to the previous route. Default false (updates the address bar in place). */
push?: boolean;
}
export interface NerdRouter {
/** The current deep link ({ query, hash }), populated once nerd.ready() resolves (also on ctx.route). */
get(): NerdRoute;
/** Calls listener once when ready with the opening route, and again on browser Back/Forward or address bar hash edits. Calling set() updates the address without firing observe. Returns a stop function. */
observe(listener: (route: NerdRoute) => void): () => void;
/** Updates the app's query string and/or hash on /a/[slug]. Accepts { query?, hash? } (omitting either keeps it) or a relative string ("?tab=notes#item-1", "#settings", "", which replaces both). Reserved query keys (open, made, source) throw invalid_argument. */
set(next: NerdRouteInput | string, options?: NerdRouteSetOptions): void;
/** Builds a full shareable URL for this app (e.g. for nerd.copy or nerd.share). Omit target for the current route, or pass { query?, hash? } or a relative string ("?item=123#comments"). */
url(target?: NerdRouteInput | string): string;
}
export interface NerdContext {
app: { id: string; slug: string; name: string };
viewer: NerdViewer;
group: { name: string; label: "Group" | "Household" | "Team"; canInvite: boolean } | null;
/**
* writable: the collections this viewer may add to (a viewer of a shared app keeps their own account-scope collections).
* ownOnly: the writable collections where this viewer may change or remove only the records they added. It is empty for
* everyone but a participant, and names the shared collections the manifest opens with "contribute": true. Ask
* nerd.data.canChange(collection, record) before offering edit or delete on a record.
* links: where each "scope": "link" collection stands (see NerdLink), by collection name.
*/
data: { scope: NerdScope; readOnly: boolean; writable: string[]; ownOnly: string[]; status: NerdLiveStatus; links: Record };
/** The viewer's colour scheme, live. */
theme: NerdTheme;
/** The manifest's named theme. */
appTheme: string;
/** The current deep link ({ query, hash }) from the app's address bar. */
route: NerdRoute;
/** True in the owner's preview and in render checks: data is disposable. */
preview: boolean;
mode: "hosted" | "standalone";
}
export interface NerdRecordMeta {
createdAt: string;
updatedAt: string;
/** User ids (see nerd.members). Null for device scope. */
createdBy?: string | null;
/** The space a record of a space-scope collection belongs to. */
space?: string;
updatedBy?: string | null;
}
export type NerdRecord> = T & { id: string; meta: NerdRecordMeta };
export type NerdSnapshot = Record>> = { [K in keyof S]: NerdRecord[] };
export type NerdErrorCode =
| "invalid_record" | "record_missing" | "storage_full" | "unavailable" | "timeout" | "busy" | "too_large"
| "read_only" | "offline" | "permission_denied" | "invalid_url" | "invalid_argument" | "unhandled_rejection";
export interface NerdError extends Error { code: NerdErrorCode }
export interface NerdWriteOptions { requestId?: string }
/** space: required for space-scope collections ("scope": "space"): the space the new record belongs to. */
export interface NerdCreateOptions extends NerdWriteOptions { id?: string; space?: string }
export interface NerdData = Record>> {
snapshot(): Promise>;
/** Calls listener with the current snapshot now and after every change, including changes made on other devices and by other members. Returns a stop function. */
observe(listener: (snapshot: NerdSnapshot) => void, onError?: (error: NerdError) => void): () => void;
create(collection: K, values: S[K], options?: NerdCreateOptions): Promise>;
update(collection: K, id: string, patch: Partial, options?: NerdWriteOptions): Promise>;
remove(collection: keyof S & string, id: string, options?: NerdWriteOptions): Promise;
/** Undo a remove: the record comes back with the same id and its last values, for everyone. */
restore(collection: K, id: string, options?: NerdWriteOptions): Promise>;
/**
* Whether this viewer may change or remove a record: true for anyone who may write the collection, except that a
* participant (see data.ownOnly) may change only the records they added (meta.createdBy is theirs). A change the
* viewer may not make fails with read_only, so offer edit and delete only where this is true.
*/
canChange(collection: keyof S & string, record: { meta?: NerdRecordMeta } | null | undefined): boolean;
/** One record from the current snapshot, or null. */
get(collection: K, id: string): Promise | null>;
/** Filter, search, sort and page one collection of the current snapshot. */
query(collection: K, query?: NerdQuery): Promise[]>;
/** query(), re-run on every change. Returns a stop function. */
observeQuery(collection: K, query: NerdQuery, listener: (records: NerdRecord[]) => void, onError?: (error: NerdError) => void): () => void;
/** The record a ref field points at, from the latest snapshot, or null. */
resolve(record: object, field: string): NerdRecord | null;
/** A number between two positions for manual ordering (drag to reorder): between(prev?.position, next?.position). */
between(a?: number | null, b?: number | null): number;
/** One record's changes, newest first. */
history(collection: keyof S & string, id: string): Promise;
status(): NerdLiveStatus;
/** Calls listener now and whenever the live status changes. Returns a stop function. */
observeStatus(listener: (status: NerdLiveStatus) => void): () => void;
}
/** where: { field: value } or { field: { eq | ne | lt | lte | gt | gte | in | contains | exists: value } }; also id, createdAt, updatedAt, createdBy, updatedBy. contains: case-insensitive substring for strings, exact item for lists. order: "field" or "-field" (descending), or several. search: case-insensitive text matched in any string or list field (one phrase, not separate terms). */
/** order: "field" (ascending), "-field" (descending), or several to break ties. A record with no value for the field comes last in either direction; false sorts before true, so "-starred" lists the starred first. */
export interface NerdQuery { where?: Record; search?: string; order?: string | string[]; limit?: number; offset?: number }
/** via: set when the change was made through another app this one's records are linked to; the name of that app. Say it ("through Schedule"). */
export interface NerdHistoryEntry { op: "create" | "update" | "remove" | "restore"; collection: string; id: string; values: Record | null; changed: string[] | null; by: string | null; at: string; via?: string }
export interface NerdComment { id: string; body: string; mentions: string[]; by: string | null; at: string; editedAt: string | null }
export interface NerdFile { id: string; name: string; mime: string; bytes: number; width: number | null; height: number | null; createdBy: string | null; createdAt: string }
/** Where this person and device stand with the app's notifications. devices counts every device they turned on for this app. */
export interface NerdPushStatus { state: "on" | "off" | "needs_install" | "blocked" | "unsupported" | "unavailable" | "signed_out"; channel: "push" | "email" | "both"; devices: number }
export interface NerdScheduled { id: string; key: string | null; title: string; body: string; at: string; repeat: "none" | "daily" | "weekly"; to: "me" | "everyone" | string[]; push?: string; image?: string; open?: string }
export interface NerdPresence { id: string; name: string; state: unknown; self: boolean }
export interface NerdSourceState { status: "pending" | "ok" | "error"; data: T | null; fetchedAt: string | null; error: string | null }
export type NerdIntegrationState = "connected" | "not_connected" | "needs_reconnect" | "not_configured" | "signed_out" | "unavailable";
/** { separator: true } draws a line between groups of items. */
export type NerdMenuItem = { label: string; run: () => void; icon?: string; danger?: boolean; disabled?: boolean } | { separator: true };
export interface NerdSpace { id: string; name: string; data: Record; owner: string | null; members: string[]; createdAt: string | null }
/** Groups inside an app with their own members, picked from the app's members. Records of space-scope collections and documents opened with { space } are readable only by that space's members. */
export interface NerdSpaces {
/** The spaces the viewer belongs to. */
list(): Promise;
get(id: string): NerdSpace | null;
observe(listener: (spaces: NerdSpace[]) => void): () => void;
/** App owners and editors; the creator becomes the owner and a member. data: small extras (a color), up to 1 KB. */
create(space: { name: string; members?: string[]; data?: Record }): Promise;
/** The owner only. members replaces the list; owner hands the space over. */
update(id: string, patch: { name?: string; members?: string[]; data?: Record; owner?: string }): Promise;
/** Any member but the owner. */
leave(id: string): Promise;
/** The owner only: deletes the space with its records and documents. */
remove(id: string): Promise;
}
/**
* Where a "scope": "link" collection stands. Its records live in another app that this app's owner links on the app's
* page; read and change them through nerd.data like any other collection. state:
* "unbound" no app is linked yet. The collection is empty. Say so, and that the owner links an app on the app's page.
* "on" linked. from is the other app's name.
* "paused" linked, and stopped for now (the other app is offline or under review). The collection is empty.
* "hidden" linked, and this person is not one of the people the other app's owner shares the records with.
*/
export interface NerdLink { state: "unbound" | "on" | "paused" | "hidden"; from: string | null }
export interface NerdLinks {
/** Each linked collection's state, by collection name. Empty when the app has no linked collection. */
list(): Promise>;
/** Calls listener with the states now and whenever one changes. */
observe(listener: (links: Record) => void): () => void;
}
export interface NerdMembers {
/** The people this app is shared with (owner first). Empty for device scope. */
list(): Promise;
/** Calls listener with the current list now and whenever it changes. */
observe(listener: (members: NerdMember[]) => void): () => void;
}
/** A shared text document (Yjs). Needs "documents": true in manifest.data. */
export interface NerdDoc {
readonly id: string;
/** The Y.Doc; `doc.getText("text")` is `text`. Use nerd.docs.Y for other shared types. */
readonly doc: unknown;
/** The document's Y.Text. */
readonly text: unknown;
/** The current text. */
get(): string;
/** Calls listener now and after every change, local or remote. Returns a stop function. */
observe(listener: (text: string) => void): () => void;
/** Keeps a