@aishift/widget-sdk
v2.2.2
Published
# 使用例
Readme
AI-Shift widget SDK
使用例
import { createClient, Client, MesaageFormat } from '@aishift/widget-sdk'
async function asyncFunction() {
// SDK クライアント作成
const client = await createClient({
tenantId: 'tenantId',
projectId: 'projectId',
tenantSubDomain: 'tenantSubDomain',
updateMessageCallback: (type, messageId, message, isEnd) => {
// メッセージが更新されたときのコールバック
},
addMessageCallback: message => {
// メッセージが追加されたときのコールバック
},
})
// メッセージ送信
client.send({message: "こんにちは"}, onError: () => {
console.error(error)
})
// promptId つきでメッセージ送信
client.sendWithPrompt({
promptId: 'promptId',
scenarioVersion: '1',
message: "こんにちは",
errorCallback: error => {
console.error(error);
},
});
// 過去のメッセージ取得
const pastMessages: MessageFormat[] = await client.getPastMessages({limit: 20, at: 1608623909884})
// ウェルカムメッセージ取得
const { welcomeMessage, recommendations } = client.getWelcomeMessage();
// 初めてログインしたユーザかどうか
client.isLoggedIn()
// シナリオjson取得
const scenario = await client.getScenarioJson()
// シナリオ遷移状態保存
client.storeScenarioState({ version: '1', sceneId: 'sceneId' });
// シナリオ遷移状態取得
const scenarioState = client.getScenarioState()
}
APIs
createClient
SDK クライアントを作成する
インターフェース
createClient: ({
tenantId,
tenantSubDomain,
projectId,
updateMessageCallback,
addMessageCallback,
}: {
tenantId: string;
tenantSubDomain: string;
projectId: string;
updateMessageCallback: (
type: string,
messageId: string,
message: MessageFormt,
isEnd: boolean
) => void;
addMessageCallback: (message: MessageFormat) => void;
}) => Promise<Client>;引数
tenantId: テナント IDtenantSubDomain: テナントサブドメインprojectId: プロジェクト IDupdateMessageCallback: LLM による回答メッセージに変更があった時に呼ばれるコールバックaddMessageCallback: LLM による回答メッセージが新規作成された時に呼ばれるコールバック
返り値
Client: SDK クライアント
MessageFromat 型
{
at: number,
type: "message",
messages: [
{
attachment: {
mediaType: "plainText",
payload: string,
referenceUrls: {title: string, link: string}[],
followUpUrls: {title: string, link: string}[]
}
}
],
sender: {
id: string,
name: string,
type: 'Bot' | 'Customer' | 'Operator'
},
isWelcome: boolean,
referer: string,
senderMessageId: string
isEnd: boolean
}at: メッセージが送信された時間 (unixtime)messages[index].attachment.payload: メッセージ本文messages[index].attachment.referenceUrls: LLM が回答に使用したリソースの URL のリストmessages[index].attachment.followUpUrls: LLM 提案する回答をフォローするリソースの URL のリストsender.type: ボット or カスタマー (チャットユーザー) or オペレーターsenderMessageId: メッセージごとの一意な IDisEnd: メッセージの更新が終わったかを判定するフラグ
getPastMessages
at より前のメッセージを最大 limit だけ取得する
インターフェース
getPastMessages: ({ limit, at }: { limit: number; at: number }) => Promise<MessageFormat[]>;引数
limit: 取得するメッセージの最大数at: 取得したいメッセージの最新時刻 (unixtime)
返り値
messages:MessageFormat型オブジェクトの配列
send
LLM にメッセージを送信する
インターフェース
send: ({ message, errorCallback }: { message: string; errorCallback: (error: Error) => void }) =>
string;引数
message: LLM に送信するメッセージerrorCallback: エラーが発生したときに呼ばれるコールバック
返り値
senderMessageId: メッセージごとの一意な ID
sendWithPrompt
LLM にプロンプトを指定してメッセージを送信する
インターフェース
sendWithPrompt: ({
message,
promptId,
scenarioVersion,
errorCallback,
}: {
message: string;
promptId: string;
scenarioVersion: string;
errorCallback: (error: Error) => void;
}) => string;引数
message: LLM に送信するメッセージpromptId: プロンプトの IDscenarioVersion: 固定シナリオのバージョンerrorCallback: エラーが発生したときに呼ばれるコールバック
返り値
senderMessageId: メッセージごとの一意な ID
getWelcomeMessage
ウェルカムメッセージ取得
インターフェース
getWelcomeMessage: () => {welcomeMessage: string; recommendations: string[]};返り値
- welcomeMessage: ウェルカムメッセージ
- recommendations: オススメ質問の配列
isLoggedIn
ログイン済みかどうかを返す
isLoggedIn: () => Promise<boolean>;返り値
isLoggedIn: ログイン済み -> true 未ログイン -> false
getCustomerInfo
カスタマー情報の取得をする
getCustomerInfo: () => Promise<{ customerId: string }>;返り値
customerId: チャットユーザーの ID
unsubscribeMessageCallback
メッセージ更新コールバックの解除する
unsubscribeMessageCallback: () => void;getScenarioJson
固定シナリオ JSON を返す
getScenarioJson: () => Promise<ScenarioJson>;返り値
scenarioJson: 固定シナリオ JSON
ScenarioJson 型
type ScenarioJson = {
scenario: Scene;
version: string;
};
type Scene = {
id: string;
title: string;
content: string;
imagePath: string;
children: Scene[];
mainMessage?: string;
promptId?: string;
messageOptions?: {
label: string;
value: string;
}[];
};getScenarioState
固定シナリオの遷移状態を取得する
getScenarioState: () => { version: string; sceneId: string } | undefined;返り値
version: 固定シナリオのバージョンsceneId: 固定シナリオのシーン ID
storeScenarioState
固定シナリオの遷移状態を保存する
storeScenarioState: ({ version, sceneId }: { version: string; sceneId: string }) => void;引数
version: 固定シナリオのバージョンsceneId: 固定シナリオのシーン ID
getVersion
SDK のバージョンを返す
getVersion: () => string;返り値
version: SDK のバージョン
