Compare commits
15 Commits
v0.3.0
...
b52d37170c
| Author | SHA1 | Date | |
|---|---|---|---|
| b52d37170c | |||
| 6f4f65a8ee | |||
| 488ba20eb6 | |||
| 05f077b798 | |||
| f964d4de9b | |||
| 882e12340c | |||
| fb89ffbc16 | |||
| 8e051a12e1 | |||
| c66b315fe5 | |||
| d2a3bfcd15 | |||
| 600f5ff0bc | |||
| 4ef1b89a2d | |||
| f9f37b0835 | |||
| 43b5147f45 | |||
| 239d63dff7 |
4
bun.lock
4
bun.lock
@@ -5,7 +5,7 @@
|
||||
"": {
|
||||
"name": "boxbrain",
|
||||
"dependencies": {
|
||||
"identitydb": "0.2.1",
|
||||
"identitydb": "^0.5.0",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@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=="],
|
||||
|
||||
"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=="],
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "boxbrain",
|
||||
"version": "0.3.0",
|
||||
"version": "0.5.0",
|
||||
"description": "Human-like persona harness framework powered by LLMs and IdentityDB.",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -48,7 +48,7 @@
|
||||
"prepublishOnly": "bun run check && bun run test && bun run build && bun run pack:check"
|
||||
},
|
||||
"dependencies": {
|
||||
"identitydb": "0.2.1"
|
||||
"identitydb": "^0.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
|
||||
@@ -5,36 +5,31 @@ import type {
|
||||
MemorySpace,
|
||||
PersonaMessage,
|
||||
ScheduledAvailabilitySnapshot,
|
||||
} from './types';
|
||||
import { addUtcDays, buildAvailabilitySnapshot, dateKeysAround, startOfUtcDay, toIso } from './schedule';
|
||||
} from "./types";
|
||||
import { addUtcDays, dateKeysAround, startOfUtcDay, toIso } from "./schedule";
|
||||
|
||||
export function formatMessageHistory(input: { personaName: string; messages: PersonaMessage[] }): string {
|
||||
export function formatMessageHistory(input: {
|
||||
personaName: string;
|
||||
messages: PersonaMessage[];
|
||||
}): string {
|
||||
return input.messages
|
||||
.map((message) => {
|
||||
const sender = message.sender === 'persona' ? input.personaName : 'user';
|
||||
const sender = message.sender === "persona" ? input.personaName : "user";
|
||||
return `${sender}@${toIso(message.time)}: ${message.content}`;
|
||||
})
|
||||
.join('\n');
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function conversationInstruction(): string {
|
||||
return [
|
||||
'You are controlling the persona, not a generic assistant.',
|
||||
'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.',
|
||||
'Prefer short, natural, chat-like wording and allow splitting one thought into multiple messages.',
|
||||
export function conversationInstruction(baseSystemPrompt?: string): string {
|
||||
const parts = [
|
||||
...(baseSystemPrompt === undefined ? [] : [baseSystemPrompt]),
|
||||
"You are controlling the persona, not a generic assistant.",
|
||||
"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.",
|
||||
"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.',
|
||||
].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');
|
||||
];
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
export async function buildMandatoryConversationContext(input: {
|
||||
@@ -47,15 +42,27 @@ export async function buildMandatoryConversationContext(input: {
|
||||
const now = startOfUtcDay(input.now);
|
||||
const from = addUtcDays(now, -1).toISOString();
|
||||
const to = addUtcDays(now, 2).toISOString();
|
||||
const scheduleEntries = await input.memory.listScheduleEntries(input.persona.id, from, to);
|
||||
const personaAndUserFacts = await input.memory.findFacts(input.persona.id, ['persona', input.persona.displayName, 'user']);
|
||||
const memorySummary = personaAndUserFacts.length === 0
|
||||
? '기억이 없음'
|
||||
: personaAndUserFacts.map((fact) => `- ${fact.statement}`).join('\n');
|
||||
const scheduleEntries = await input.memory.listScheduleEntries(
|
||||
input.persona.id,
|
||||
from,
|
||||
to,
|
||||
);
|
||||
const personaAndUserFacts = await input.memory.findFacts(input.persona.id, [
|
||||
"persona",
|
||||
input.persona.displayName,
|
||||
"user",
|
||||
]);
|
||||
const memorySummary =
|
||||
personaAndUserFacts.length === 0
|
||||
? "기억이 없음"
|
||||
: personaAndUserFacts.map((fact) => `- ${fact.statement}`).join("\n");
|
||||
|
||||
return {
|
||||
formattedMessageHistory: formatMessageHistory({ personaName: input.persona.displayName, messages: input.messages }),
|
||||
conversationWindowLabel: `Required conversation window: yesterday/today. Schedule dates: ${dateKeysAround(input.now).join(', ')}.`,
|
||||
formattedMessageHistory: formatMessageHistory({
|
||||
personaName: input.persona.displayName,
|
||||
messages: input.messages,
|
||||
}),
|
||||
conversationWindowLabel: `Required conversation window: yesterday/today. Schedule dates: ${dateKeysAround(input.now).join(", ")}.`,
|
||||
memorySummary,
|
||||
personaAndUserFacts,
|
||||
scheduleEntries,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
321
src/persona.ts
321
src/persona.ts
@@ -1,4 +1,4 @@
|
||||
import { InMemoryMemoryStore } from './memory';
|
||||
import { InMemoryMemoryStore } from "./memory";
|
||||
import {
|
||||
addUtcDays,
|
||||
blocksToDailySchedule,
|
||||
@@ -9,13 +9,13 @@ import {
|
||||
scheduleTargetDay,
|
||||
startOfUtcDay,
|
||||
toIso,
|
||||
} from './schedule';
|
||||
} from "./schedule";
|
||||
import { ExtractedFact, extractFacts } from "identitydb";
|
||||
import {
|
||||
buildMandatoryConversationContext,
|
||||
conversationInstruction,
|
||||
formatMessageHistory,
|
||||
memoryExtractionInstruction,
|
||||
} from './conversation';
|
||||
} from "./conversation";
|
||||
import type {
|
||||
BoxBrainMemoryStore,
|
||||
DateTimeInput,
|
||||
@@ -27,28 +27,31 @@ import type {
|
||||
PersonaOptions,
|
||||
ScheduleEntry,
|
||||
ScheduledAvailabilitySnapshot,
|
||||
} from './types';
|
||||
} from "./types";
|
||||
|
||||
interface CreateMode {
|
||||
type: 'create';
|
||||
type: "create";
|
||||
displayName: string;
|
||||
seedMessage: string;
|
||||
}
|
||||
|
||||
interface LoadMode {
|
||||
type: 'load';
|
||||
type: "load";
|
||||
spaceId: string;
|
||||
}
|
||||
|
||||
type Mode = CreateMode | LoadMode;
|
||||
|
||||
function defaultInitialFact(displayName: string, seedMessage: string): FactDraft {
|
||||
function defaultInitialFact(
|
||||
displayName: string,
|
||||
seedMessage: string,
|
||||
): FactDraft {
|
||||
return {
|
||||
statement: `${displayName} is a BoxBrain persona initialized from this seed: ${seedMessage}`,
|
||||
topics: ['persona', displayName],
|
||||
source: 'boxbrain.persona.initialization',
|
||||
topics: ["persona", displayName],
|
||||
source: "boxbrain.persona.initialization",
|
||||
confidence: 1,
|
||||
metadata: { boxbrainType: 'persona-initial-fact' },
|
||||
metadata: { boxbrainType: "persona-initial-fact" },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -65,18 +68,28 @@ export class Persona {
|
||||
private readonly mode: Mode;
|
||||
private readonly readyPromise: Promise<MemorySpace>;
|
||||
private availabilitySnapshot?: ScheduledAvailabilitySnapshot;
|
||||
readonly baseSystemPrompt: string | undefined;
|
||||
|
||||
constructor(displayName: string, seedMessage: string, options?: PersonaOptions);
|
||||
constructor(
|
||||
displayName: string,
|
||||
seedMessage: string,
|
||||
options?: PersonaOptions,
|
||||
);
|
||||
constructor(spaceId: string, options?: PersonaOptions);
|
||||
constructor(first: string, second?: string | PersonaOptions, third?: PersonaOptions) {
|
||||
if (typeof second === 'string') {
|
||||
this.mode = { type: 'create', displayName: first, seedMessage: second };
|
||||
constructor(
|
||||
first: string,
|
||||
second?: string | PersonaOptions,
|
||||
third?: PersonaOptions,
|
||||
) {
|
||||
if (typeof second === "string") {
|
||||
this.mode = { type: "create", displayName: first, seedMessage: second };
|
||||
this.options = third ?? {};
|
||||
} else {
|
||||
this.mode = { type: 'load', spaceId: first };
|
||||
this.mode = { type: "load", spaceId: first };
|
||||
this.options = second ?? {};
|
||||
}
|
||||
this.memory = this.options.memory ?? new InMemoryMemoryStore();
|
||||
this.baseSystemPrompt = this.options.baseSystemPrompt;
|
||||
this.readyPromise = this.initialize();
|
||||
}
|
||||
|
||||
@@ -84,10 +97,17 @@ export class Persona {
|
||||
return this.readyPromise;
|
||||
}
|
||||
|
||||
async createDailySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> {
|
||||
get spaceId(): Promise<string> {
|
||||
return this.readyPromise.then((v) => v.id);
|
||||
}
|
||||
|
||||
async createDailySchedule(
|
||||
datetime: DateTimeInput,
|
||||
message: string,
|
||||
): Promise<ScheduleEntry[]> {
|
||||
const persona = await this.ready();
|
||||
if (!this.options.models?.schedule) {
|
||||
throw new Error('createDailySchedule requires options.models.schedule.');
|
||||
throw new Error("createDailySchedule requires options.models.schedule.");
|
||||
}
|
||||
const targetDay = scheduleTargetDay(datetime);
|
||||
const blocks = await this.options.models.schedule.generateDailySchedule({
|
||||
@@ -96,17 +116,31 @@ export class Persona {
|
||||
message,
|
||||
instruction: scheduleInstruction(),
|
||||
});
|
||||
const entries = blocksToDailySchedule({ persona, targetDay, message, blocks });
|
||||
await this.emit('persona.schedule.daily.generated', { targetDay: targetDay.toISOString(), count: entries.length, message });
|
||||
const entries = blocksToDailySchedule({
|
||||
persona,
|
||||
targetDay,
|
||||
message,
|
||||
blocks,
|
||||
});
|
||||
await this.emit("persona.schedule.daily.generated", {
|
||||
targetDay: targetDay.toISOString(),
|
||||
count: entries.length,
|
||||
message,
|
||||
});
|
||||
await this.memory.saveScheduleEntries(persona.id, entries);
|
||||
await this.refreshAvailability(datetime);
|
||||
return entries;
|
||||
}
|
||||
|
||||
async createMonthlySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> {
|
||||
async createMonthlySchedule(
|
||||
datetime: DateTimeInput,
|
||||
message: string,
|
||||
): Promise<ScheduleEntry[]> {
|
||||
const persona = await this.ready();
|
||||
if (!this.options.models?.schedule) {
|
||||
throw new Error('createMonthlySchedule requires options.models.schedule.');
|
||||
throw new Error(
|
||||
"createMonthlySchedule requires options.models.schedule.",
|
||||
);
|
||||
}
|
||||
const fromDay = scheduleTargetDay(datetime);
|
||||
const days = daysInMonth(fromDay);
|
||||
@@ -117,8 +151,16 @@ export class Persona {
|
||||
days,
|
||||
instruction: scheduleInstruction(),
|
||||
});
|
||||
const entries = blocksToMonthlySchedule({ persona, fromDay, message, blocks });
|
||||
await this.emit('persona.schedule.monthly.generated', { count: entries.length, message });
|
||||
const entries = blocksToMonthlySchedule({
|
||||
persona,
|
||||
fromDay,
|
||||
message,
|
||||
blocks,
|
||||
});
|
||||
await this.emit("persona.schedule.monthly.generated", {
|
||||
count: entries.length,
|
||||
message,
|
||||
});
|
||||
await this.memory.saveScheduleEntries(persona.id, entries);
|
||||
await this.refreshAvailability(datetime);
|
||||
return entries;
|
||||
@@ -127,14 +169,24 @@ export class Persona {
|
||||
async deleteSchedulesBefore(cutoffExclusive: DateTimeInput): Promise<number> {
|
||||
const persona = await this.ready();
|
||||
const cutoff = toIso(cutoffExclusive);
|
||||
const deleted = await this.memory.deleteScheduleEntriesBefore(persona.id, cutoff);
|
||||
const deleted = await this.memory.deleteScheduleEntriesBefore(
|
||||
persona.id,
|
||||
cutoff,
|
||||
);
|
||||
await this.memory.addFact(persona.id, {
|
||||
statement: `Schedules before ${cutoff} were deleted or marked inactive.`,
|
||||
topics: ['persona.schedule.deleted', 'schedule', cutoff.slice(0, 10)],
|
||||
source: 'boxbrain.schedule.prune',
|
||||
metadata: { boxbrainType: 'schedule-deletion', cutoffExclusive: cutoff, deleted },
|
||||
topics: ["persona.schedule.deleted", "schedule", cutoff.slice(0, 10)],
|
||||
source: "boxbrain.schedule.prune",
|
||||
metadata: {
|
||||
boxbrainType: "schedule-deletion",
|
||||
cutoffExclusive: cutoff,
|
||||
deleted,
|
||||
},
|
||||
});
|
||||
await this.emit("persona.schedule.deleted", {
|
||||
cutoffExclusive: cutoff,
|
||||
deleted,
|
||||
});
|
||||
await this.emit('persona.schedule.deleted', { cutoffExclusive: cutoff, deleted });
|
||||
return deleted;
|
||||
}
|
||||
|
||||
@@ -142,12 +194,15 @@ export class Persona {
|
||||
return this.deleteSchedulesBefore(datetime);
|
||||
}
|
||||
|
||||
async getTodayScheduledAvailability(datetime: DateTimeInput): Promise<ScheduledAvailabilitySnapshot> {
|
||||
async getTodayScheduledAvailability(
|
||||
datetime: DateTimeInput,
|
||||
): Promise<ScheduledAvailabilitySnapshot> {
|
||||
if (!this.availabilitySnapshot) {
|
||||
await this.refreshAvailability(datetime);
|
||||
}
|
||||
const snapshot = this.availabilitySnapshot;
|
||||
if (!snapshot) throw new Error('Availability snapshot was not initialized.');
|
||||
if (!snapshot)
|
||||
throw new Error("Availability snapshot was not initialized.");
|
||||
|
||||
const today = startOfUtcDay(datetime).toISOString();
|
||||
if (snapshot.windowStartAt !== today) {
|
||||
@@ -155,7 +210,8 @@ export class Persona {
|
||||
}
|
||||
|
||||
const refreshed = this.availabilitySnapshot;
|
||||
if (!refreshed) throw new Error('Availability snapshot was not initialized.');
|
||||
if (!refreshed)
|
||||
throw new Error("Availability snapshot was not initialized.");
|
||||
return refreshed;
|
||||
}
|
||||
|
||||
@@ -166,9 +222,11 @@ export class Persona {
|
||||
}): Promise<OutgoingMessageDraft> {
|
||||
const persona = await this.ready();
|
||||
if (!this.options.models?.conversation) {
|
||||
throw new Error('sendMessage requires options.models.conversation.');
|
||||
throw new Error("sendMessage requires options.models.conversation.");
|
||||
}
|
||||
const availability = await this.getTodayScheduledAvailability(input.datetime);
|
||||
const availability = await this.getTodayScheduledAvailability(
|
||||
input.datetime,
|
||||
);
|
||||
const context = await buildMandatoryConversationContext({
|
||||
persona,
|
||||
now: input.datetime,
|
||||
@@ -176,20 +234,24 @@ export class Persona {
|
||||
messages: input.messageHistory,
|
||||
availability,
|
||||
});
|
||||
await this.emit('persona.conversation.context.loaded', {
|
||||
await this.emit("persona.conversation.context.loaded", {
|
||||
factCount: context.personaAndUserFacts.length,
|
||||
scheduleEntryCount: context.scheduleEntries.length,
|
||||
});
|
||||
|
||||
const userMessage = [...input.messageHistory].reverse().find((message) => message.sender === 'user')?.content;
|
||||
let draft = ensureDraft(await this.options.models.conversation.generateReply({
|
||||
const userMessage = [...input.messageHistory]
|
||||
.reverse()
|
||||
.find((message) => message.sender === "user")?.content;
|
||||
let draft = ensureDraft(
|
||||
await this.options.models.conversation.generateReply({
|
||||
persona,
|
||||
now: toIso(input.datetime),
|
||||
mode: 'reply',
|
||||
mode: "reply",
|
||||
context,
|
||||
...(userMessage === undefined ? {} : { userMessage }),
|
||||
instruction: conversationInstruction(),
|
||||
}));
|
||||
instruction: conversationInstruction(this.baseSystemPrompt),
|
||||
}),
|
||||
);
|
||||
|
||||
if (input.getLatestMessageHistory && this.options.models.rewrite) {
|
||||
const latest = await input.getLatestMessageHistory();
|
||||
@@ -209,20 +271,28 @@ export class Persona {
|
||||
draft,
|
||||
context: latestContext,
|
||||
});
|
||||
await this.emit('persona.conversation.rewrite.checked', { rewrite: decision.rewrite, reason: decision.reason ?? null });
|
||||
await this.emit("persona.conversation.rewrite.checked", {
|
||||
rewrite: decision.rewrite,
|
||||
reason: decision.reason ?? null,
|
||||
});
|
||||
if (decision.rewrite) {
|
||||
draft = ensureDraft(decision.draft ?? await this.options.models.conversation.generateReply({
|
||||
draft = ensureDraft(
|
||||
decision.draft ??
|
||||
(await this.options.models.conversation.generateReply({
|
||||
persona,
|
||||
now: toIso(input.datetime),
|
||||
mode: 'reply',
|
||||
mode: "reply",
|
||||
context: latestContext,
|
||||
instruction: conversationInstruction(),
|
||||
}));
|
||||
instruction: conversationInstruction(this.baseSystemPrompt),
|
||||
})),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await this.emit('persona.conversation.reply.generated', { messageCount: draft.messages.length });
|
||||
await this.emit("persona.conversation.reply.generated", {
|
||||
messageCount: draft.messages.length,
|
||||
});
|
||||
return draft;
|
||||
}
|
||||
|
||||
@@ -232,9 +302,13 @@ export class Persona {
|
||||
}): Promise<OutgoingMessageDraft> {
|
||||
const persona = await this.ready();
|
||||
if (!this.options.models?.conversation) {
|
||||
throw new Error('startConversation requires options.models.conversation.');
|
||||
throw new Error(
|
||||
"startConversation requires options.models.conversation.",
|
||||
);
|
||||
}
|
||||
const availability = await this.getTodayScheduledAvailability(input.datetime);
|
||||
const availability = await this.getTodayScheduledAvailability(
|
||||
input.datetime,
|
||||
);
|
||||
const context = await buildMandatoryConversationContext({
|
||||
persona,
|
||||
now: input.datetime,
|
||||
@@ -242,81 +316,152 @@ export class Persona {
|
||||
messages: input.messageHistory,
|
||||
availability,
|
||||
});
|
||||
const draft = ensureDraft(await this.options.models.conversation.generateReply({
|
||||
const draft = ensureDraft(
|
||||
await this.options.models.conversation.generateReply({
|
||||
persona,
|
||||
now: toIso(input.datetime),
|
||||
mode: 'start-conversation',
|
||||
mode: "start-conversation",
|
||||
context,
|
||||
instruction: conversationInstruction(),
|
||||
}));
|
||||
await this.emit('persona.conversation.started', { messageCount: draft.messages.length });
|
||||
instruction: conversationInstruction(this.baseSystemPrompt),
|
||||
}),
|
||||
);
|
||||
await this.emit("persona.conversation.started", {
|
||||
messageCount: draft.messages.length,
|
||||
});
|
||||
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: {
|
||||
datetime: DateTimeInput;
|
||||
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({ 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',
|
||||
});
|
||||
const contextFacts = await this.memory.findFacts(persona.id, [
|
||||
"persona",
|
||||
persona.displayName,
|
||||
"user",
|
||||
]);
|
||||
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,
|
||||
}),
|
||||
].join("\n");
|
||||
const extractedFacts = (
|
||||
await extractFacts(statement, this.options.models.factExtractor)
|
||||
).map((fact) => this.extractedToDraft(fact, statement));
|
||||
|
||||
for (const fact of extractedFacts) {
|
||||
await this.memory.addFact(persona.id, fact);
|
||||
}
|
||||
await this.emit('persona.memory.sleep.persisted', { factCount: drafts.length });
|
||||
return drafts;
|
||||
await this.emit("persona.memory.sleep.persisted", {
|
||||
factCount: extractedFacts.length,
|
||||
});
|
||||
return extractedFacts;
|
||||
}
|
||||
|
||||
private async initialize(): Promise<MemorySpace> {
|
||||
const now = toIso(this.options.now ?? new Date());
|
||||
if (this.mode.type === 'load') {
|
||||
if (this.mode.type === "load") {
|
||||
const existing = await this.memory.getSpace(this.mode.spaceId);
|
||||
if (!existing) throw new Error(`Persona space not found: ${this.mode.spaceId}`);
|
||||
await this.emit('persona.loaded', { displayName: existing.displayName });
|
||||
if (!existing)
|
||||
throw new Error(`Persona space not found: ${this.mode.spaceId}`);
|
||||
await this.emit("persona.loaded", { displayName: existing.displayName });
|
||||
await this.refreshAvailability(now, existing);
|
||||
return existing;
|
||||
}
|
||||
|
||||
const space = await this.memory.createSpace({ displayName: this.mode.displayName, seedMessage: this.mode.seedMessage, now });
|
||||
const modelFacts = this.options.models?.initialization
|
||||
? await this.options.models.initialization.extractInitialFacts({
|
||||
const space = await this.memory.createSpace({
|
||||
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 extracteds = (
|
||||
await extractFacts(statement, this.options.models.factExtractor)
|
||||
).map((fact) => this.extractedToDraft(fact, statement));
|
||||
|
||||
for (const fact of extracteds) {
|
||||
await this.memory.addFact(space.id, fact);
|
||||
}
|
||||
await this.emit('persona.initialized', { displayName: space.displayName, factCount: facts.length }, space.id);
|
||||
|
||||
await this.emit(
|
||||
"persona.initialized",
|
||||
{ displayName: space.displayName, factCount: extracteds.length },
|
||||
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);
|
||||
return space;
|
||||
}
|
||||
|
||||
private async refreshAvailability(datetime: DateTimeInput, knownPersona?: MemorySpace): Promise<void> {
|
||||
const persona = knownPersona ?? await this.ready();
|
||||
private async refreshAvailability(
|
||||
datetime: DateTimeInput,
|
||||
knownPersona?: MemorySpace,
|
||||
): Promise<void> {
|
||||
const persona = knownPersona ?? (await this.ready());
|
||||
const start = startOfUtcDay(datetime);
|
||||
const end = addUtcDays(start, 2);
|
||||
const entries = await this.memory.listScheduleEntries(persona.id, start.toISOString(), end.toISOString());
|
||||
this.availabilitySnapshot = buildAvailabilitySnapshot({ now: datetime, entries });
|
||||
await this.emit('persona.availability.refreshed', { rangeCount: this.availabilitySnapshot.ranges.length }, persona.id);
|
||||
const entries = await this.memory.listScheduleEntries(
|
||||
persona.id,
|
||||
start.toISOString(),
|
||||
end.toISOString(),
|
||||
);
|
||||
this.availabilitySnapshot = buildAvailabilitySnapshot({
|
||||
now: datetime,
|
||||
entries,
|
||||
});
|
||||
await this.emit(
|
||||
"persona.availability.refreshed",
|
||||
{ rangeCount: this.availabilitySnapshot.ranges.length },
|
||||
persona.id,
|
||||
);
|
||||
}
|
||||
|
||||
private async emit(name: string, data?: Record<string, unknown>, explicitSpaceId?: string): Promise<void> {
|
||||
private async emit(
|
||||
name: string,
|
||||
data?: Record<string, unknown>,
|
||||
explicitSpaceId?: string,
|
||||
): Promise<void> {
|
||||
if (!this.options.debug) return;
|
||||
const event: DebugEvent = {
|
||||
name,
|
||||
|
||||
27
src/types.ts
27
src/types.ts
@@ -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;
|
||||
}
|
||||
|
||||
@@ -179,6 +160,7 @@ export interface PersonaOptions {
|
||||
models?: PersonaModels;
|
||||
debug?: DebugHook;
|
||||
now?: DateTimeInput;
|
||||
baseSystemPrompt?: string;
|
||||
}
|
||||
|
||||
export interface BoxBrainMemoryStore {
|
||||
@@ -190,4 +172,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>;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ describe('Conversation API', () => {
|
||||
memory,
|
||||
now: '2026-05-01T10:00:00.000Z',
|
||||
models: {
|
||||
initialization: { async extractInitialFacts() { return []; } },
|
||||
conversation: {
|
||||
async generateReply(input) {
|
||||
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({
|
||||
datetime: '2026-05-01T12:00:00.000Z',
|
||||
@@ -124,4 +124,30 @@ describe('Conversation API', () => {
|
||||
expect(mode).toBe('start-conversation');
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { InMemoryMemoryStore, Persona, type FactDraft } from '../src';
|
||||
import { InMemoryMemoryStore, Persona } from '../src';
|
||||
|
||||
describe('Persona initialization', () => {
|
||||
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',
|
||||
debug: (event) => { debug.push(event.name); },
|
||||
models: {
|
||||
initialization: {
|
||||
async extractInitialFacts(input): Promise<FactDraft[]> {
|
||||
return [
|
||||
{
|
||||
statement: `${input.displayName} likes quiet cafes.`,
|
||||
topics: ['persona', input.displayName],
|
||||
factExtractor: {
|
||||
async extract(input) {
|
||||
return [{
|
||||
statement: 'Mina likes quiet cafes.',
|
||||
topics: [{ name: 'persona' }, { name: 'Mina' }],
|
||||
source: 'test',
|
||||
},
|
||||
];
|
||||
}];
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -31,6 +29,18 @@ describe('Persona initialization', () => {
|
||||
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 () => {
|
||||
const memory = new InMemoryMemoryStore();
|
||||
const created = new Persona('Joon', 'Joon is a freelance designer.', { memory, now: '2026-05-01T10:00:00.000Z' });
|
||||
|
||||
@@ -8,17 +8,18 @@ describe('sleepMemory', () => {
|
||||
memory,
|
||||
now: '2026-05-01T10:00:00.000Z',
|
||||
models: {
|
||||
memoryExtraction: {
|
||||
factExtractor: {
|
||||
async extract(input) {
|
||||
expect(input.formattedMessageHistory).toContain('user@2026-05-01T15:00:00.000Z: 나는 타입스크립트를 2025년부터 시작했어');
|
||||
expect(input.instruction).toContain('Objectivize');
|
||||
return [
|
||||
{
|
||||
if (input.includes('Seed:')) {
|
||||
return [{ statement: 'Mina remembers stable details.', topics: [{ name: 'persona' }, { name: 'Mina' }] }];
|
||||
}
|
||||
expect(input).toContain('user@2026-05-01T15:00:00.000Z: 나는 타입스크립트를 2025년부터 시작했어');
|
||||
expect(input).toContain('Objectivize');
|
||||
return [{
|
||||
statement: 'The user started TypeScript in 2025.',
|
||||
topics: ['user', 'TypeScript', '2025'],
|
||||
topics: [{ name: 'user' }, { name: 'TypeScript' }, { name: '2025' }],
|
||||
confidence: 0.9,
|
||||
},
|
||||
];
|
||||
}];
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user