14 Commits

Author SHA1 Message Date
d8da1ec998 fix: fix all type error caused by identitydb
All checks were successful
CI / verify (push) Successful in 13s
Publish / publish (push) Successful in 22s
2026-05-20 23:32:13 +09:00
b52d37170c v0.5.0
Some checks failed
CI / verify (push) Failing after 10s
Publish / publish (push) Failing after 9s
2026-05-20 23:22:58 +09:00
6f4f65a8ee test: make test to work with latest identitydb 2026-05-20 23:22:46 +09:00
488ba20eb6 feat: update identitydb to 0.5.0 2026-05-20 23:22:32 +09:00
05f077b798 v0.4.0
All checks were successful
CI / verify (push) Successful in 12s
Publish / publish (push) Successful in 21s
2026-05-19 23:24:42 +09:00
f964d4de9b feat: add baseSystemPrompt 2026-05-19 23:24:34 +09:00
882e12340c v0.3.5
All checks were successful
CI / verify (push) Successful in 14s
Publish / publish (push) Successful in 21s
2026-05-19 22:31:57 +09:00
fb89ffbc16 feat: upgrade identitydb to 0.4.0 2026-05-19 22:31:52 +09:00
8e051a12e1 v0.3.4
All checks were successful
CI / verify (push) Successful in 15s
Publish / publish (push) Successful in 27s
2026-05-19 22:10:03 +09:00
c66b315fe5 feat: upgrade identitydb to 0.3.0 2026-05-19 22:10:01 +09:00
d2a3bfcd15 v0.3.3
All checks were successful
CI / verify (push) Successful in 13s
Publish / publish (push) Successful in 21s
2026-05-17 23:42:25 +09:00
600f5ff0bc feat: use FactExtractor
All checks were successful
CI / verify (push) Successful in 11s
2026-05-17 23:41:02 +09:00
4ef1b89a2d v0.3.2
All checks were successful
CI / verify (push) Successful in 10s
Publish / publish (push) Successful in 19s
2026-05-17 23:14:51 +09:00
f9f37b0835 fix: upgrade identitydb to ^0.2.2 2026-05-17 23:14:38 +09:00
10 changed files with 342 additions and 143 deletions

View File

