メインコンテンツへスキップ
Episodes APIは、エピソード(イベント)を時系列で記録・取得するためのインターフェースです。Q&A履歴、ナレッジの変更履歴、ユーザーアクションなどを追跡できます。

概要

const client = new NdxClient({
  apiKey: process.env.NEURADEX_API_KEY,
  projectId: 'your-project-id',
});

// Episodes APIにアクセス
const episodes = await client.episodes.list({ timeRange: 'today' });
await client.episodes.create({ ... });

メソッド一覧

メソッド説明
list(options?)エピソード一覧を取得
get(id)特定のエピソードを取得
create(input)新しいエピソードを記録
getChildren(id)子エピソードを取得
getBySession(sessionId)セッション内のエピソードを取得
getRecent(limit?)最近のエピソードを取得
getToday()今日のエピソードを取得
getThisWeek()今週のエピソードを取得
search(query, options?)エピソードをベクトル検索

list()

条件を指定してエピソード一覧を取得します。

パラメータ

options.timeRange
string
時間範囲プリセット: 'today' | 'this_week' | 'this_month'
options.from
string
開始日時(ISO 8601形式)
options.to
string
終了日時(ISO 8601形式)
options.episodeType
string
エピソードタイプでフィルタ: 'question' | 'answer' | 'knowledge_created' など
options.actorType
string
アクタータイプでフィルタ: 'user' | 'agent' | 'system'
options.actorId
string
アクターIDでフィルタ
options.sessionId
string
セッションIDでフィルタ
options.channel
string
チャネルでフィルタ: 'widget' | 'api' | 'mcp'
options.limit
number
デフォルト:"50"
取得する最大件数
options.page
number
デフォルト:"1"
ページ番号

戻り値

interface EpisodeListResponse {
  data: Episode[];
  total: number;
  page: number;
  limit: number;
}

interface Episode {
  id: string;
  episodeType: string;
  content: string;
  actorType: string;
  actorId: string | null;
  actorName: string | null;
  scopeType: string;
  scopeId: string;
  channel: string | null;
  sessionId: string | null;
  parentEpisodeId: string | null;
  relatedKnowledgeIds: string[];
  changeReason: string | null;
  previousValue: object | null;
  occurredAt: string;
  createdAt: string;
}

使用例

// 今日のエピソード
const result = await client.episodes.list({ timeRange: 'today' });

// 特定ユーザーのエピソード
const result = await client.episodes.list({
  actorType: 'user',
  actorId: 'user-123',
  limit: 100,
});

// 日付範囲で取得
const result = await client.episodes.list({
  from: '2025-01-01T00:00:00Z',
  to: '2025-01-07T23:59:59Z',
});

// 質問エピソードのみ
const result = await client.episodes.list({
  episodeType: 'question',
});

get()

特定のエピソードを取得します。

パラメータ

id
string
必須
エピソードID

使用例

const episode = await client.episodes.get('episode-123');

console.log(episode.content);
console.log(episode.actorName);
console.log(episode.occurredAt);

create()

新しいエピソードを記録します。

パラメータ

actorType
string
必須
アクタータイプ: 'user' | 'agent' | 'system'
episodeType
string
必須
エピソードタイプ: 'question' | 'answer' | 'feedback' など
content
string
必須
エピソードの内容
scopeType
string
必須
スコープタイプ: 'project' | 'organization'
scopeId
string
必須
スコープID(プロジェクトIDまたは組織ID)
actorId
string
アクターID
actorName
string
アクター名(表示用)
channel
string
チャネル: 'widget' | 'api' | 'mcp'
sessionId
string
セッションID(会話をグループ化)
parentEpisodeId
string
親エピソードID(回答を質問に紐付けなど)
関連するナレッジのID

使用例

// ユーザーの質問を記録
const question = await client.episodes.create({
  actorType: 'user',
  actorId: 'user-123',
  actorName: 'John Doe',
  episodeType: 'question',
  content: 'パスワードをリセットする方法は?',
  scopeType: 'project',
  scopeId: 'project-abc',
  channel: 'widget',
  sessionId: 'session-xyz',
});

