@av-pi-studio/protocol

中文文档
0.0.115发布于 2026/11/04TypeScriptES Module构建来源已验证近 30 天下载 2244

Shared wire schemas, binary frame codecs, and capability flags for Pi-Studio. This package is the single source of truth for everything that crosses the WebSocket between the daemon and any client (CLI, web, mobile, desktop).

原始使用文档
文档目录6 节
  1. Install
  2. What's in here
  3. Usage
  4. Design rules
  5. Adding a new wire message type
  6. Development

@av-pi-studio/protocol

Shared wire schemas, binary frame codecs, and capability flags for Pi-Studio. This package is the single source of truth for everything that crosses the WebSocket between the daemon and any client (CLI, web, mobile, desktop).

It is a zero-runtime-workspace-dependency library — the only external dependency is zod — so it can be imported by browser and React Native clients as freely as by the Node.js daemon.


Install

npm install @av-pi-studio/protocol

What's in here

  • JSON wire schemas (messages.ts) — every message type that can appear on the WebSocket, as Zod schemas plus their inferred TypeScript types: the hello/status handshake, RPC requests/responses, broadcasts (agent_update, agent_stream, …), and the top-level envelope union that wraps them all.
  • Binary frame codecs (binary-frames/) — encode/decode functions for the two binary wire formats: terminal I/O ([opcode][slot][payload]) and chunked file transfer. Both use Uint8Array, never Buffer, so they run unchanged in a browser.
  • Client capability flags (client-capabilities.ts) — CLIENT_CAPS (what a client can advertise in hello.capabilities) and SERVER_FEATURES (what the daemon advertises in server_info.features), plus a supports() helper.
  • Endpoint parsing (endpoint.ts) — turns a daemon target string (host:port, ws://…, relay://…) into a structured EndpointDescriptor.
  • Provider manifest types (provider-manifest.ts) — UI-facing scaffolding for describing providers/modes (labels, color tiers, capability flags).

Usage

import {
  helloSchema,
  agentStreamEventSchema,
  encodeTerminalFrame,
  decodeTerminalFrame,
  parseEndpoint,
  rpcName,
} from "@av-pi-studio/protocol";

// Validate + parse an inbound message
const hello = helloSchema.parse(rawJson);

// Build a canonical dotted RPC name
const name = rpcName("agent", "permission", "respond", "request");
// → "agent.permission.respond.request"

// Parse a daemon connection target
const endpoint = parseEndpoint("workstation.local:6767");
// → { kind: "direct", host: "workstation.local", port: 6767, ssl: false, raw: "…" }

Every exported schema has a paired TypeScript type (XSchema / X), inferred with z.infer<> — import whichever you need.

Design rules

These are enforced by convention and by the package's own tests, not by tooling:

  • Append-only. Fields are only ever added, and only as optional. Nothing is ever removed, narrowed, or given a new discriminant value. This is what lets an older daemon load data written by a newer one (and vice versa) without a migration step.
  • Schema passthrough. Most schemas use Zod's .passthrough(), so unknown fields from a newer protocol version survive a round-trip through an older decoder instead of being stripped or rejected.
  • Zero workspace imports. This package never imports from another @av-pi-studio/* package — it has to stay usable standalone by any future client, including ones that don't otherwise depend on the rest of the monorepo.
  • Cross-platform binary codecs. The binary frame encode/decode functions only use Uint8Array — no Node-only APIs — so they work identically in Node, the browser, and React Native.
  • Dotted RPC names. New RPCs are named via rpcName(domain, sub, op, dir) → "domain.provider.operation.direction" (e.g. agent.permission.respond.request). Legacy flat names are still accepted on the wire via aliases, but never generated by new code.

Adding a new wire message type

  1. Define the Zod schema (and its inferred type) in messages.ts or a new sibling file.
  2. Add it to the sessionMessageSchema discriminated union.
  3. Export it from index.ts.
  4. Add a round-trip test (messages.envelopes.test.ts or a new *.test.ts).
  5. Register the corresponding handler in the daemon's HandlerRegistry (@av-pi-studio/server).

Development

npm run build       # tsc -b
npx vitest run packages/protocol

Tests are Vitest and live alongside their source files as *.test.ts.

版本与发布记录

@av-pi-studio/protocol 共发布 114 个版本,首次发布于 2026/07/18;过去 12 个月发布了 114 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.115latest2026/11/043 天前
0.0.1142026/10/299 天前
0.0.1132026/10/2613 天前
0.0.1122026/10/2613 天前
0.0.1112026/10/2513 天前
0.0.1102026/10/1028 天前

关于 @av-pi-studio/protocol 的常见问题

@av-pi-studio/protocol 是什么?

Shared wire schemas, binary frame codecs, and capability flags for Pi-Studio. This package is the single source of truth for everything that crosses the WebSocket between the daemon and any client (CLI, web, mobile, desktop).

如何安装 @av-pi-studio/protocol?

在项目目录运行 npm install @av-pi-studio/protocol,或使用 pnpm add / yarn add / bun add。

@av-pi-studio/protocol 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@av-pi-studio/protocol 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@av-pi-studio/protocol 可以商用吗?

@av-pi-studio/protocol 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@av-pi-studio/protocol 的下载量有多少?

根据 npm 官方下载统计,@av-pi-studio/protocol 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,244 次,日均约 75 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@av-pi-studio/protocol 还在维护吗?

最新版本 0.0.115 发布于 2026/11/04(3 天前);至今共发布 114 个版本,其中过去 12 个月发布了 114 个;首次发布于 2026/07/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。