airops js
1.0.0
通过我们的 JavaScript SDK 在您的客户端应用程序中使用 AirOps API。
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 进行哈希处理。这将生成一个哈希用户 ID,该用户 ID 对于您的 API 密钥和用户 ID 组合来说是唯一的。工作区 ID 和 API 密钥可以在您的工作区设置中找到。
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 ;
} ;
const airopsInstance = AirOps . identify ( {
userId : 'YOUR_USER_ID' ,
workspaceId : 'YOUR_WORKSPACE_ID' ,
hashedUserId : 'YOUR_USER_ID_HASH' ,
} ) ;
请注意,执行公共应用程序不需要授权。如果您正在使用不带参数的公共初始化:
const airopsInstance = new AirOps ( ) ;
成功初始化 SDK 后,您就可以开始使用可用的方法与我们的 API 进行交互。请注意,这些方法将返回承诺。
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 ;
}
result 方法实现了结果的拉取逻辑,超时时间为10 分钟。如果您想实现自己的拉取逻辑,可以使用下面描述的 getResults 方法。
您可以使用 getResults 方法实现自己的拉取逻辑。
const result = await airopsInstance . apps . getResults ( {
executionId : response . executionId ,
} ) ;
对于聊天应用程序,您可以使用chatStream
方法,该方法允许您向聊天应用程序发送消息。
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 ;
回复
chatStream 方法的响应将包含 sessionId 和等待响应的结果方法。为了继续聊天,请传递 sessionId 和消息。
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 构建者 Slack 社区。