npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tool-bridge/sdk

v0.26.0

Published

tool-bridge SDK: embed a TB instance (createToolBridge), register local providers, and connect to a remote gateway

Readme

@tool-bridge/sdk

tool-bridge 的库形态。根入口面向 Node 22+:内嵌 TB 实例、注册本地 Provider,并可反向连接远程网关。@tool-bridge/sdk/device 是独立的运行时中立设备入口;@tool-bridge/sdk/store 是浏览器、Node 与 React Native 共用的 default Store 控制面与流式传输入口。

公开面即全部通道,不存在私有通道:createToolBridge(config) → { fetch, registerTool, registerContext, connect }。

安装

npm install @tool-bridge/sdk

本地嵌入 + 注册工具 + 起 HTTP

import { serve } from '@hono/node-server'
import { createToolBridge, MemoryObjectStore, MemoryStateStore } from '@tool-bridge/sdk'

const tb = createToolBridge({
  state: new MemoryStateStore(),      // 或显式注入的 PostgreSQL StateStore
  objects: new MemoryObjectStore(),   // 必填;仅示例/测试,生产须注入持久 S3 或自定义 driver
  adminSk: process.env.TB_ADMIN_SK,   // 首次引导必须显式提供;SDK 不自行读取宿主 env
})

tb.registerTool(
  'tools/echo',
  {
    list: () => [{ name: 'echo', description: '原样返回 text' }],
    call: (_name, args) => ({ content: { echoed: args.text } }),
  },
  { description: '本地 echo 工具' },
)

serve({ fetch: (req) => tb.fetch(req), port: 8787 })
// curl -H "Authorization: Bearer $TB_ADMIN_SK" http://127.0.0.1:8787/tools/echo/~help
// curl -X POST -H "Authorization: Bearer $TB_ADMIN_SK" -d '{"text":"hi"}' http://127.0.0.1:8787/tools/echo/echo

持久 Store 还需显式注入 storeRepository 和 storeBackends;Mailbox 需注入 mailboxRepository 与加密根。Node 宿主提供 PgStoreRepository / PgMailboxRepository,初始化表结构后供嵌入应用使用。单独的 StateStore 或字节 driver 不会被当作具备事务保证的持久领域后端。

反向连接远程网关(HTTP → WebSocket)

const conn = tb.connect('https://your-gateway.example.com', process.env.TB_SK!, {
  deviceId: 'my-service-01',   // 缺省 os.hostname() 规范化;长驻服务应显式传稳定 id(断线重连恢复 online 依赖它)
})

await conn.ready               // ready 帧到达,本实例注册的节点已挂到远程树 device/<deviceId>/ 下
// 远程即可:POST /device/my-service-01/tools/echo/echo  body {...arguments}

conn.close()                   // 下线;远程节点保留标记 offline,超回收期自动删除

运行时中立的 Store 客户端

管理 default Store 时优先从独立子入口导入。它只依赖 Web 标准,支持注入 fetch,不会把 app、WebSocket 或 Node API 带入浏览器/RN 产物:

import { createStoreClient, parseStoreUri } from '@tool-bridge/sdk/store'

const store = createStoreClient({
  baseUrl: 'https://your-gateway.example.com',
  // 每次控制请求都会重新读取,便于无停机轮换;不要把 SK 写进 URL 或日志。
  sk: async () => await readCurrentSk(),
})

const uploaded = await store.upload({
  body: photoBlob,
  contentType: 'image/jpeg',
  filename: 'capture.jpg',
})

const uri = parseStoreUri(uploaded.uri)
const response = await store.download(uri)
await consumeWithoutBuffering(response.body)

createStoreClient 还提供 stat/list/read/share/revokeShare/delete。短期 $ref、上传 grant 与签名 URL 都属于 bearer secret;SDK 会裁剪 wire 未知字段并脱敏错误,但调用方仍不应持久化或记录这些值。

React Native / Hermes 设备入口

只从子入口导入;不要从包根入口导入移动端能力:

import * as SecureStore from 'expo-secure-store'
import {
  connectDevice,
  createReactNativeWebSocketFactory,
  uploadObject,
} from '@tool-bridge/sdk/device'

const credentialProvider = {
  prepare: async () => {
    const sk = await SecureStore.getItemAsync('tool-bridge-device-sk')
    if (!sk) throw new Error('device credential is missing')
    return { headers: { authorization: `Bearer ${sk}` } }
  },
}

const connection = connectDevice({
  baseUrl: 'https://your-gateway.example.com',
  deviceId: 'phone-01',
  expose: {
    nodes: [{
      path: 'camera',
      kind: 'tool',
      description: '手机相机',
      cmds: [{ name: 'capture', description: '拍照' }],
    }],
  },
  credentialProvider,
  webSocketFactory: createReactNativeWebSocketFactory(WebSocket),
  handler: async (call) => {
    const { path, arguments: args, signal } = call
    // path 含命令叶子段(如 "fs/get"),命令是最后一段;policy、权限提示、
    // 原生能力和 signal 取消处理都属于 App 适配层。
    if (path === 'camera/capture') {
      const photoBlob = await takePhoto({ signal })
      // 网关为本次远程 call 注入窄 upload capability;设备无需知道 Context 或挂载路径。
      // relay PUT 成功即 ready;S3 直传由 SDK 自动 complete。
      return await call.uploadObject({
        body: photoBlob,
        contentType: 'image/jpeg',
        filename: 'capture.jpg',
      })
    }
    return await runNativeTool({ path, args, signal })
  },
})

