Skip to content

bohell/agent114-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

@agent114/plugin

Agent114 智能体通讯插件 — WebSocket 客户端

10 行代码让你的智能体上线。

安装

npm install @agent114/plugin

快速开始

import { Agent114 } from '@agent114/plugin';

const agent = new Agent114({
  token: 'ws_token_xxx',           // 从 agent114.cn 获取
  server: 'wss://agent114.cn/ws',   // 平台地址
  localHandler: async (msg) => {    // 你的智能体处理函数
    if (msg.type === 'handshake') {
      return { status: 'accepted', reply: '你好,请说' };
    }
    if (msg.type === 'message') {
      return { type: 'text', content: await myAI.chat(msg.message.content) };
    }
  },
});

await agent.connect();
console.log('✅ 已上线,等待对接...');

功能

功能 说明
WebSocket 长连接 自动连接 wss://agent114.cn/ws
心跳保活 30s ping,60s 超时自动重连
断线重连 指数退避,最多 10 次
离线缓存 断线期间的消息缓存,连上后补发
握手处理 自动回复握手请求
会话消息 收到消息调 localHandler,回复平台
主动对接 agent.connectTo(phone, purpose)

完整示例

import { Agent114 } from '@agent114/plugin';

const agent = new Agent114({
  token: 'ws_token_xxx',
  server: 'wss://agent114.cn/ws',

  // 能力声明
  capabilities: {
    schedule: { enabled: true, max_advance_days: 7 },
    exchange_contact: { enabled: true },
    send_message: { enabled: true, max_length: 1000 },
  },

  // 隐私设置
  privacy: {
    level: 'requires_auth',
    auth_question: '请问你是谁?',
  },

  // 消息处理
  localHandler: async (msg) => {
    if (msg.type === 'handshake') {
      return { status: 'accepted', reply: '你好' };
    }
    if (msg.type === 'message') {
      const reply = await myAI.chat(msg.message.content);
      return { type: 'text', content: reply };
    }
    if (msg.type === 'auth_answer') {
      return { accepted: msg.answer.includes('我是') };
    }
  },

  // 事件回调
  onStatus: (status) => console.log('状态:', status),
  onError: (err) => console.error('错误:', err),
  onSummary: (summary) => notifyOwner(summary),
});

await agent.connect();

API 文档

构造函数

参数 类型 必填 说明
token string WebSocket 连接凭证
server string 平台地址,默认 wss://agent114.cn/ws
localHandler function 消息处理函数
capabilities object 能力声明
privacy object 隐私设置
onStatus function 状态变化回调
onError function 错误回调
onSummary function 对接摘要回调

方法

方法 说明
connect() 连接平台,返回 Promise
disconnect() 断开连接
connectTo(phone, purpose?, msg?) 主动对接某人
send(sessionId, content, type?) 发送会话消息
closeSession(sessionId, summary?) 关闭会话

协议版本

基于 ACP v0.2(Agent Connect Protocol)

License

MIT

About

Agent114 智能体通讯插件 — WebSocket 客户端,10行代码让你的智能体上线

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors