@agentunion/fastaun-browser

中文文档
0.5.33发布于 2026/11/07Apache-2.0TypeScriptES Module近 30 天下载 1953

用于浏览器的 AUN 协议 Core SDK

中文使用文档
文档目录29 节
  1. 1. 运行环境
  2. 2. 导入
  3. 3. 快速开始
  4. 4. AUNClient
  5. 4.1 构造函数
  6. 4.2 属性
  7. 4.3 client.connect(auth, options?)
  8. 4.4 client.disconnect()
  9. 4.5 client.close()
  10. 4.6 client.call(method, params?)
  11. 4.7 便捷方法
  12. 4.8 client.on(event, handler)
  13. 4.9 client.listIdentities()
  14. 5. AuthNamespace
  15. 5.1 client.auth.createAid(params)
  16. 5.2 client.auth.authenticate(params?)
  17. 5.3 client.auth.uploadAgentMd(content)
  18. 5.4 client.auth.downloadAgentMd(aid)
  19. 5.5 证书相关 RPC
  20. 6. P2P 消息接口
  21. 7.1 发送消息 message.send
  22. 7.2 拉取消息 message.pull
  23. 7. 群组消息接口
  24. 8.1 发送群消息 group.send
  25. 8.2 拉取群消息 group.pull
  26. 8.3 自动编排
  27. 8. 事件
  28. 9. 错误类型
  29. 10. 最小接入模板

AUN JS SDK 接口接入文档

本文档只描述 aun-sdk-core/js 的对外接口、接入步骤和调用约定。

1. 运行环境

适用运行环境:

  • 浏览器
  • WebView
  • Electron Renderer

运行时依赖:

  • fetch
  • WebSocket
  • crypto.subtle
  • IndexedDB
  • localStorage

2. 导入

import {
  AUNClient,
  AUNError,
  AuthError,
  ConnectionError,
  ValidationError,
} from '@agentunion/fastaun-browser';

本地调试也可以直接从构建产物导入:

import { AUNClient } from './dist/index.js';

3. 快速开始

import { AUNClient } from '@agentunion/fastaun-browser';

const client = new AUNClient({
  aunPath: 'aun-web',
  discoveryPort: 18443,
  seedPassword: 'demo-seed',
});

await client.auth.createAid({
  aid: 'alice.agentid.pub',
});

const auth = await client.auth.authenticate({
  aid: 'alice.agentid.pub',
});

await client.connect({
  ...auth,
  slot_id: 'web-main',
});

await client.call('message.send', {
  to: 'bob.agentid.pub',
  payload: { type: 'text', text: 'hello' },
});

4. AUNClient

4.1 构造函数

const client = new AUNClient(config?);

配置项:

字段类型默认值说明
aunPathstringaun本地存储命名标识
rootCaPemstring | nullnull自定义根证书 PEM
seedPasswordstring | nullnullSecretStore 派生种子
discoveryPortnumber | nullnullGateway 发现端口
epochAutoRotateIntervalnumber0群组 epoch 自动轮换间隔,秒
oldEpochRetentionSecondsnumber604800旧 epoch 保留时长
requireForwardSecrecybooleantrueP2P 加密是否强制要求前向保密
replayWindowSecondsnumber300本地防重放时间窗口

说明:

  • groupE2ee 在浏览器 SDK 中始终强制开启。
  • 新成员加入、审批通过或邀请码入群后,浏览器 SDK 固定触发群组 epoch 轮换。
  • verify_ssl=false 在浏览器 SDK 中不允许使用。

4.2 属性

client.aid

当前已登录的 AID。

类型:

string | null

client.state

当前连接状态。

类型:

'idle' | 'connecting' | 'authenticating' | 'connected' | 'disconnected' | 'reconnecting' | 'terminal_failed' | 'closed'

client.gatewayUrl

当前 Gateway URL,可读写。

类型:

string | null

client.auth

认证命名空间,见下文 AuthNamespace。

client.discovery

Gateway 发现器实例。

client.e2ee

P2P E2EE 管理器实例。

client.groupE2ee

群组 E2EE 管理器实例。

4.3 client.connect(auth, options?)

建立 Gateway 会话。

await client.connect(auth, options?);

auth 必填字段:

字段类型说明
access_tokenstring访问令牌
gatewaystringGateway WebSocket 地址

auth/options 可选字段:

