Skip to content

Types for your editor

types/pocket.d.ts has the types of Pi Pocket’s public extension API. With it, your editor completes names and finds mistakes, and Pi reads the exact shapes. The reference pages give the same members in words.

Use one of these:

  1. _pocket.d.ts in the drop-in folder. Pi Pocket makes this link to its types file each time that it starts. Its leading _ keeps it from loading as a module. The same import then works on all machines. If you point the link at another readable file, such as your own checkout’s types, Pi Pocket leaves it as it is. A file of your own with this name also stays.
  2. The path in Pi Pocket’s folder: /path/to/pi-pocket/types/pocket.d.ts.
  3. The copy on the documentation site: https://tannermidd.github.io/pi-pocket/docs/pocket.d.ts. It is for the latest release.

Where Pi Pocket cannot make links (on Windows, or when the system refuses them), _pocket.d.ts is a copy of the types file instead. Its first line says so, and Pi Pocket refreshes it at each start when the types change. Do not edit it.

Use import type. Node removes it, so the import has no effect when the module runs:

import type { Host, Web } from "./_pocket.d.ts";
export default function setup(host: Host, web: Web) {
web.route("GET", "status", () => ({ folder: host.dataDir }));
}

Do not write import { type Host }. The gallery’s check reads that form as an import that runs, and it refuses a drop-in that imports a file that is not its own.

A browser module is JavaScript. Give the types in JSDoc:

/** @param {import("./_pocket.d.ts").Pocket} pocket */
export default function setup(pocket) {
pocket.rail({ id: "usage", title: "Usage", icon: "pulse", run: () => {} });
}

From a browser module in a folder, the path is ../_pocket.d.ts.

The types file imports types from Pi Durable, Pi AI, and Preact by package name. The drop-in folder’s tsconfig.json gives them, also where the system refuses links: Pi Pocket writes it at each start, and its paths lead each of Pi Pocket’s packages to its types. Its first line says that Pi Pocket made it, and Pi Pocket refreshes it when its folders change. Do not edit it. To use your own settings, replace it with a file of your own, without that first line: Pi Pocket then leaves it as it is. Where links work, the drop-in folder’s node_modules link to Pi Pocket’s packages gives the types to tools that do not read tsconfig.json.

tsc skips <name>.js when there is a <name>.ts. To check both with tsc, put the browser module in the folder <name>/. An editor checks each file that you open.

  • Host and the types that it uses: SafeText, LinkedSessions, and LinkedSession.
  • Web and the types that it uses: WebAccess, WebMethod, WebHandler, and WebRequest.
  • Command, CommandContext, and CommandResult.
  • Pocket and the types that it uses: BrowserModule, Action, the objects that you give (RailSpec, MenuSpec, LauncherSpec, PageSpec, SheetSpec, ToolCallSpec, CallSummary), Added, PageProps, SheetProps, and the props of the pieces.
  • Module, ToolOptions, Person, Role, and NoticeLevel, on this page.
  • Since 0.22.0, experimental: PublicPageHandler, PublicPageRequest, PublicPageAnswer, PublicLinks, PublicLinkRequest, and PublicLinkInfo (Public pages); AskRequest, AskField, AskAnswer, HiddenValue, CommandRun, CommandEnvProvider, BrowserPageHandle, BrowserControl, and BrowserTarget (People’s values); AppEvents, AppEventName, SettingField, and SettingValue (Events, services, and settings).

The types file does not have the internal members of the host. It does not have Pi Durable’s own API either: import defineExtension, defineTool, and the rest from @earendil-works/pi-durable.

Each member has @since, and @experimental when it applies. API versions tells you what these mean.

interface Module {
default(host: Host, web: Web): Extension | readonly Extension[] | void | Promise<…>;
pocketApi?: number;
}

Since 0.1.0Stable

A module: one .ts file that default-exports a setup function. Modules, loading, and errors tells you how Pi Pocket loads it.

default(host: Host, web: Web): Extension | readonly Extension[] | void | Promise<Extension | readonly Extension[] | void>;

Since 0.1.0Stable

Sets up the module. It gets the host and web. It returns the module’s extensions: one, a list, or nothing when the module only adds to the app. It can be async. Pi Pocket 0.14.0 added web, async setup, and modules that return nothing.

pocketApi?: number;

Since 0.14.0Stable

The version of the extension API that the module needs. A Pi Pocket with an older version does not set up the module. See API versions.

interface ToolOptions {
readonly subagents?: "always";
}

Since 0.29.0Experimental

Pi Pocket’s options on a tool definition, beside Pi Durable’s own. Add them with Object.assign after defineTool (Tools).

  • subagents: "always": give the tool to every subagent whose extensions include it, when the session enables it, even when its tools list leaves it out. It is added once. A deferred tool is offered at once with a tool list; a tool that only scripts call brings codemode. Without a list, or on send, nothing changes. It does not add its extension. The tool’s prompt and spawn result do not mention the marker or the tools it adds.

Older Pi Pocket ignores the field. The API version stays 2: this is an addition, not a break.

interface Person {
readonly id: string;
readonly name: string;
readonly role: Role;
}

Since 0.14.0Stable

A person: the person who sent a request (request.user), who ran a command (ctx.user), or who uses a tab (pocket.me()).

  • id: the person’s id. web.send(event, data, { to: id }) sends to this person’s tabs.
  • name: the person’s name, as the app shows it.
  • role: the person’s role.
type Role = "owner" | "guest" | "viewer";

Since 0.14.0Stable

A person’s role. The Glossary tells what each role can do.

type NoticeLevel = "info" | "warning" | "error";

Since 0.1.0Stable

How important a notice is.

Pi Pocket’s checks fail when the types file and its code do not agree:

  • In npm run check, test/public-types.ts compares the real host, web, and command context with the public types, in both directions. It also fails for a new host member that is neither in the types file nor in its list of internal members.
  • In npm run check, test/types-file.ts checks the types file itself.
  • In npm test, test/extension-ui.test.ts compares the members of the real pocket with Pocket.

A change that adds a member also adds it to the types file, with @since, and to its reference page.

These pages describe Pi Pocket 0.31.3. Your copy has the pages of its own version in its docs/ folder.