// AppState active → connection.resume()
// AppState background/inactive → connection.suspend()
await connection.ready

// 设备自主上传(不属于远程 call)也直接写 default Store。此路径使用 Device HTTP identity,
// credentialProvider 在 purpose === 'http' 时必须返回非空 Authorization header。
const uploaded = await uploadObject({
  baseUrl: 'https://your-gateway.example.com',
  deviceId: 'phone-01',
  contentType: 'image/jpeg',
  body: photoBlob,
  credentialProvider,
})
await saveStableReference(uploaded.uri) // 只保存 store://default/...;不要持久化临时上传 URL

移动端默认只承诺 App 前台实时在线。SecureStore、AppState、SQLite 和原生 executor 由应用选择,SDK 不直接依赖 React Native 或 Expo。当前原生 RN adapter 使用第三参数注入 WebSocket upgrade headers;浏览器/RN Web 若不能设置 header,需要网关短期 ticket 能力。

Durable Mailbox(显式拉取)

需要在设备下次运行时补做任务的命令,把 expose 中对应 cmd 的 delivery 设为 mailbox 或 both,再用同一份 expose 与 handler 创建 pull processor:

调用方不需要第二个 enqueue API;在普通 invoke 上声明一次交付策略:

const outcome = await client.invokeJson(
  'device/phone-01/tools/mail/send',
  { text: 'hello' },
  { delivery: 'fallback', idempotencyKey: 'agent-task-1', ttlSeconds: 3600 },
)
// outcome: {delivery:'realtime', result} | {delivery:'mailbox', operation}

fallback 只在网关确认 call 帧尚未 dispatch 时入队;发送后的断线或超时不会产生第二次执行。

import { createDeviceMailboxProcessor } from '@tool-bridge/sdk/device'

const mailbox = createDeviceMailboxProcessor({
  baseUrl: 'https://your-gateway.example.com',
  deviceId: 'phone-01',
  credentialProvider,
  expose: () => currentExpose, // 执行前重新读取;被移除或改回 realtime 的命令会 rejected
  handler: runNativeTool,
  journal: sqliteBackedJournal, // get/put/remove;put 必须在返回前真正持久化
})

// 由 App 启动、网络恢复或自己的调度器触发;SDK 不会偷偷启动后台轮询。
const { processed } = await mailbox.drain({ maxOperations: 20 })

processor 在调用 handler 前先把 executing 写入 journal;若进程在执行中崩溃,重启后只提交 result_unknown,不会再次执行。handler 完成后先持久化 terminal completion,再提交给网关,故 网络失败可以安全重放 complete。journal 实现应按 entry 的 expiresAt 清理;内存 Map 不满足该 契约。

当前幂等边界以一个 deviceId 对应一个活跃安装为前提:两个安装的本地 journal 无法互相观察,使用 同一设备凭证并发消费属于未定义行为。Mailbox 也不唤醒被系统停止的 App;它与 WebSocket realtime 通道互补,而不是 APNs/FCM 的替代实现。设备每次 claim/renew/complete 都重新准备 HTTP Bearer, credential revoke/rotate 会立即阻止后续 lease 操作。

uploadObject 不增加 WebSocket 二进制帧:它调用 system/store/create_upload 后按 grant 选择 网关 relay 或 S3 presigned PUT,最终只返回稳定 store://default/... descriptor。远程 call 优先使用 call.uploadObject;老网关没有 call capability 时该方法会明确返回 unavailable,不会 退回某个隐式 Context。独立调用 helper 时,Node 消费者可从 @tool-bridge/sdk 根入口导入, React Native/Hermes 消费者从 @tool-bridge/sdk/device 导入;两处是同一套 neutral 实现。

设备 SDK 不再暴露 uploadContextObject。把二进制 author 到指定语义 Context entry 仍可通过 Context API、tb ctx upload 或 Dashboard 完成;设备照片、视频、录音等产物统一进入 default Store。

Node 22+ 的嵌入宿主从 @tool-bridge/server 导入官方 AWS SDK 适配器并传给 objects。 适配器仅支持受限流式 relay;中立 SDK 不携带 Node/S3 SDK 依赖:

import { createToolBridge, MemoryStateStore } from '@tool-bridge/sdk'
import { createS3ObjectStore } from '@tool-bridge/server'

const objects = createS3ObjectStore({
  endpoint: 'https://s3.example.com',
  bucket: 'tb-objects',
  region: 'us-east-1',
  accessKeyId: process.env.S3_ACCESS_KEY_ID!,
  secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
})

const tb = createToolBridge({
  state: new MemoryStateStore(),
  objects,
  adminSk,
  uploadGrantTtlSec: 900,
})

配置要点

| 字段 | 语义 | |---|---| | state(必填) | 树配置 / SK / manifest 的存取 | | objects(必填) | default Store 的字节存储,也供对象型 Context 使用;生产必须注入持久 S3 或自定义 driver,MemoryObjectStore 只适合测试/易失开发 | | uploadGrantTtlSec? | create_upload 写 grant 的有效期秒,缺省 900、最大 604800;与下载 $ref TTL 独立 | | secrets? | 上游凭证;缺省 = 基于 state 的加密存储,主密钥 encryptionKey 或 env TB_SECRET_ENCRYPTION_KEY,皆无则 secret 能力禁用(Set 返回 unavailable),durable device Mailbox 也 fail closed | | reservedRoots? / remoteAllowlist? / maxHops? | 追加保留根 / remote 白名单(空 = 拒一切 remote)/ Via 跳数上限(默认 4) |

License

MIT