@ airops/ airops-js (英语)
使用 空中行动 API 在您的客户端应用程序中使用我们的JavaScript SDK.
开始
安装
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构建器 松动 社区.