AgentSdk API 参考 · JS / 浏览器

AgentSdk API 参考 · JS / 浏览器

JS SDK 主入口是 AgentSdk / createAgentSdk(按 appId 单例)。当前公开能力仅 超视距遥操 + 扩展技能。

创建与获取

ts
import { createAgentSdk, AgentSdk } from 'linksoul-agentsdk';

// 工厂(推荐)
const sdk = createAgentSdk(appId, {
  authProvider: async () => {
    const res = await fetch('/api/ws-auth', { method: 'POST' });
    const { url } = await res.json();
    return { url };
  },
});

// 等价写法
const sdk2 = AgentSdk.create(appId, options);
AgentSdk.get(appId);            // AgentSdk | undefined
AgentSdk.listAppIds();          // string[]
成员签名说明
createAgentSdk(appId, options)(appId: string, options: AgentSdkOptions) => AgentSdk创建实例(同 appId 复用)
AgentSdk.create同上静态方法,等价入口
AgentSdk.get(appId)(appId: string) => AgentSdk | undefined获取已建实例
AgentSdk.listAppIds()() => string[]所有已注册 appId

实例成员

ts
class AgentSdk {
  readonly appId: string;
  readonly options: AgentSdkOptions;
  readonly connection: AgentConnection;

  on<K>(event: K, listener: (value: ConnectionEvents[K]) => void): () => void;
  connect(): Promise<void>;
  close(): void;
  release(): void;
  createTeleopSession(options: TeleopOptions): TeleopSession;
  createExtSkillSession(options: ExtSkillOptions): ExtSkillSession;
  send(message: Record<string, unknown>): void;
  newEventId(): string;
}
方法说明
on(event, listener)订阅连接级事件,返回取消订阅函数
connect()调 authProvider 换取签名 URL 并建立 WebSocket
close()关闭连接、停止重连与保活
release()close() + 从单例表移除
createTeleopSession(options)创建遥操会话,见 Teleop API
createExtSkillSession(options)创建扩展技能会话,见 ExtSkill API
send(message)发送一条 JSON 报文(未连接会抛错)
newEventId()生成 event_<base36>_<seq>_<hex> 事件 ID

连接事件 ConnectionEvents

ts
type ConnectionEvents = {
  connected: void;
  disconnected: { willReconnect: boolean };
  reconnecting: { attempt: number };
  message: { data: unknown };
  sent: { data: Record<string, unknown> };
  error: Error;
  keepalive: { state: 'idle' | 'probing' | 'healthy' | 'timeout'; probes: number };
};

AgentSdkOptions(= ConnectionOptions)

ts
type ConnectionOptions = {
  authProvider: AuthProvider;                       // 必填:换取已签名代理 URL
  socketFactory?: SocketFactory;                    // 可注入自定义 WebSocket(测试用)
  reconnect?: boolean | { delayMs?: number };       // 默认开启,3s
  keepAlive?: KeepAliveOptions;
};

type AuthProvider = () => Promise<{ url: string; protocols?: string | string[] }>;

⚠️ 未提供的 API

与 Java / Python / iOS SDK 不同,JS SDK 没有 registerAudio2Llm 等 8 种被动回调、没有 registerTaskFlow(任务流)、没有 querySkill / queryState。详情见 被动回调 API 与 主动请求 API。