// エージェントの回答を記録
const answer = await client.episodes.create({
  actorType: 'agent',
  actorName: 'Support Bot',
  episodeType: 'answer',
  content: 'パスワードのリセットは、ログイン画面の「パスワードを忘れた」リンクから行えます。',
  scopeType: 'project',
  scopeId: 'project-abc',
  channel: 'widget',
  sessionId: 'session-xyz',
  parentEpisodeId: question.id,  // 質問に紐付け
  relatedKnowledgeIds: ['knowledge-1', 'knowledge-2'],
});

getChildren()

指定したエピソードの子エピソード(返信など)を取得します。

パラメータ

id
string
必須
親エピソードID

使用例

// 質問に対する回答を取得
const children = await client.episodes.getChildren('question-episode-id');

for (const child of children) {
  console.log(`回答: ${child.content}`);
}

getBySession()

セッション内のすべてのエピソードを取得します。会話履歴の取得に便利です。

パラメータ

sessionId
string
必須
セッションID

使用例

// 会話履歴を取得
const result = await client.episodes.getBySession('session-123');

for (const episode of result.data) {
  const prefix = episode.episodeType === 'question' ? 'Q' : 'A';
  console.log(`${prefix}: ${episode.content}`);
}

getRecent()

最近のエピソードを取得します。

パラメータ

limit
number
デフォルト:"10"
取得する件数

使用例

// 最近10件
const result = await client.episodes.getRecent();

// 最近50件
const result = await client.episodes.getRecent(50);

getToday() / getThisWeek()

今日・今週のエピソードを取得するショートカットメソッドです。

使用例

// 今日のエピソード
const today = await client.episodes.getToday();

// 今週のエピソード
const thisWeek = await client.episodes.getThisWeek();

ベクトル検索でエピソードを検索します。「パスワードリセット」「先週のエラー」など、あいまいなクエリでも関連するエピソードを発見できます。

パラメータ

query
string
必須
検索クエリ
options.limit
number
結果の最大件数

戻り値

interface EpisodeSearchResponse {
  results: EpisodeSearchResult[];
  query: string;
}

interface EpisodeSearchResult {
  id: string;
  episodeType: string;
  content: string;
  actorType: string;
  actorName: string | null;
  occurredAt: string;
  channel: string | null;
  score: number;
}

使用例

// エピソードを検索
const result = await client.episodes.search('パスワードリセット');

for (const episode of result.results) {
  console.log(`${episode.actorName}: ${episode.content} (スコア: ${episode.score})`);
}

// 件数を指定
const result = await client.episodes.search('デプロイエラー', { limit: 5 });

エピソードタイプ

タイプ説明
questionユーザーからの質問
answerエージェントの回答
feedbackユーザーのフィードバック
knowledge_createdナレッジの作成
knowledge_updatedナレッジの更新
knowledge_deletedナレッジの削除
search検索の実行

ユースケース

会話履歴の保存

async function recordConversation(
  sessionId: string,
  question: string,
  answer: string,
  userId: string,
) {
  // 質問を記録
  const questionEpisode = await client.episodes.create({
    actorType: 'user',
    actorId: userId,
    episodeType: 'question',
    content: question,
    scopeType: 'project',
    scopeId: projectId,
    channel: 'api',
    sessionId,
  });

  // 回答を記録
  await client.episodes.create({
    actorType: 'agent',
    actorName: 'Assistant',
    episodeType: 'answer',
    content: answer,
    scopeType: 'project',
    scopeId: projectId,
    channel: 'api',
    sessionId,
    parentEpisodeId: questionEpisode.id,
  });
}

アクティビティダッシュボード

async function getActivityStats() {
  const today = await client.episodes.getToday();

  const stats = {
    questions: today.data.filter(e => e.episodeType === 'question').length,
    answers: today.data.filter(e => e.episodeType === 'answer').length,
    uniqueUsers: new Set(
      today.data
        .filter(e => e.actorType === 'user')
        .map(e => e.actorId)
    ).size,
  };

  console.log(`今日の質問数: ${stats.questions}`);
  console.log(`今日の回答数: ${stats.answers}`);
  console.log(`ユニークユーザー: ${stats.uniqueUsers}`);

  return stats;
}

次のステップ

Knowledge API

ナレッジの管理

Memory API

コンテキスト組み立て