feat: use FactExtractor
All checks were successful
CI / verify (push) Successful in 11s

This commit is contained in:
2026-05-17 23:41:02 +09:00
parent 4ef1b89a2d
commit 600f5ff0bc
7 changed files with 106 additions and 90 deletions

View File

@@ -30,16 +30,6 @@ export function conversationInstruction(): string {
].join("\n");
}
export function memoryExtractionInstruction(now: string): string {
return [
`Current objective time: ${now}.`,
"Read the message history and extract durable facts worth remembering.",
"Objectivize subjective statements before storage.",
'Example: "I started TypeScript in 2025" becomes "The user started TypeScript in 2025."',
"Prefer facts about the persona, the user, their relationship, preferences, history, schedule-relevant events, and stable traits.",
].join("\n");
}
export async function buildMandatoryConversationContext(input: {
persona: MemorySpace;
now: DateTimeInput;

View File

@@ -1,4 +1,4 @@
import { IdentityDB, type Fact as IdentityFact } from 'identitydb';
import { IdentityDB, extractFact, type Fact as IdentityFact, type FactExtractor } from 'identitydb';
import type { BoxBrainMemoryStore, FactDraft, MemorySpace, ScheduleEntry, StoredFact } from './types';
function normalizeTopics(topics: string[]): string[] {
@@ -74,6 +74,17 @@ export class InMemoryMemoryStore implements BoxBrainMemoryStore {
this.schedules.set(spaceId, kept);
return entries.length - kept.length;
}
async ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact> {
const extracted = await extractFact(statement, extractor);
return this.addFact(spaceId, {
statement: extracted.statement ?? statement,
topics: extracted.topics.map((t) => t.name),
...(typeof extracted.confidence === 'number' ? { confidence: extracted.confidence } : {}),
...(typeof extracted.source === 'string' ? { source: extracted.source } : {}),
...(extracted.metadata !== undefined && extracted.metadata !== null ? { metadata: extracted.metadata as Record<string, unknown> } : {}),
});
}
}
export interface IdentityDbMemoryStoreOptions {
@@ -172,6 +183,11 @@ export class IdentityDbMemoryStore implements BoxBrainMemoryStore {
return 0;
}
async ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact> {
const fact = await this.options.db.ingestStatement(statement, { extractor, spaceName: spaceId });
return this.fromIdentityFact(fact);
}
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;
return {

View File

@@ -10,11 +10,11 @@ import {
startOfUtcDay,
toIso,
} from "./schedule";
import { extractFact } from "identitydb";
import {
buildMandatoryConversationContext,
conversationInstruction,
formatMessageHistory,
memoryExtractionInstruction,
} from "./conversation";
import type {
BoxBrainMemoryStore,
@@ -334,35 +334,50 @@ export class Persona {
messageHistory: PersonaMessage[];
}): Promise<FactDraft[]> {
const persona = await this.ready();
if (!this.options.models?.memoryExtraction) {
throw new Error("sleepMemory requires options.models.memoryExtraction.");
if (!this.options.models?.factExtractor) {
throw new Error("sleepMemory requires options.models.factExtractor.");
}
const contextFacts = await this.memory.findFacts(persona.id, [
"persona",
persona.displayName,
"user",
]);
const drafts = await this.options.models.memoryExtraction.extract({
persona,
now: toIso(input.datetime),
formattedMessageHistory: formatMessageHistory({
const statement = [
`Current objective time: ${toIso(input.datetime)}.`,
"Read the message history and extract durable facts worth remembering.",
"Objectivize subjective statements before storage.",
'Example: "I started TypeScript in 2025" becomes "The user started TypeScript in 2025."',
"Prefer facts about the persona, the user, their relationship, preferences, history, schedule-relevant events, and stable traits.",
"",
"Context facts:",
...contextFacts.map((f) => `- ${f.statement}`),
"",
"Message history:",
formatMessageHistory({
personaName: persona.displayName,
messages: input.messageHistory,
}),
contextFacts,
instruction: memoryExtractionInstruction(toIso(input.datetime)),
});
for (const draft of drafts) {
await this.memory.addFact(persona.id, {
...draft,
topics: [...draft.topics, "sleepMemory"],
source: draft.source ?? "boxbrain.sleepMemory",
});
}
].join("\n");
const extracted = await extractFact(
statement,
this.options.models.factExtractor,
);
const draft: FactDraft = {
statement: extracted.statement ?? statement,
topics: [...extracted.topics.map((t) => t.name), "sleepMemory"],
source: extracted.source ?? "boxbrain.sleepMemory",
...(typeof extracted.confidence === 'number'
? { confidence: extracted.confidence }
: {}),
...(extracted.metadata !== undefined && extracted.metadata !== null
? { metadata: extracted.metadata as Record<string, unknown> }
: {}),
};
await this.memory.addFact(persona.id, draft);
await this.emit("persona.memory.sleep.persisted", {
factCount: drafts.length,
factCount: 1,
});
return drafts;
return [draft];
}
private async initialize(): Promise<MemorySpace> {
@@ -381,24 +396,38 @@ export class Persona {
seedMessage: this.mode.seedMessage,
now,
});
const modelFacts = this.options.models?.initialization
? await this.options.models.initialization.extractInitialFacts({
displayName: this.mode.displayName,
seedMessage: this.mode.seedMessage,
now,
})
: undefined;
const facts = modelFacts ?? [
defaultInitialFact(this.mode.displayName, this.mode.seedMessage),
];
for (const fact of facts) {
if (this.options.models?.factExtractor) {
const statement = `Persona: ${this.mode.displayName}\nSeed: ${this.mode.seedMessage}`;
const extracted = await extractFact(
statement,
this.options.models.factExtractor,
);
const draft: FactDraft = {
statement: extracted.statement ?? statement,
topics: extracted.topics.map((t) => t.name),
source: extracted.source ?? "boxbrain.persona.initialization",
...(typeof extracted.confidence === 'number'
? { confidence: extracted.confidence }
: {}),
...(extracted.metadata !== undefined && extracted.metadata !== null
? { metadata: extracted.metadata as Record<string, unknown> }
: {}),
};
await this.memory.addFact(space.id, draft);
await this.emit(
"persona.initialized",
{ displayName: space.displayName, factCount: 1 },
space.id,
);
} else {
const fact = defaultInitialFact(this.mode.displayName, this.mode.seedMessage);
await this.memory.addFact(space.id, fact);
await this.emit(
"persona.initialized",
{ displayName: space.displayName, factCount: 1 },
space.id,
);
}
await this.emit(
"persona.initialized",
{ displayName: space.displayName, factCount: facts.length },
space.id,
);
await this.refreshAvailability(now, space);
return space;
}

View File

@@ -1,3 +1,5 @@
import type { FactExtractor } from 'identitydb';
export type DateTimeInput = Date | string | number;
export type PersonaConstructorMode = 'create' | 'load';
@@ -117,18 +119,6 @@ export interface RewriteModel {
decide(input: RewriteDecisionInput): Promise<RewriteDecision>;
}
export interface MemoryExtractionInput {
persona: MemorySpace;
now: string;
formattedMessageHistory: string;
contextFacts: StoredFact[];
instruction: string;
}
export interface MemoryExtractionModel {
extract(input: MemoryExtractionInput): Promise<FactDraft[]>;
}
export interface ScheduleBlock {
startTime: string;
endTime: string;
@@ -158,19 +148,10 @@ export interface ScheduleModel {
generateMonthlySchedule(input: MonthlyScheduleGenerationInput): Promise<ScheduleBlock[]>;
}
export interface PersonaInitializationModel {
extractInitialFacts(input: {
displayName: string;
seedMessage: string;
now: string;
}): Promise<FactDraft[]>;
}
export interface PersonaModels {
initialization?: PersonaInitializationModel;
factExtractor?: FactExtractor;
conversation?: ConversationModel;
rewrite?: RewriteModel;
memoryExtraction?: MemoryExtractionModel;
schedule?: ScheduleModel;
}
@@ -190,4 +171,5 @@ export interface BoxBrainMemoryStore {
saveScheduleEntries(spaceId: string, entries: ScheduleEntry[]): Promise<void>;
listScheduleEntries(spaceId: string, fromInclusive: string, toExclusive: string): Promise<ScheduleEntry[]>;
deleteScheduleEntriesBefore(spaceId: string, cutoffExclusive: string): Promise<number>;
ingestStatement(spaceId: string, statement: string, extractor: FactExtractor): Promise<StoredFact>;
}