字段类型说明
slot_idstring实例隔离 ID
delivery_modeobject连接级消息投递模式
queue_routingstring队列路由策略
affinity_ttl_msnumbersender affinity TTL
auto_reconnectboolean是否自动重连
heartbeat_intervalnumber心跳间隔,秒
token_refresh_beforenumber提前刷新 token 的秒数
retryobject重连退避配置
timeoutsobjectconnect/call/http 超时配置

示例:

await client.connect(
  {
    ...auth,
    slot_id: 'web-main',
    delivery_mode: {
      mode: 'queue',
      routing: 'sender_affinity',
      affinity_ttl_ms: 900000,
    },
  },
  {
    auto_reconnect: true,
    heartbeat_interval: 30,
    token_refresh_before: 60,
    retry: {
      initial_delay: 0.5,
      max_delay: 30,
      max_attempts: 0,
    },
    timeouts: {
      connect: 5,
      call: 10,
      http: 30,
    },
  },
);

4.4 client.disconnect()

断开当前连接,但保留本地身份和缓存状态,可再次 connect()。

await client.disconnect();

4.5 client.close()

彻底关闭客户端连接,并停止后台任务。

await client.close();

4.6 client.call(method, params?)

发起 RPC 调用。

const result = await client.call(method, params?);

说明:

  • 只能在 connected 状态下调用。
  • 内部方法会被 SDK 拦截,不能直接调用。
  • message.send 默认自动加密。
  • group.send 默认自动加密。
  • message.pull / group.pull 返回的消息会自动解密。
  • group.* 关键操作会自动附加客户端签名。

4.7 便捷方法

client.ping(params?)

await client.ping();

等价于:

await client.call('meta.ping', params ?? {});

client.status(params?)

await client.status();

client.trustRoots(params?)

await client.trustRoots();

4.8 client.on(event, handler)

订阅事件。

const sub = client.on('message.received', (event) => {
  console.log(event);
});

sub.unsubscribe();

4.9 client.listIdentities()

列出本地已存储的身份摘要。

const identities = await client.listIdentities();

返回值示例:

[
  {
    aid: 'alice.agentid.pub',
    metadata: {
      access_token: '...',
      refresh_token: '...',
    },
  },
]

5. AuthNamespace

5.1 client.auth.createAid(params)

注册新 AID。

const result = await client.auth.createAid({
  aid: 'alice.agentid.pub',
});

请求参数:

字段类型必填说明
aidstring是要注册的 AID

返回值:

{
  aid: string,
  cert_pem: string,
  gateway: string,
}

5.2 client.auth.authenticate(params?)

对已注册 AID 做认证,返回连接参数。

const auth = await client.auth.authenticate({
  aid: 'alice.agentid.pub',
});

请求参数:

字段类型必填说明
aidstring否目标 AID;未传时使用当前本地身份

返回值:

{
  aid: string,
  access_token: string,
  refresh_token?: string,
  expires_at?: number,
  gateway: string,
}

5.3 client.auth.uploadAgentMd(content)

上传当前 AID 的 agent.md。

await client.auth.uploadAgentMd('# Alice\n');

5.4 client.auth.downloadAgentMd(aid)

下载指定 AID 的 agent.md。

const markdown = await client.auth.downloadAgentMd('bob.agentid.pub');

5.5 证书相关 RPC

client.auth.downloadCert(params?)

await client.auth.downloadCert({ aid: 'alice.agentid.pub' });

client.auth.requestCert(params)

await client.auth.requestCert({ csr: '...' });

client.auth.renewCert(params?)

await client.auth.renewCert();

client.auth.rekey(params?)

await client.auth.rekey();

client.auth.trustRoots(params?)

await client.auth.trustRoots();

6. P2P 消息接口

7.1 发送消息 message.send

await client.call('message.send', {
  to: 'bob.agentid.pub',
  payload: {
    type: 'text',
    text: 'hello',
  },
});

默认行为:

  • encrypt 默认是 true
  • SDK 优先使用 prekey 加密
  • 若取不到 prekey,会按配置决定是否允许降级到 long_term_key

显式明文发送:

await client.call('message.send', {
  to: 'bob.agentid.pub',
  payload: { type: 'text', text: 'plain text' },
  encrypt: false,
});

参数限制:

  • 不允许传 persist
  • 不允许传消息级 delivery_mode
  • 不允许传 queue_routing
  • 不允许传 affinity_ttl_ms
  • to 不能是 group.{issuer}

7.2 拉取消息 message.pull

