fix: fix all type error caused by identitydb
All checks were successful
CI / verify (push) Successful in 13s
Publish / publish (push) Successful in 22s

This commit is contained in:
2026-05-20 23:32:13 +09:00
parent b52d37170c
commit d8da1ec998
4 changed files with 215 additions and 75 deletions

View File

@@ -1,12 +1,26 @@
import { IdentityDB, extractFact, type Fact as IdentityFact, type FactExtractor } from 'identitydb'; import {
import type { BoxBrainMemoryStore, FactDraft, MemorySpace, ScheduleEntry, StoredFact } from './types'; IdentityDB,
extractFacts,
type Fact as IdentityFact,
type FactExtractor,
} from "identitydb";
import type {
BoxBrainMemoryStore,
FactDraft,
MemorySpace,
ScheduleEntry,
StoredFact,
} from "./types";
import { extractedToDraft } from "./utils";
function normalizeTopics(topics: string[]): string[] { function normalizeTopics(topics: string[]): string[] {
return [...new Set(topics.map((topic) => topic.trim()).filter(Boolean))]; return [...new Set(topics.map((topic) => topic.trim()).filter(Boolean))];
} }
function includesAnyTopic(fact: StoredFact, topics: string[]): boolean { function includesAnyTopic(fact: StoredFact, topics: string[]): boolean {
const normalized = new Set(normalizeTopics(topics).map((topic) => topic.toLowerCase())); const normalized = new Set(
normalizeTopics(topics).map((topic) => topic.toLowerCase()),
);
return fact.topics.some((topic) => normalized.has(topic.toLowerCase())); return fact.topics.some((topic) => normalized.has(topic.toLowerCase()));
} }
@@ -15,8 +29,16 @@ export class InMemoryMemoryStore implements BoxBrainMemoryStore {
readonly facts = new Map<string, StoredFact[]>(); readonly facts = new Map<string, StoredFact[]>();
readonly schedules = new Map<string, ScheduleEntry[]>(); readonly schedules = new Map<string, ScheduleEntry[]>();
async createSpace(input: { displayName: string; seedMessage: string; now: string }): Promise<MemorySpace> { async createSpace(input: {
const slug = input.displayName.toLowerCase().replace(/[^a-z0-9가-힣]+/gi, '-').replace(/^-|-$/g, '') || 'persona'; displayName: string;
seedMessage: string;
now: string;
}): Promise<MemorySpace> {
const slug =
input.displayName
.toLowerCase()
.replace(/[^a-z0-9가-힣]+/gi, "-")
.replace(/^-|-$/g, "") || "persona";
const space: MemorySpace = { const space: MemorySpace = {
id: `persona-${slug}-${crypto.randomUUID()}`, id: `persona-${slug}-${crypto.randomUUID()}`,
displayName: input.displayName, displayName: input.displayName,
@@ -48,7 +70,9 @@ export class InMemoryMemoryStore implements BoxBrainMemoryStore {
} }
async listFacts(spaceId: string): Promise<StoredFact[]> { async listFacts(spaceId: string): Promise<StoredFact[]> {
return [...(this.facts.get(spaceId) ?? [])].sort((a, b) => a.createdAt.localeCompare(b.createdAt)); return [...(this.facts.get(spaceId) ?? [])].sort((a, b) =>
a.createdAt.localeCompare(b.createdAt),
);
} }
async findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]> { async findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]> {
@@ -57,33 +81,56 @@ export class InMemoryMemoryStore implements BoxBrainMemoryStore {
return facts.filter((fact) => includesAnyTopic(fact, topics)); return facts.filter((fact) => includesAnyTopic(fact, topics));
} }
async saveScheduleEntries(spaceId: string, entries: ScheduleEntry[]): Promise<void> { async saveScheduleEntries(
const existing = (this.schedules.get(spaceId) ?? []).filter((entry) => !entries.some((incoming) => incoming.id === entry.id)); spaceId: string,
this.schedules.set(spaceId, [...existing, ...entries].sort((a, b) => a.startAt.localeCompare(b.startAt))); entries: ScheduleEntry[],
): Promise<void> {
const existing = (this.schedules.get(spaceId) ?? []).filter(
(entry) => !entries.some((incoming) => incoming.id === entry.id),
);
this.schedules.set(
spaceId,
[...existing, ...entries].sort((a, b) =>
a.startAt.localeCompare(b.startAt),
),
);
} }
async listScheduleEntries(spaceId: string, fromInclusive: string, toExclusive: string): Promise<ScheduleEntry[]> { async listScheduleEntries(
spaceId: string,
fromInclusive: string,
toExclusive: string,
): Promise<ScheduleEntry[]> {
return (this.schedules.get(spaceId) ?? []) return (this.schedules.get(spaceId) ?? [])
.filter((entry) => entry.startAt < toExclusive && entry.endAt > fromInclusive) .filter(
(entry) => entry.startAt < toExclusive && entry.endAt > fromInclusive,
)
.sort((a, b) => a.startAt.localeCompare(b.startAt)); .sort((a, b) => a.startAt.localeCompare(b.startAt));
} }
async deleteScheduleEntriesBefore(spaceId: string, cutoffExclusive: string): Promise<number> { async deleteScheduleEntriesBefore(
spaceId: string,
cutoffExclusive: string,
): Promise<number> {
const entries = this.schedules.get(spaceId) ?? []; const entries = this.schedules.get(spaceId) ?? [];
const kept = entries.filter((entry) => entry.endAt > cutoffExclusive); const kept = entries.filter((entry) => entry.endAt > cutoffExclusive);
this.schedules.set(spaceId, kept); this.schedules.set(spaceId, kept);
return entries.length - kept.length; return entries.length - kept.length;
} }
async ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact> { async ingestStatement(
const extracted = await extractFact(statement, extractor); spaceId: string,
return this.addFact(spaceId, { statement: string,
statement: extracted.statement ?? statement, extractor: FactExtractor,
topics: extracted.topics.map((t) => t.name), ): Promise<StoredFact[]> {
...(typeof extracted.confidence === 'number' ? { confidence: extracted.confidence } : {}), const extracted = await extractFacts(statement, extractor);
...(typeof extracted.source === 'string' ? { source: extracted.source } : {}), const stored: StoredFact[] = [];
...(extracted.metadata !== undefined && extracted.metadata !== null ? { metadata: extracted.metadata as Record<string, unknown> } : {}), for (const fact of extracted) {
}); stored.push(
await this.addFact(spaceId, extractedToDraft(fact, statement)),
);
}
return stored;
} }
} }
@@ -94,14 +141,22 @@ export interface IdentityDbMemoryStoreOptions {
export class IdentityDbMemoryStore implements BoxBrainMemoryStore { export class IdentityDbMemoryStore implements BoxBrainMemoryStore {
constructor(private readonly options: IdentityDbMemoryStoreOptions) {} constructor(private readonly options: IdentityDbMemoryStoreOptions) {}
async createSpace(input: { displayName: string; seedMessage: string; now: string }): Promise<MemorySpace> { async createSpace(input: {
const slug = input.displayName.toLowerCase().replace(/[^a-z0-9가-힣]+/gi, '-').replace(/^-|-$/g, '') || 'persona'; displayName: string;
seedMessage: string;
now: string;
}): Promise<MemorySpace> {
const slug =
input.displayName
.toLowerCase()
.replace(/[^a-z0-9가-힣]+/gi, "-")
.replace(/^-|-$/g, "") || "persona";
const spaceName = `persona-${slug}-${crypto.randomUUID()}`; const spaceName = `persona-${slug}-${crypto.randomUUID()}`;
const space = await this.options.db.upsertSpace({ const space = await this.options.db.upsertSpace({
name: spaceName, name: spaceName,
description: `BoxBrain persona space for ${input.displayName}`, description: `BoxBrain persona space for ${input.displayName}`,
metadata: { metadata: {
boxbrainType: 'persona-space', boxbrainType: "persona-space",
displayName: input.displayName, displayName: input.displayName,
seedMessage: input.seedMessage, seedMessage: input.seedMessage,
createdAt: input.now, createdAt: input.now,
@@ -118,9 +173,22 @@ export class IdentityDbMemoryStore implements BoxBrainMemoryStore {
async getSpace(spaceId: string): Promise<MemorySpace | null> { async getSpace(spaceId: string): Promise<MemorySpace | null> {
const space = await this.options.db.getSpaceByName(spaceId); const space = await this.options.db.getSpaceByName(spaceId);
if (!space) return null; if (!space) return null;
const metadata = typeof space.metadata === 'object' && space.metadata !== null && !Array.isArray(space.metadata) ? space.metadata as Record<string, unknown> : {}; const metadata =
const displayName = typeof metadata['displayName'] === 'string' ? metadata['displayName'] : space.name; typeof space.metadata === "object" &&
return { id: space.name, displayName, createdAt: space.createdAt, metadata }; space.metadata !== null &&
!Array.isArray(space.metadata)
? (space.metadata as Record<string, unknown>)
: {};
const displayName =
typeof metadata["displayName"] === "string"
? metadata["displayName"]
: space.name;
return {
id: space.name,
displayName,
createdAt: space.createdAt,
metadata,
};
} }
async addFact(spaceId: string, fact: FactDraft): Promise<StoredFact> { async addFact(spaceId: string, fact: FactDraft): Promise<StoredFact> {
@@ -130,66 +198,111 @@ export class IdentityDbMemoryStore implements BoxBrainMemoryStore {
confidence: fact.confidence ?? null, confidence: fact.confidence ?? null,
source: fact.source ?? null, source: fact.source ?? null,
metadata: (fact.metadata ?? null) as never, metadata: (fact.metadata ?? null) as never,
topics: normalizeTopics(fact.topics).map((topic) => ({ name: topic, category: 'entity' as const, granularity: 'concrete' as const })), topics: normalizeTopics(fact.topics).map((topic) => ({
name: topic,
category: "entity" as const,
granularity: "concrete" as const,
})),
}); });
return this.fromIdentityFact(stored); return this.fromIdentityFact(stored);
} }
async listFacts(spaceId: string): Promise<StoredFact[]> { async listFacts(spaceId: string): Promise<StoredFact[]> {
const topics = await this.options.db.listTopics({ spaceName: spaceId, includeFacts: true }); const topics = await this.options.db.listTopics({
spaceName: spaceId,
includeFacts: true,
});
const collected = new Map<string, StoredFact>(); const collected = new Map<string, StoredFact>();
for (const topic of topics) { for (const topic of topics) {
for (const fact of topic.facts) { for (const fact of topic.facts) {
collected.set(fact.id, this.fromIdentityFact(fact)); collected.set(fact.id, this.fromIdentityFact(fact));
} }
} }
return [...collected.values()].sort((a, b) => a.createdAt.localeCompare(b.createdAt)); return [...collected.values()].sort((a, b) =>
a.createdAt.localeCompare(b.createdAt),
);
} }
async findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]> { async findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]> {
const uniqueTopics = normalizeTopics(topics); const uniqueTopics = normalizeTopics(topics);
const collected = new Map<string, StoredFact>(); const collected = new Map<string, StoredFact>();
for (const topic of uniqueTopics) { for (const topic of uniqueTopics) {
const facts = await this.options.db.getTopicFacts(topic, { spaceName: spaceId }); const facts = await this.options.db.getTopicFacts(topic, {
spaceName: spaceId,
});
for (const fact of facts) { for (const fact of facts) {
collected.set(fact.id, this.fromIdentityFact(fact)); collected.set(fact.id, this.fromIdentityFact(fact));
} }
} }
return [...collected.values()].sort((a, b) => a.createdAt.localeCompare(b.createdAt)); return [...collected.values()].sort((a, b) =>
a.createdAt.localeCompare(b.createdAt),
);
} }
async saveScheduleEntries(spaceId: string, entries: ScheduleEntry[]): Promise<void> { async saveScheduleEntries(
spaceId: string,
entries: ScheduleEntry[],
): Promise<void> {
for (const entry of entries) { for (const entry of entries) {
await this.addFact(spaceId, { await this.addFact(spaceId, {
statement: `${entry.title} from ${entry.startAt} to ${entry.endAt}.`, statement: `${entry.title} from ${entry.startAt} to ${entry.endAt}.`,
topics: ['schedule', entry.startAt.slice(0, 10), entry.activity, 'persona'], topics: [
source: 'boxbrain.schedule', "schedule",
entry.startAt.slice(0, 10),
entry.activity,
"persona",
],
source: "boxbrain.schedule",
metadata: { ...entry.metadata, scheduleEntry: entry }, metadata: { ...entry.metadata, scheduleEntry: entry },
}); });
} }
} }
async listScheduleEntries(spaceId: string, fromInclusive: string, toExclusive: string): Promise<ScheduleEntry[]> { async listScheduleEntries(
const facts = await this.findFacts(spaceId, ['schedule']); spaceId: string,
fromInclusive: string,
toExclusive: string,
): Promise<ScheduleEntry[]> {
const facts = await this.findFacts(spaceId, ["schedule"]);
return facts return facts
.map((fact) => fact.metadata?.['scheduleEntry']) .map((fact) => fact.metadata?.["scheduleEntry"])
.filter((value): value is ScheduleEntry => typeof value === 'object' && value !== null && !Array.isArray(value)) .filter(
.filter((entry) => entry.startAt < toExclusive && entry.endAt > fromInclusive) (value): value is ScheduleEntry =>
typeof value === "object" && value !== null && !Array.isArray(value),
)
.filter(
(entry) => entry.startAt < toExclusive && entry.endAt > fromInclusive,
)
.sort((a, b) => a.startAt.localeCompare(b.startAt)); .sort((a, b) => a.startAt.localeCompare(b.startAt));
} }
async deleteScheduleEntriesBefore(_spaceId: string, _cutoffExclusive: string): Promise<number> { async deleteScheduleEntriesBefore(
_spaceId: string,
_cutoffExclusive: string,
): Promise<number> {
// IdentityDB is append-oriented at the public API level. Record schedule deletion as a fact at the Persona layer. // IdentityDB is append-oriented at the public API level. Record schedule deletion as a fact at the Persona layer.
return 0; return 0;
} }
async ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact> { async ingestStatement(
const fact = await this.options.db.ingestStatement(statement, { extractor, spaceName: spaceId }); spaceId: string,
return this.fromIdentityFact(fact); statement: string,
extractor: FactExtractor,
): Promise<StoredFact[]> {
const facts = await this.options.db.ingestStatements(statement, {
extractor,
spaceName: spaceId,
});
return facts.map((fact) => this.fromIdentityFact(fact));
} }
private fromIdentityFact(fact: IdentityFact): StoredFact { private fromIdentityFact(fact: IdentityFact): StoredFact {
const metadata = typeof fact.metadata === 'object' && fact.metadata !== null && !Array.isArray(fact.metadata) ? fact.metadata as Record<string, unknown> : undefined; const metadata =
typeof fact.metadata === "object" &&
fact.metadata !== null &&
!Array.isArray(fact.metadata)
? (fact.metadata as Record<string, unknown>)
: undefined;
return { return {
id: fact.id, id: fact.id,
statement: fact.statement, statement: fact.statement,
@@ -202,8 +315,10 @@ export class IdentityDbMemoryStore implements BoxBrainMemoryStore {
} }
} }
export async function createSqliteIdentityMemoryStore(filename: string): Promise<IdentityDbMemoryStore> { export async function createSqliteIdentityMemoryStore(
const db = await IdentityDB.connect({ client: 'sqlite', filename }); filename: string,
): Promise<IdentityDbMemoryStore> {
const db = await IdentityDB.connect({ client: "sqlite", filename });
await db.initialize(); await db.initialize();
return new IdentityDbMemoryStore({ db }); return new IdentityDbMemoryStore({ db });
} }

View File

@@ -28,6 +28,7 @@ import type {
ScheduleEntry, ScheduleEntry,
ScheduledAvailabilitySnapshot, ScheduledAvailabilitySnapshot,
} from "./types"; } from "./types";
import { extractedToDraft } from "./utils";
interface CreateMode { interface CreateMode {
type: "create"; type: "create";
@@ -331,20 +332,6 @@ export class Persona {
return draft; return draft;
} }
private extractedToDraft(fact: ExtractedFact, statement: string): FactDraft {
return {
statement: fact.statement ?? statement,
topics: [...fact.topics.map((t) => t.name), "sleepMemory"],
source: fact.source ?? "boxbrain.sleepMemory",
...(typeof fact.confidence === "number"
? { confidence: fact.confidence }
: {}),
...(fact.metadata !== undefined && fact.metadata !== null
? { metadata: fact.metadata as Record<string, unknown> }
: {}),
};
}
async sleepMemory(input: { async sleepMemory(input: {
datetime: DateTimeInput; datetime: DateTimeInput;
messageHistory: PersonaMessage[]; messageHistory: PersonaMessage[];
@@ -376,7 +363,7 @@ export class Persona {
].join("\n"); ].join("\n");
const extractedFacts = ( const extractedFacts = (
await extractFacts(statement, this.options.models.factExtractor) await extractFacts(statement, this.options.models.factExtractor)
).map((fact) => this.extractedToDraft(fact, statement)); ).map((fact) => extractedToDraft(fact, statement));
for (const fact of extractedFacts) { for (const fact of extractedFacts) {
await this.memory.addFact(persona.id, fact); await this.memory.addFact(persona.id, fact);
@@ -407,7 +394,7 @@ export class Persona {
const statement = `Persona: ${this.mode.displayName}\nSeed: ${this.mode.seedMessage}`; const statement = `Persona: ${this.mode.displayName}\nSeed: ${this.mode.seedMessage}`;
const extracteds = ( const extracteds = (
await extractFacts(statement, this.options.models.factExtractor) await extractFacts(statement, this.options.models.factExtractor)
).map((fact) => this.extractedToDraft(fact, statement)); ).map((fact) => extractedToDraft(fact, statement));
for (const fact of extracteds) { for (const fact of extracteds) {
await this.memory.addFact(space.id, fact); await this.memory.addFact(space.id, fact);

View File

@@ -1,14 +1,14 @@
import type { FactExtractor } from 'identitydb'; import type { FactExtractor } from "identitydb";
export type DateTimeInput = Date | string | number; export type DateTimeInput = Date | string | number;
export type PersonaConstructorMode = 'create' | 'load'; export type PersonaConstructorMode = "create" | "load";
export type ScheduleGranularity = 'day' | 'ten-minute'; export type ScheduleGranularity = "day" | "ten-minute";
export type ScheduleActivity = string; export type ScheduleActivity = string;
export type AvailabilityMode = 'online' | 'do-not-disturb' | 'offline'; export type AvailabilityMode = "online" | "do-not-disturb" | "offline";
export interface MemorySpace { export interface MemorySpace {
id: string; id: string;
@@ -59,7 +59,7 @@ export interface ScheduledAvailabilitySnapshot {
} }
export interface PersonaMessage { export interface PersonaMessage {
sender: 'persona' | 'user'; sender: "persona" | "user";
time: DateTimeInput; time: DateTimeInput;
content: string; content: string;
} }
@@ -85,7 +85,7 @@ export interface MandatoryConversationContext {
export interface ReplyGenerationInput { export interface ReplyGenerationInput {
persona: MemorySpace; persona: MemorySpace;
now: string; now: string;
mode: 'reply' | 'start-conversation'; mode: "reply" | "start-conversation";
context: MandatoryConversationContext; context: MandatoryConversationContext;
userMessage?: string; userMessage?: string;
instruction: string; instruction: string;
@@ -144,8 +144,12 @@ export interface MonthlyScheduleGenerationInput {
} }
export interface ScheduleModel { export interface ScheduleModel {
generateDailySchedule(input: DailyScheduleGenerationInput): Promise<ScheduleBlock[]>; generateDailySchedule(
generateMonthlySchedule(input: MonthlyScheduleGenerationInput): Promise<ScheduleBlock[]>; input: DailyScheduleGenerationInput,
): Promise<ScheduleBlock[]>;
generateMonthlySchedule(
input: MonthlyScheduleGenerationInput,
): Promise<ScheduleBlock[]>;
} }
export interface PersonaModels { export interface PersonaModels {
@@ -164,13 +168,28 @@ export interface PersonaOptions {
} }
export interface BoxBrainMemoryStore { export interface BoxBrainMemoryStore {
createSpace(input: { displayName: string; seedMessage: string; now: string }): Promise<MemorySpace>; createSpace(input: {
displayName: string;
seedMessage: string;
now: string;
}): Promise<MemorySpace>;
getSpace(spaceId: string): Promise<MemorySpace | null>; getSpace(spaceId: string): Promise<MemorySpace | null>;
addFact(spaceId: string, fact: FactDraft): Promise<StoredFact>; addFact(spaceId: string, fact: FactDraft): Promise<StoredFact>;
listFacts(spaceId: string): Promise<StoredFact[]>; listFacts(spaceId: string): Promise<StoredFact[]>;
findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]>; findFacts(spaceId: string, topics: string[]): Promise<StoredFact[]>;
saveScheduleEntries(spaceId: string, entries: ScheduleEntry[]): Promise<void>; saveScheduleEntries(spaceId: string, entries: ScheduleEntry[]): Promise<void>;
listScheduleEntries(spaceId: string, fromInclusive: string, toExclusive: string): Promise<ScheduleEntry[]>; listScheduleEntries(
deleteScheduleEntriesBefore(spaceId: string, cutoffExclusive: string): Promise<number>; spaceId: string,
ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact>; fromInclusive: string,
toExclusive: string,
): Promise<ScheduleEntry[]>;
deleteScheduleEntriesBefore(
spaceId: string,
cutoffExclusive: string,
): Promise<number>;
ingestStatement(
spaceId: string,
statement: string,
extractor: FactExtractor,
): Promise<StoredFact[]>;
} }

19
src/utils.ts Normal file
View File

@@ -0,0 +1,19 @@
import { ExtractedFact } from "identitydb";
import { FactDraft } from "./types";
export function extractedToDraft(
fact: ExtractedFact,
statement: string,
): FactDraft {
return {
statement: fact.statement ?? statement,
topics: [...fact.topics.map((t) => t.name), "sleepMemory"],
source: fact.source ?? "boxbrain.sleepMemory",
...(typeof fact.confidence === "number"
? { confidence: fact.confidence }
: {}),
...(fact.metadata !== undefined && fact.metadata !== null
? { metadata: fact.metadata as Record<string, unknown> }
: {}),
};
}