完全なサンプル
Beeuni SDK でセッションを実行し、応答を受け取ります。
スクリプトを full-example.ts として保存します。既存のプロジェクトにセッションを作成し、イベントストリームを開いてプロンプトを送信し、会話履歴を読み取ります。
import { createBeeuni, narrowChatEvent } from '@beeuni/sdk';
async function main() {
const apiKey = process.env.BEEUNI_API_KEY;
const projectId = process.env.BEEUNI_PROJECT_ID;
if (!apiKey || !projectId) {
throw new Error('Set BEEUNI_API_KEY and BEEUNI_PROJECT_ID.');
}
const beeuni = createBeeuni({
backendUrl: 'https://api.beeuni.ai/v1',
getToken: async () => apiKey,
});
const created = await beeuni.project(projectId).sessions.create({
name: 'SDK example',
});
console.log('Session:', created.session_id);
const session = beeuni.session(projectId, created.session_id);
const { runtimeSessionId } = await session.ensureReady();
let finish!: () => void;
let failure: string | undefined;
let submitted = false;
const finished = new Promise<void>((resolve) => { finish = resolve; });
const stream = await session.stream({
onEvent: (event) => {
const e = narrowChatEvent(event);
if (!e || !submitted || !('sessionID' in e) || e.sessionID !== runtimeSessionId) return;
if (e.type === 'message.part.updated') console.log(e.part);
if (e.type === 'session.error') {
failure = JSON.stringify(e.error ?? 'Session failed.');
finish();
}
if (e.type === 'session.idle') finish();
},
});
const timer = setTimeout(() => {
failure = 'Reply timed out. Check the session for pending permissions or questions.';
finish();
}, 180_000);
try {
submitted = true;
await session.send('Describe your capabilities in one paragraph.');
await finished;
if (failure) throw new Error(failure);
console.log(JSON.stringify(await session.messages(), null, 2));
} finally {
clearTimeout(timer);
stream.close();
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
実行
Account → Tokens で API キーを作成し、その認証情報でアクセスできるプロジェクトを指定します。プロジェクトに設定されたモデルを使用し、セッションの実行にはクレジットを消費します。
npm install @beeuni/sdk
BEEUNI_API_KEY=your-api-key BEEUNI_PROJECT_ID=your-project-id npx tsx full-example.ts
次のステップ
React の画面には React hooks を使用します。権限要求、質問、起動エラーについては Sessions を参照してください。