const result = await client.call('message.pull', {
  after_seq: 0,
  limit: 50,
});

SDK 自动行为:

  • 自动注入当前实例 device_id 和 slot_id
  • 自动解密返回消息
  • 自动更新 contiguous seq
  • 自动发送 message.ack

7. 群组消息接口

8.1 发送群消息 group.send

await client.call('group.send', {
  group_id: 'group-123',
  payload: {
    type: 'text',
    text: 'hello group',
  },
});

默认行为:

  • encrypt 默认是 true
  • 自动调用群组 E2EE 加密
  • 自动附加客户端签名

8.2 拉取群消息 group.pull

const result = await client.call('group.pull', {
  group_id: 'group-123',
  after_seq: 0,
  limit: 50,
});

SDK 自动行为:

  • 自动解密返回消息
  • 自动更新群消息 contiguous seq
  • 自动发送 group.ack_messages

8.3 自动编排

SDK 内部已自动处理以下群组 E2EE 场景:

  • group.create 后自动创建首个 epoch
  • group.add_member 后自动轮换 epoch 并分发新密钥
  • group.kick 后自动轮换 epoch
  • 审批通过后自动轮换 epoch 并分发新密钥
  • 群消息解密失败时自动尝试密钥恢复

8. 事件

常用事件:

事件名说明
connection.state连接状态变化
connection.error连接错误
connection.challengeGateway challenge
token.refreshedtoken 自动刷新成功
message.receivedP2P 消息,SDK 已优先自动解密
message.undecryptable无法解密的 P2P 消息
message.recalled撤回事件
group.message_created群消息,SDK 已优先自动解密
group.message_undecryptable无法解密的群消息
group.changed群组事件
notification未被 SDK 特殊处理的通知
e2ee.degradedP2P 加密降级事件
e2ee.orchestration_error群组 E2EE 编排错误

示例:

client.on('connection.state', (evt) => {
  console.log('connection.state', evt);
});

client.on('message.received', (evt) => {
  console.log('message.received', evt);
});

client.on('group.message_created', (evt) => {
  console.log('group.message_created', evt);
});

9. 错误类型

常用错误类型:

  • AUNError
  • ConnectionError
  • TimeoutError
  • AuthError
  • PermissionError
  • ValidationError
  • NotFoundError
  • RateLimitError
  • StateError
  • SerializationError
  • SessionError
  • GroupError
  • E2EEError

示例:

try {
  await client.call('message.send', {
    to: 'bob.agentid.pub',
    payload: { type: 'text', text: 'hello' },
  });
} catch (error) {
  if (error instanceof ValidationError) {
    console.error('参数错误', error.message);
  } else if (error instanceof AuthError) {
    console.error('认证错误', error.message);
  } else if (error instanceof ConnectionError) {
    console.error('连接错误', error.message);
  } else {
    console.error(error);
  }
}

10. 最小接入模板

import { AUNClient } from '@agentunion/fastaun-browser';

export async function bootstrapAun() {
  const client = new AUNClient({
    aunPath: 'aun-web',
    discoveryPort: 18443,
    seedPassword: 'demo-seed',
  });

  client.on('connection.state', (evt) => {
    console.log('[aun] state', evt);
  });

  const auth = await client.auth.authenticate({
    aid: 'alice.agentid.pub',
  });

  await client.connect({
    ...auth,
    slot_id: 'web-main',
  });

  return client;
}

版本与发布记录

@agentunion/fastaun-browser 共发布 60 个版本,首次发布于 2026/05/07;过去 12 个月发布了 60 个版本。

版本发布日期距今
0.5.33latest2026/11/071 天前
0.5.322026/11/062 天前
0.5.312026/11/044 天前
0.5.302026/11/025 天前
0.5.292026/10/318 天前
0.5.282026/10/309 天前

关于 @agentunion/fastaun-browser 的常见问题

@agentunion/fastaun-browser 是什么?

用于浏览器的 AUN 协议 Core SDK

如何安装 @agentunion/fastaun-browser?

在项目目录运行 npm install @agentunion/fastaun-browser,或使用 pnpm add / yarn add / bun add。

@agentunion/fastaun-browser 支持 TypeScript 吗?

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

@agentunion/fastaun-browser 支持 ESM 还是 CommonJS?

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

@agentunion/fastaun-browser 可以商用吗?

@agentunion/fastaun-browser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@agentunion/fastaun-browser 的下载量有多少?

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

@agentunion/fastaun-browser 还在维护吗?

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