@airops/airops-js

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

JavaScript SDK for interacting with AirOps API

原始使用文档
文档目录10 节
  1. Installation
  2. Server configuration
  3. Example Node.js backend configuration.
  4. Initialize the SDK
  5. Usage
  6. Execute an App
  7. Pull results async
  8. Chat Stream
  9. Error handling
  10. Need help?

@airops/airops-js

Use AirOps API in your client application with our JavaScript SDK.

npm

Getting Started

Installation

 npm i @airops/airops-js

or use the CDN:

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

Server configuration

To authenticate with our API using the SDK, you'll need three pieces of information: your workspace id, API key and user id (identifier for the user in your application). First, use the API key to hash your user id on your back-end server. This will result in a hashed user id that is unique to your API key and user id combination. Workspace id and API key can be found in your workspace settings.

Example Node.js backend configuration.

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;
};

Initialize the SDK

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

Note that authorization is not required to execute public apps. If you're working with public initialize without arguments:

const airopsInstance = new AirOps();

Usage

Once you have successfully initialized the SDK, you can begin using the methods available to interact with our API. Note that the methods will return promises.

Execute an App

The SDK provides a method for executing an app. In order to stream the app results you will need to enable stream and pass a callback function to the execute method. Optionally you can pass an extra callback function to get a notification when the app is finished.

// 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();

Response

The response from the execute method will contain the execution id that can be used to retrieve results later along with two methods to wait for results or cancel the execution:

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;
}

The result method implements pulling logic for results with a timeout of 10 minutes. If you want to implement your own pulling logic you can use the getResults method described below.

Pull results async

You can implement your own pulling logic using the getResults method.

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

Chat Stream

For Chat Apps, you can use the chatStream method which allows you to send messages to the 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;

Response

The response from the chatStream method will contain the sessionId and a result method to wait for the response. In order to continue with the chat pass the sessionId along with the message.

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

Error handling

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

Need help?

Join our AirOps builder slack community.

版本与发布记录

@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 是什么?

JavaScript SDK for interacting with AirOps API

如何安装 @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 和提交记录判断。