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를 해시합니다. 그러면 API 키와 사용자 ID 조합에 고유한 해시된 사용자 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 ;
}
결과 메소드는 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 빌더 슬랙 커뮤니티에 참여하세요.