@@ -5,7 +5,7 @@
"": { "": {
"name": "boxbrain", "name": "boxbrain",
"dependencies": { "dependencies": {
"identitydb": "0.2.1", "identitydb": "^0.5.0",
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "latest", "@types/bun": "latest",
@@ -249,7 +249,7 @@
"iconv-lite": ["iconv-lite@0.7.2", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw=="], "iconv-lite": ["iconv-lite@0.7.2", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw=="],
"identitydb": ["identitydb@0.2.1", "", { "dependencies": { "kysely": "^0.28.8", "mysql2": "^3.15.3", "pg": "^8.16.0" } }, "sha512-e+caNqI7F6JaqgyIFQbdiT5/2Frs5PEJgy3mmF+qUVspHZ4z6QtFF5jonDnpYtJpZ9guPWfeQ/xteeaiwOJ5zA=="], "identitydb": ["identitydb@0.5.0", "", { "dependencies": { "kysely": "^0.28.8", "mysql2": "^3.15.3", "pg": "^8.16.0" } }, "sha512-3cp14fb5nDKFakRqHdJrOBOgpDWLlvJ/K2q8405muAfqcJja9ds2tS9ksoaEiMgGU5r+mIz6lADZ3DYNpGqHVQ=="],
"is-property": ["is-property@1.0.2", "", {}, "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g=="], "is-property": ["is-property@1.0.2", "", {}, "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g=="],

View File

@@ -1,6 +1,6 @@
{ {
"name": "boxbrain", "name": "boxbrain",
"version": "0.3.1", "version": "0.5.0",
"description": "Human-like persona harness framework powered by LLMs and IdentityDB.", "description": "Human-like persona harness framework powered by LLMs and IdentityDB.",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",
@@ -48,7 +48,7 @@
"prepublishOnly": "bun run check && bun run test && bun run build && bun run pack:check" "prepublishOnly": "bun run check && bun run test && bun run build && bun run pack:check"
}, },
"dependencies": { "dependencies": {
"identitydb": "0.2.1" "identitydb": "^0.5.0"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "latest", "@types/bun": "latest",

View File

@@ -20,24 +20,16 @@ export function formatMessageHistory(input: {
.join("\n"); .join("\n");
} }
export function conversationInstruction(): string { export function conversationInstruction(baseSystemPrompt?: string): string {
return [ const parts = [
...(baseSystemPrompt === undefined ? [] : [baseSystemPrompt]),
"You are controlling the persona, not a generic assistant.", "You are controlling the persona, not a generic assistant.",
"Use the send_message tool conceptually: return one or more outgoing messages.", "Use the send_message tool conceptually: return one or more outgoing messages.",
"Unless the persona strongly prefers otherwise, keep each outgoing message to at most one sentence.", "Unless the persona strongly prefers otherwise, keep each outgoing message to at most one sentence.",
"Prefer short, natural, chat-like wording and allow splitting one thought into multiple messages.", "Prefer short, natural, chat-like wording and allow splitting one thought into multiple messages.",
'If mandatory memory says "기억이 없음", the persona may naturally wonder about missing context instead of pretending to remember.', 'If mandatory memory says "기억이 없음", the persona may naturally wonder about missing context instead of pretending to remember.',
].join("\n"); ];
} return parts.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: { export async function buildMandatoryConversationContext(input: {

View File

@@ -1,12 +1,26 @@
import { IdentityDB, type Fact as IdentityFact } 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,23 +81,57 @@ 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[]> {
const extracted = await extractFacts(statement, extractor);
const stored: StoredFact[] = [];
for (const fact of extracted) {
stored.push(
await this.addFact(spaceId, extractedToDraft(fact, statement)),
);
}
return stored;
}
} }
export interface IdentityDbMemoryStoreOptions { export interface IdentityDbMemoryStoreOptions {
@@ -83,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,
@@ -107,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> {
@@ -119,61 +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[]> {
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,
@@ -186,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

@@ -10,11 +10,11 @@ import {
startOfUtcDay, startOfUtcDay,
toIso, toIso,
} from "./schedule"; } from "./schedule";
import { ExtractedFact, extractFacts } from "identitydb";
import { import {
buildMandatoryConversationContext, buildMandatoryConversationContext,
conversationInstruction, conversationInstruction,
formatMessageHistory, formatMessageHistory,
memoryExtractionInstruction,
} from "./conversation"; } from "./conversation";
import type { import type {
BoxBrainMemoryStore, BoxBrainMemoryStore,
@@ -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";
@@ -68,6 +69,7 @@ export class Persona {
private readonly mode: Mode; private readonly mode: Mode;
private readonly readyPromise: Promise<MemorySpace>; private readonly readyPromise: Promise<MemorySpace>;
private availabilitySnapshot?: ScheduledAvailabilitySnapshot; private availabilitySnapshot?: ScheduledAvailabilitySnapshot;
readonly baseSystemPrompt: string | undefined;
constructor( constructor(
displayName: string, displayName: string,
@@ -88,6 +90,7 @@ export class Persona {
this.options = second ?? {}; this.options = second ?? {};
} }
this.memory = this.options.memory ?? new InMemoryMemoryStore(); this.memory = this.options.memory ?? new InMemoryMemoryStore();
this.baseSystemPrompt = this.options.baseSystemPrompt;
this.readyPromise = this.initialize(); this.readyPromise = this.initialize();
} }
@@ -247,7 +250,7 @@ export class Persona {
mode: "reply", mode: "reply",
context, context,
...(userMessage === undefined ? {} : { userMessage }), ...(userMessage === undefined ? {} : { userMessage }),
instruction: conversationInstruction(), instruction: conversationInstruction(this.baseSystemPrompt),
}), }),
); );
@@ -281,7 +284,7 @@ export class Persona {
now: toIso(input.datetime), now: toIso(input.datetime),
mode: "reply", mode: "reply",
context: latestContext, context: latestContext,
instruction: conversationInstruction(), instruction: conversationInstruction(this.baseSystemPrompt),
})), })),
); );
} }
@@ -320,7 +323,7 @@ export class Persona {
now: toIso(input.datetime), now: toIso(input.datetime),
mode: "start-conversation", mode: "start-conversation",
context, context,
instruction: conversationInstruction(), instruction: conversationInstruction(this.baseSystemPrompt),
}), }),
); );
await this.emit("persona.conversation.started", { await this.emit("persona.conversation.started", {
@@ -334,35 +337,41 @@ export class Persona {
messageHistory: PersonaMessage[]; messageHistory: PersonaMessage[];
}): Promise<FactDraft[]> { }): Promise<FactDraft[]> {
const persona = await this.ready(); const persona = await this.ready();
if (!this.options.models?.memoryExtraction) { if (!this.options.models?.factExtractor) {
throw new Error("sleepMemory requires options.models.memoryExtraction."); throw new Error("sleepMemory requires options.models.factExtractor.");
} }
const contextFacts = await this.memory.findFacts(persona.id, [ const contextFacts = await this.memory.findFacts(persona.id, [
"persona", "persona",
persona.displayName, persona.displayName,
"user", "user",
]); ]);
const drafts = await this.options.models.memoryExtraction.extract({ const statement = [
persona, `Current objective time: ${toIso(input.datetime)}.`,
now: toIso(input.datetime), "Read the message history and extract durable facts worth remembering.",
formattedMessageHistory: formatMessageHistory({ "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, personaName: persona.displayName,
messages: input.messageHistory, messages: input.messageHistory,
}), }),
contextFacts, ].join("\n");
instruction: memoryExtractionInstruction(toIso(input.datetime)), const extractedFacts = (
}); await extractFacts(statement, this.options.models.factExtractor)
for (const draft of drafts) { ).map((fact) => extractedToDraft(fact, statement));
await this.memory.addFact(persona.id, {
...draft, for (const fact of extractedFacts) {
topics: [...draft.topics, "sleepMemory"], await this.memory.addFact(persona.id, fact);
source: draft.source ?? "boxbrain.sleepMemory",
});
} }
await this.emit("persona.memory.sleep.persisted", { await this.emit("persona.memory.sleep.persisted", {
factCount: drafts.length, factCount: extractedFacts.length,
}); });
return drafts; return extractedFacts;
} }
private async initialize(): Promise<MemorySpace> { private async initialize(): Promise<MemorySpace> {
@@ -381,24 +390,33 @@ export class Persona {
seedMessage: this.mode.seedMessage, seedMessage: this.mode.seedMessage,
now, now,
}); });
const modelFacts = this.options.models?.initialization if (this.options.models?.factExtractor) {
? await this.options.models.initialization.extractInitialFacts({ const statement = `Persona: ${this.mode.displayName}\nSeed: ${this.mode.seedMessage}`;
displayName: this.mode.displayName, const extracteds = (
seedMessage: this.mode.seedMessage, await extractFacts(statement, this.options.models.factExtractor)
now, ).map((fact) => extractedToDraft(fact, statement));
})
: undefined; for (const fact of extracteds) {
const facts = modelFacts ?? [
defaultInitialFact(this.mode.displayName, this.mode.seedMessage),
];
for (const fact of facts) {
await this.memory.addFact(space.id, fact); await this.memory.addFact(space.id, fact);
} }
await this.emit( await this.emit(
"persona.initialized", "persona.initialized",
{ displayName: space.displayName, factCount: facts.length }, { displayName: space.displayName, factCount: extracteds.length },
space.id, 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.refreshAvailability(now, space); await this.refreshAvailability(now, space);
return space; return space;
} }

View File

@@ -1,12 +1,14 @@
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;
@@ -57,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;
} }
@@ -83,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;
@@ -117,18 +119,6 @@ export interface RewriteModel {
decide(input: RewriteDecisionInput): Promise<RewriteDecision>; 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 { export interface ScheduleBlock {
startTime: string; startTime: string;
endTime: string; endTime: string;
@@ -154,23 +144,18 @@ 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(
export interface PersonaInitializationModel { input: MonthlyScheduleGenerationInput,
extractInitialFacts(input: { ): Promise<ScheduleBlock[]>;
displayName: string;
seedMessage: string;
now: string;
}): Promise<FactDraft[]>;
} }
export interface PersonaModels { export interface PersonaModels {
initialization?: PersonaInitializationModel; factExtractor?: FactExtractor;
conversation?: ConversationModel; conversation?: ConversationModel;
rewrite?: RewriteModel; rewrite?: RewriteModel;
memoryExtraction?: MemoryExtractionModel;
schedule?: ScheduleModel; schedule?: ScheduleModel;
} }
@@ -179,15 +164,32 @@ export interface PersonaOptions {
models?: PersonaModels; models?: PersonaModels;
debug?: DebugHook; debug?: DebugHook;
now?: DateTimeInput; now?: DateTimeInput;
baseSystemPrompt?: string;
} }
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,
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> }
: {}),
};
}

