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 (アプリケーション内のユーザーの識別子) の 3 つの情報が必要です。まず、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 と対話するために使用できるメソッドの使用を開始できます。メソッドは Promise を返すことに注意してください。
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 と、結果を待つか実行をキャンセルするための 2 つのメソッドが含まれます。
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 コミュニティに参加してください。