ストレージアダプター
KnowledgePulse は起動時にストレージバックエンドを選択するためのファクトリーパターンを使用しています。すべてのストアは同じ非同期インターフェースを実装しているため、バックエンドの切り替えにコード変更は不要で、環境変数のみで対応できます。
アーキテクチャ
┌─────────────────────────────────── ───────┐
│ createStore() │
│ (ファクトリー関数) │
├──────────────────────────────────────────┤
│ │
│ KP_STORE_BACKEND = "memory"(デフォルト)│
│ ┌────────────────────────────┐ │
│ │ MemorySkillStore │ │
│ │ MemoryKnowledgeStore │ │
│ │ MemoryReputationStore │ │
│ │ MemoryApiKeyStore │ │
│ │ MemoryRateLimitStore │ │
│ │ MemoryAuditLogStore │ │
│ └─────────────── ─────────────┘ │
│ │
│ KP_STORE_BACKEND = "sqlite" │
│ ┌────────────────────────────┐ │
│ │ SqliteSkillStore │ │
│ │ SqliteKnowledgeStore │ │
│ │ SqliteReputationStore │ │
│ │ SqliteApiKeyStore │ │
│ │ SqliteRateLimitStore │ │
│ │ SqliteAuditLogStore │ │
│ └────────────────────────────┘ │
│ │
│ KP_STORE_BACKEND = "qdrant"(将来) │
│ ┌────────────────────────────┐ │
│ │ (スケルトン --- 未実装) │ │
│ └────────────────────────────┘ │
│ │
└──────────────────────────────────────────┘
ストアファクトリー
createStore() 関数は環境変数 KP_STORE_BACKEND を読み取り、適切なストアセットを返します:
import { createStore } from "./store/factory.js";
const stores = await createStore();
// stores.skills — SkillStore
// stores.knowledge — KnowledgeStore
// stores.reputation — ReputationStore
// stores.apiKeys — ApiKeyStore
// stores.rateLimit — RateLimitStore
// stores.auditLog — AuditLogStore
AllStores インターフェース
interface AllStores {
skills: SkillStore;
knowledge: KnowledgeStore;
reputation: ReputationStore;
apiKeys: ApiKeyStore;
rateLimit: RateLimitStore;
auditLog: AuditLogStore;
}
すべてのストアメソッドは Promise を返すため、インターフェースはバックエンド非依存です。インメモリストアは即座に解決し、データベースバックエンドのストアは実際の I/O を実行します。
環境変数
| 変数 | 値 | デフォルト | 説明 |
|---|---|---|---|
KP_STORE_BACKEND | memory、sqlite | memory | ストレージバックエンドを選択 |
KP_SQLITE_PATH | ファイルパス | knowledgepulse.db | SQLite データベースファイルのパス(バックエンドが sqlite の場合のみ使用) |
Memory バックエンド
デフォルトのバックエンドはすべてのデータを JavaScript の Map オブジェクトに格納します。プロセスの再起動でデータは失われます。
最適な用途: 開発、テスト、CI パイプライン、デモ。
# 明示的に指定(デフォルトと同じ)
KP_STORE_BACKEND=memory bun run registry/src/index.ts