@airops/airops-js

中文文档
2.3.0发布于 2024/08/29MITTypeScriptESM + CJS近 30 天下载 3996

与 AirOps API 互动的 JavaScript SDK

中文使用文档
文档目录10 节
  1. 安装
  2. 服务器配置
  3. 示例节点.js后端配置.
  4. 初始化 SDK
  5. 使用量
  6. 执行 App
  7. 拉结果同步
  8. 聊天流
  9. 处理错误
  10. 需要帮忙吗?

@ airops/ airops-js (英语)

使用 空中行动 API 在您的客户端应用程序中使用我们的JavaScript SDK.

npm

开始

安装

 npm i @airops/airops-js

或使用CDN:

<script src=“https://cdn.jsdelivr.net/npm/@airops/airops-js/dist/index.umd.min.js”></script>

服务器配置

要使用 SDK 认证我们的 API, 您需要三条信息: 您的工作空间 ID、 API 密钥和用户 id(应用程序中用户的识别符). 首先,使用 API 键将您的用户 ID 在您的后端服务器上散开 。 这将导致 对您的 API 密钥和用户 ID 组合独有的散列用户 ID 。 工作空间 id 和 API 密钥可见于您 工作空间设置.

示例节点.js后端配置.

const crypto = require('crypto');

const userIdHash = () => {
  const apiKey = 'YOUR_API_KEY';
  const userId = 'YOUR_USER_ID';

  // Convert your api key to a buffer
  const key = Buffer.from(apiKey, 'utf-8');

  // Hash the message using HMAC-SHA256 and the key
  const hash = crypto.createHmac('sha256', key).update(userId).digest('hex');

  return hash;
};

初始化 SDK

const airopsInstance = AirOps.identify({
  userId: 'YOUR_USER_ID',
  workspaceId: 'YOUR_WORKSPACE_ID',
  hashedUserId: 'YOUR_USER_ID_HASH',
});

请注意,授权是 没有 需要执行公共应用程序。 如果你和公众合作 没有争论:

const airopsInstance = new AirOps();

使用量

一旦成功初始化了SDK,就可以开始使用可用的方法与我们的API互动. 注意这些方法会回报承诺.

执行 App

SDK提供了一个执行应用程序的方法. 要流出应用结果, 您需要启用流并传递回调函数到执行方法 。 可以选择的是,您可以通过额外的回调功能,以便在应用程序完成后获得通知.

// Execute an app
const response = await airopsInstance.apps.execute({
  appId: 1,
  version: 1,
  payload: {
    inputs: {
      name: 'XXXXYYYYZZZZ',
    },
  },
  stream: true, // Optional - Default false
  streamCallback: (data: { content: string }) => {
    // Do something with the data
  }, // Optional, required if stream is true
  streamCompletedCallback: (data: { content: string }) => {
    // Do something with the data
  }, // Optional
});

// Wait for result
const result = await response.result();
// Do something with result.output

// Cancel execution
await response.cancel();

回应

执行方法的响应将包含执行id, 用于稍后获取结果, 以及等待结果或取消执行的两个方法:

interface ExecuteResponse {
  executionId: number;
  result: () => Promise<AppExecution>;
  cancel: () => Promise<void>;
}

interface AppExecution {
  airops_app_id: number;
  id: number;
  status: string;
  output: string;
  stream_channel_id: string;
}

结果方法采用拉逻辑来计算结果,但超时 10分钟。如果您想要执行自己的拉动逻辑,您可以使用下面描述的获取Results方法.

拉结果同步

您可以使用 GetResults 方法执行自己的拉动逻辑 .

const result = await airopsInstance.apps.getResults({
  executionId: response.executionId,
});

聊天流

对于聊天Apps,你可以使用 chatStream 允许您向 Chat App 发送消息的方法.

const response = await airopsInstance.apps.chatStream({
  appId: 2,
  message,
  inputs: {
    name: 'XXXXYYYYZZZZ',
  },
  streamCallback: ({ action: ChatAction, ...data) => {
    // ChatAction can either be 'agent-response', 'agent-action' or 'agent-action-error'
    // data will have different values depending on the action:
    //   - agent-response: { token: string, stream_finished: boolean, result :string }
    //   - agent-action: { tool: string, tool_input: Record<string, string> }
    //   - agent-action-error: { tool: string, tool_error: string }
  },
  streamCompletedCallback: (data: { result: string }) => {
    // do something with data.result
  },
  ...(sessionId && { sessionId }), // optionally pass sessionId to continue chat.
});

// Wait for result
const result = await response.result();
// Do something with result.result

// Cancel execution
await response.cancel();

// Use session id to continue chat
response.sessionId;

回应

聊天Stream方法的响应会包含会话Id和一个等待响应的结果方法. 为了继续聊天,将会话Id和消息一起通过.

export interface ChatStreamResponse {
  sessionId: string;
  result: Promise<{ result: string }>; // result is a promise that resolves when the execution is completed.
}

处理错误

try {
  await airopsInstance.apps.execute({
    appId: 1,
    version: 4,
    payload: {
      inputs: { name: 'XXXXYYYYZZZZ' },
    },
  });
} catch (e) {
  // Do something with error.message
}

需要帮忙吗?

加入我们的AirOps构建器 松动 社区.

版本与发布记录

@airops/airops-js 共发布 12 个版本,首次发布于 2023/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.0latest2024/08/292 年前
2.2.02023/08/043 年前
2.1.02023/07/203 年前
2.0.02023/07/203 年前
1.4.02023/07/193 年前
1.3.02023/07/053 年前

关于 @airops/airops-js 的常见问题

@airops/airops-js 是什么?

与 AirOps API 互动的 JavaScript SDK

如何安装 @airops/airops-js?

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

@airops/airops-js 支持 TypeScript 吗?

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

@airops/airops-js 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@airops/airops-js 可以商用吗?

@airops/airops-js 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@airops/airops-js 的下载量有多少?

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

@airops/airops-js 还在维护吗?

最新版本 2.3.0 发布于 2024/08/29(2 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/06/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。