View File

@@ -55,7 +55,6 @@ describe('Conversation API', () => {
memory, memory,
now: '2026-05-01T10:00:00.000Z', now: '2026-05-01T10:00:00.000Z',
models: { models: {
initialization: { async extractInitialFacts() { return []; } },
conversation: { conversation: {
async generateReply(input) { async generateReply(input) {
memorySummary = input.context.memorySummary; memorySummary = input.context.memorySummary;
@@ -64,7 +63,8 @@ describe('Conversation API', () => {
}, },
}, },
}); });
await persona.ready(); const space = await persona.ready();
memory.facts.set(space.id, []);
await persona.sendMessage({ await persona.sendMessage({
datetime: '2026-05-01T12:00:00.000Z', datetime: '2026-05-01T12:00:00.000Z',
@@ -124,4 +124,30 @@ describe('Conversation API', () => {
expect(mode).toBe('start-conversation'); expect(mode).toBe('start-conversation');
expect(started.messages).toEqual(['오늘 좀 조용하네.']); expect(started.messages).toEqual(['오늘 좀 조용하네.']);
}); });
it('includes baseSystemPrompt at the start of the instruction when provided', async () => {
const memory = new InMemoryMemoryStore();
let captured: ReplyGenerationInput | undefined;
const persona = new Persona('Mina', 'Mina likes quiet cafes.', {
memory,
now: '2026-05-01T10:00:00.000Z',
baseSystemPrompt: 'You are a helpful assistant. Always be kind.',
models: {
conversation: {
async generateReply(input) {
captured = input;
return { messages: ['Hello!'] };
},
},
},
});
await persona.ready();
await persona.sendMessage({
datetime: '2026-05-01T12:00:00.000Z',
messageHistory: [{ sender: 'user', time: '2026-05-01T12:00:00.000Z', content: 'Hi' }],
});
expect(captured?.instruction.startsWith('You are a helpful assistant. Always be kind.')).toBe(true);
});
}); });

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { InMemoryMemoryStore, Persona, type FactDraft } from '../src'; import { InMemoryMemoryStore, Persona } from '../src';
describe('Persona initialization', () => { describe('Persona initialization', () => {
it('creates a new isolated persona space from displayName and seed message', async () => { it('creates a new isolated persona space from displayName and seed message', async () => {
@@ -10,15 +10,13 @@ describe('Persona initialization', () => {
now: '2026-05-01T10:00:00.000Z', now: '2026-05-01T10:00:00.000Z',
debug: (event) => { debug.push(event.name); }, debug: (event) => { debug.push(event.name); },
models: { models: {
initialization: { factExtractor: {
async extractInitialFacts(input): Promise<FactDraft[]> { async extract(input) {
return [ return [{
{ statement: 'Mina likes quiet cafes.',
statement: `${input.displayName} likes quiet cafes.`, topics: [{ name: 'persona' }, { name: 'Mina' }],
topics: ['persona', input.displayName],
source: 'test', source: 'test',
}, }];
];
}, },
}, },
}, },
@@ -31,6 +29,18 @@ describe('Persona initialization', () => {
expect(debug).toContain('persona.initialized'); expect(debug).toContain('persona.initialized');
}); });
it('exposes baseSystemPrompt on the persona instance when provided', async () => {
const memory = new InMemoryMemoryStore();
const persona = new Persona('Hana', 'Hana is a cheerful barista.', {
memory,
now: '2026-05-01T10:00:00.000Z',
baseSystemPrompt: 'You are a helpful assistant. Always be kind.',
});
await persona.ready();
expect(persona.baseSystemPrompt).toBe('You are a helpful assistant. Always be kind.');
});
it('loads an existing persona space by space id without creating another space', async () => { it('loads an existing persona space by space id without creating another space', async () => {
const memory = new InMemoryMemoryStore(); const memory = new InMemoryMemoryStore();
const created = new Persona('Joon', 'Joon is a freelance designer.', { memory, now: '2026-05-01T10:00:00.000Z' }); const created = new Persona('Joon', 'Joon is a freelance designer.', { memory, now: '2026-05-01T10:00:00.000Z' });

View File

@@ -8,17 +8,18 @@ describe('sleepMemory', () => {
memory, memory,
now: '2026-05-01T10:00:00.000Z', now: '2026-05-01T10:00:00.000Z',
models: { models: {
memoryExtraction: { factExtractor: {
async extract(input) { async extract(input) {
expect(input.formattedMessageHistory).toContain('user@2026-05-01T15:00:00.000Z: 나는 타입스크립트를 2025년부터 시작했어'); if (input.includes('Seed:')) {
expect(input.instruction).toContain('Objectivize'); return [{ statement: 'Mina remembers stable details.', topics: [{ name: 'persona' }, { name: 'Mina' }] }];
return [ }
{ expect(input).toContain('user@2026-05-01T15:00:00.000Z: 나는 타입스크립트를 2025년부터 시작했어');
expect(input).toContain('Objectivize');
return [{
statement: 'The user started TypeScript in 2025.', statement: 'The user started TypeScript in 2025.',
topics: ['user', 'TypeScript', '2025'], topics: [{ name: 'user' }, { name: 'TypeScript' }, { name: '2025' }],
confidence: 0.9, confidence: 0.9,
}, }];
];
}, },
}, },
}, },