Compare commits
21 Commits
49f75afcf4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| d8da1ec998 | |||
| b52d37170c | |||
| 6f4f65a8ee | |||
| 488ba20eb6 | |||
| 05f077b798 | |||
| f964d4de9b | |||
| 882e12340c | |||
| fb89ffbc16 | |||
| 8e051a12e1 | |||
| c66b315fe5 | |||
| d2a3bfcd15 | |||
| 600f5ff0bc | |||
| 4ef1b89a2d | |||
| f9f37b0835 | |||
| 43b5147f45 | |||
| 239d63dff7 | |||
| 8bd6926a95 | |||
| bedbd01807 | |||
| 90214cec5c | |||
| 864f118a9b | |||
| 5d489bc875 |
29
.gitea/workflows/publish.yml
Normal file
29
.gitea/workflows/publish.yml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
name: Publish
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: oven/bun:1
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: https://gitea.com/actions/checkout@v6.0.2
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
- name: Test
|
||||||
|
run: bun run test
|
||||||
|
- name: Typecheck
|
||||||
|
run: bun run check
|
||||||
|
- name: Build
|
||||||
|
run: bun run build
|
||||||
|
- name: Publish to npm
|
||||||
|
run: |
|
||||||
|
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
|
||||||
|
bun publish --access public
|
||||||
|
env:
|
||||||
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
27
.npmignore
Normal file
27
.npmignore
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Dependency and build cache directories
|
||||||
|
node_modules/
|
||||||
|
.bun/
|
||||||
|
|
||||||
|
# Source, tests, and local development files
|
||||||
|
src/
|
||||||
|
tests/
|
||||||
|
scripts/
|
||||||
|
coverage/
|
||||||
|
tsconfig.json
|
||||||
|
bun.lock
|
||||||
|
|
||||||
|
# Repository and CI metadata
|
||||||
|
.git/
|
||||||
|
.gitea/
|
||||||
|
.gitignore
|
||||||
|
|
||||||
|
# Local runtime data and secrets
|
||||||
|
.data/
|
||||||
|
.hermes/
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Package artifacts
|
||||||
|
*.tgz
|
||||||
4
bun.lock
4
bun.lock
@@ -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=="],
|
||||||
|
|
||||||
|
|||||||
40
package.json
40
package.json
@@ -1,24 +1,54 @@
|
|||||||
{
|
{
|
||||||
"name": "boxbrain",
|
"name": "boxbrain",
|
||||||
"version": "0.1.0",
|
"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",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
"types": "src/index.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"import": "./dist/index.js",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"./package.json": "./package.json"
|
||||||
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"src",
|
|
||||||
"README.md"
|
"README.md"
|
||||||
],
|
],
|
||||||
|
"sideEffects": false,
|
||||||
|
"keywords": [
|
||||||
|
"llm",
|
||||||
|
"persona",
|
||||||
|
"memory",
|
||||||
|
"identitydb",
|
||||||
|
"agent"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://git.psw.kr/p-sw/BoxBrain.git"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://git.psw.kr/p-sw/BoxBrain/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://git.psw.kr/p-sw/BoxBrain",
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"check": "tsc --noEmit",
|
"check": "tsc --noEmit",
|
||||||
"build": "tsup src/index.ts --format esm --dts --sourcemap --clean",
|
"build": "tsup src/index.ts --format esm --dts --sourcemap --clean",
|
||||||
"prepare": "bun run build"
|
"clean": "rm -rf dist",
|
||||||
|
"pack:check": "bun scripts/check-package.mjs",
|
||||||
|
"prepack": "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",
|
||||||
|
|||||||
41
scripts/check-package.mjs
Normal file
41
scripts/check-package.mjs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { existsSync, readFileSync } from "node:fs";
|
||||||
|
|
||||||
|
const requiredFiles = [
|
||||||
|
"README.md",
|
||||||
|
"dist/index.js",
|
||||||
|
"dist/index.d.ts",
|
||||||
|
];
|
||||||
|
|
||||||
|
const missingFiles = requiredFiles.filter((path) => !existsSync(path));
|
||||||
|
|
||||||
|
if (missingFiles.length > 0) {
|
||||||
|
console.error(`Missing package artifact(s): ${missingFiles.join(", ")}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const packageJson = JSON.parse(readFileSync("package.json", "utf8"));
|
||||||
|
|
||||||
|
const expectedFields = {
|
||||||
|
main: "dist/index.js",
|
||||||
|
module: "dist/index.js",
|
||||||
|
types: "dist/index.d.ts",
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const [field, expected] of Object.entries(expectedFields)) {
|
||||||
|
if (packageJson[field] !== expected) {
|
||||||
|
console.error(`package.json ${field} must be ${expected}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packageJson.exports?.["."]?.types !== "./dist/index.d.ts") {
|
||||||
|
console.error("package.json exports[\".\"].types must point to ./dist/index.d.ts");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packageJson.exports?.["."]?.import !== "./dist/index.js") {
|
||||||
|
console.error("package.json exports[\".\"].import must point to ./dist/index.js");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Package metadata and artifacts are publish-ready.");
|
||||||
@@ -5,36 +5,31 @@ import type {
|
|||||||
MemorySpace,
|
MemorySpace,
|
||||||
PersonaMessage,
|
PersonaMessage,
|
||||||
ScheduledAvailabilitySnapshot,
|
ScheduledAvailabilitySnapshot,
|
||||||
} from './types';
|
} from "./types";
|
||||||
import { addUtcDays, buildAvailabilitySnapshot, dateKeysAround, startOfUtcDay, toIso } from './schedule';
|
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
|
return input.messages
|
||||||
.map((message) => {
|
.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}`;
|
return `${sender}@${toIso(message.time)}: ${message.content}`;
|
||||||
})
|
})
|
||||||
.join('\n');
|
.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function conversationInstruction(): string {
|
export function conversationInstruction(baseSystemPrompt?: string): string {
|
||||||
return [
|
const parts = [
|
||||||
'You are controlling the persona, not a generic assistant.',
|
...(baseSystemPrompt === undefined ? [] : [baseSystemPrompt]),
|
||||||
'Use the send_message tool conceptually: return one or more outgoing messages.',
|
"You are controlling the persona, not a generic assistant.",
|
||||||
'Unless the persona strongly prefers otherwise, keep each outgoing message to at most one sentence.',
|
"Use the send_message tool conceptually: return one or more outgoing messages.",
|
||||||
'Prefer short, natural, chat-like wording and allow splitting one thought into multiple 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.',
|
'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: {
|
||||||
@@ -47,15 +42,27 @@ export async function buildMandatoryConversationContext(input: {
|
|||||||
const now = startOfUtcDay(input.now);
|
const now = startOfUtcDay(input.now);
|
||||||
const from = addUtcDays(now, -1).toISOString();
|
const from = addUtcDays(now, -1).toISOString();
|
||||||
const to = addUtcDays(now, 2).toISOString();
|
const to = addUtcDays(now, 2).toISOString();
|
||||||
const scheduleEntries = await input.memory.listScheduleEntries(input.persona.id, from, to);
|
const scheduleEntries = await input.memory.listScheduleEntries(
|
||||||
const personaAndUserFacts = await input.memory.findFacts(input.persona.id, ['persona', input.persona.displayName, 'user']);
|
input.persona.id,
|
||||||
const memorySummary = personaAndUserFacts.length === 0
|
from,
|
||||||
? '기억이 없음'
|
to,
|
||||||
: personaAndUserFacts.map((fact) => `- ${fact.statement}`).join('\n');
|
);
|
||||||
|
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 {
|
return {
|
||||||
formattedMessageHistory: formatMessageHistory({ personaName: input.persona.displayName, messages: input.messages }),
|
formattedMessageHistory: formatMessageHistory({
|
||||||
conversationWindowLabel: `Required conversation window: yesterday/today. Schedule dates: ${dateKeysAround(input.now).join(', ')}.`,
|
personaName: input.persona.displayName,
|
||||||
|
messages: input.messages,
|
||||||
|
}),
|
||||||
|
conversationWindowLabel: `Required conversation window: yesterday/today. Schedule dates: ${dateKeysAround(input.now).join(", ")}.`,
|
||||||
memorySummary,
|
memorySummary,
|
||||||
personaAndUserFacts,
|
personaAndUserFacts,
|
||||||
scheduleEntries,
|
scheduleEntries,
|
||||||
|
|||||||
201
src/memory.ts
201
src/memory.ts
@@ -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 });
|
||||||
}
|
}
|
||||||
|
|||||||
331
src/persona.ts
331
src/persona.ts
@@ -1,19 +1,21 @@
|
|||||||
import { InMemoryMemoryStore } from './memory';
|
import { InMemoryMemoryStore } from "./memory";
|
||||||
import {
|
import {
|
||||||
addUtcDays,
|
addUtcDays,
|
||||||
|
blocksToDailySchedule,
|
||||||
|
blocksToMonthlySchedule,
|
||||||
buildAvailabilitySnapshot,
|
buildAvailabilitySnapshot,
|
||||||
createMonthlyScheduleEntries,
|
daysInMonth,
|
||||||
createTenMinuteDailySchedule,
|
scheduleInstruction,
|
||||||
scheduleTargetDay,
|
scheduleTargetDay,
|
||||||
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,
|
||||||
DateTimeInput,
|
DateTimeInput,
|
||||||
@@ -25,28 +27,32 @@ import type {
|
|||||||
PersonaOptions,
|
PersonaOptions,
|
||||||
ScheduleEntry,
|
ScheduleEntry,
|
||||||
ScheduledAvailabilitySnapshot,
|
ScheduledAvailabilitySnapshot,
|
||||||
} from './types';
|
} from "./types";
|
||||||
|
import { extractedToDraft } from "./utils";
|
||||||
|
|
||||||
interface CreateMode {
|
interface CreateMode {
|
||||||
type: 'create';
|
type: "create";
|
||||||
displayName: string;
|
displayName: string;
|
||||||
seedMessage: string;
|
seedMessage: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LoadMode {
|
interface LoadMode {
|
||||||
type: 'load';
|
type: "load";
|
||||||
spaceId: string;
|
spaceId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mode = CreateMode | LoadMode;
|
type Mode = CreateMode | LoadMode;
|
||||||
|
|
||||||
function defaultInitialFact(displayName: string, seedMessage: string): FactDraft {
|
function defaultInitialFact(
|
||||||
|
displayName: string,
|
||||||
|
seedMessage: string,
|
||||||
|
): FactDraft {
|
||||||
return {
|
return {
|
||||||
statement: `${displayName} is a BoxBrain persona initialized from this seed: ${seedMessage}`,
|
statement: `${displayName} is a BoxBrain persona initialized from this seed: ${seedMessage}`,
|
||||||
topics: ['persona', displayName],
|
topics: ["persona", displayName],
|
||||||
source: 'boxbrain.persona.initialization',
|
source: "boxbrain.persona.initialization",
|
||||||
confidence: 1,
|
confidence: 1,
|
||||||
metadata: { boxbrainType: 'persona-initial-fact' },
|
metadata: { boxbrainType: "persona-initial-fact" },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,18 +69,28 @@ 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(displayName: string, seedMessage: string, options?: PersonaOptions);
|
constructor(
|
||||||
|
displayName: string,
|
||||||
|
seedMessage: string,
|
||||||
|
options?: PersonaOptions,
|
||||||
|
);
|
||||||
constructor(spaceId: string, options?: PersonaOptions);
|
constructor(spaceId: string, options?: PersonaOptions);
|
||||||
constructor(first: string, second?: string | PersonaOptions, third?: PersonaOptions) {
|
constructor(
|
||||||
if (typeof second === 'string') {
|
first: string,
|
||||||
this.mode = { type: 'create', displayName: first, seedMessage: second };
|
second?: string | PersonaOptions,
|
||||||
|
third?: PersonaOptions,
|
||||||
|
) {
|
||||||
|
if (typeof second === "string") {
|
||||||
|
this.mode = { type: "create", displayName: first, seedMessage: second };
|
||||||
this.options = third ?? {};
|
this.options = third ?? {};
|
||||||
} else {
|
} else {
|
||||||
this.mode = { type: 'load', spaceId: first };
|
this.mode = { type: "load", spaceId: first };
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,20 +98,70 @@ export class Persona {
|
|||||||
return this.readyPromise;
|
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();
|
const persona = await this.ready();
|
||||||
|
if (!this.options.models?.schedule) {
|
||||||
|
throw new Error("createDailySchedule requires options.models.schedule.");
|
||||||
|
}
|
||||||
const targetDay = scheduleTargetDay(datetime);
|
const targetDay = scheduleTargetDay(datetime);
|
||||||
const entries = createTenMinuteDailySchedule({ persona, targetDay, message });
|
const blocks = await this.options.models.schedule.generateDailySchedule({
|
||||||
await this.emit('persona.schedule.daily.generated', { targetDay: targetDay.toISOString(), count: entries.length, message });
|
persona,
|
||||||
|
targetDay,
|
||||||
|
message,
|
||||||
|
instruction: scheduleInstruction(),
|
||||||
|
});
|
||||||
|
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.memory.saveScheduleEntries(persona.id, entries);
|
||||||
await this.refreshAvailability(datetime);
|
await this.refreshAvailability(datetime);
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createMonthlySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> {
|
async createMonthlySchedule(
|
||||||
|
datetime: DateTimeInput,
|
||||||
|
message: string,
|
||||||
|
): Promise<ScheduleEntry[]> {
|
||||||
const persona = await this.ready();
|
const persona = await this.ready();
|
||||||
const entries = createMonthlyScheduleEntries({ persona, fromDay: datetime, message });
|
if (!this.options.models?.schedule) {
|
||||||
await this.emit('persona.schedule.monthly.generated', { count: entries.length, message });
|
throw new Error(
|
||||||
|
"createMonthlySchedule requires options.models.schedule.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const fromDay = scheduleTargetDay(datetime);
|
||||||
|
const days = daysInMonth(fromDay);
|
||||||
|
const blocks = await this.options.models.schedule.generateMonthlySchedule({
|
||||||
|
persona,
|
||||||
|
fromDay,
|
||||||
|
message,
|
||||||
|
days,
|
||||||
|
instruction: scheduleInstruction(),
|
||||||
|
});
|
||||||
|
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.memory.saveScheduleEntries(persona.id, entries);
|
||||||
await this.refreshAvailability(datetime);
|
await this.refreshAvailability(datetime);
|
||||||
return entries;
|
return entries;
|
||||||
@@ -104,14 +170,24 @@ export class Persona {
|
|||||||
async deleteSchedulesBefore(cutoffExclusive: DateTimeInput): Promise<number> {
|
async deleteSchedulesBefore(cutoffExclusive: DateTimeInput): Promise<number> {
|
||||||
const persona = await this.ready();
|
const persona = await this.ready();
|
||||||
const cutoff = toIso(cutoffExclusive);
|
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, {
|
await this.memory.addFact(persona.id, {
|
||||||
statement: `Schedules before ${cutoff} were deleted or marked inactive.`,
|
statement: `Schedules before ${cutoff} were deleted or marked inactive.`,
|
||||||
topics: ['persona.schedule.deleted', 'schedule', cutoff.slice(0, 10)],
|
topics: ["persona.schedule.deleted", "schedule", cutoff.slice(0, 10)],
|
||||||
source: 'boxbrain.schedule.prune',
|
source: "boxbrain.schedule.prune",
|
||||||
metadata: { boxbrainType: 'schedule-deletion', cutoffExclusive: cutoff, deleted },
|
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;
|
return deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,12 +195,15 @@ export class Persona {
|
|||||||
return this.deleteSchedulesBefore(datetime);
|
return this.deleteSchedulesBefore(datetime);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTodayScheduledAvailability(datetime: DateTimeInput): Promise<ScheduledAvailabilitySnapshot> {
|
async getTodayScheduledAvailability(
|
||||||
|
datetime: DateTimeInput,
|
||||||
|
): Promise<ScheduledAvailabilitySnapshot> {
|
||||||
if (!this.availabilitySnapshot) {
|
if (!this.availabilitySnapshot) {
|
||||||
await this.refreshAvailability(datetime);
|
await this.refreshAvailability(datetime);
|
||||||
}
|
}
|
||||||
const snapshot = this.availabilitySnapshot;
|
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();
|
const today = startOfUtcDay(datetime).toISOString();
|
||||||
if (snapshot.windowStartAt !== today) {
|
if (snapshot.windowStartAt !== today) {
|
||||||
@@ -132,7 +211,8 @@ export class Persona {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const refreshed = this.availabilitySnapshot;
|
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;
|
return refreshed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,9 +223,11 @@ export class Persona {
|
|||||||
}): Promise<OutgoingMessageDraft> {
|
}): Promise<OutgoingMessageDraft> {
|
||||||
const persona = await this.ready();
|
const persona = await this.ready();
|
||||||
if (!this.options.models?.conversation) {
|
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({
|
const context = await buildMandatoryConversationContext({
|
||||||
persona,
|
persona,
|
||||||
now: input.datetime,
|
now: input.datetime,
|
||||||
@@ -153,20 +235,24 @@ export class Persona {
|
|||||||
messages: input.messageHistory,
|
messages: input.messageHistory,
|
||||||
availability,
|
availability,
|
||||||
});
|
});
|
||||||
await this.emit('persona.conversation.context.loaded', {
|
await this.emit("persona.conversation.context.loaded", {
|
||||||
factCount: context.personaAndUserFacts.length,
|
factCount: context.personaAndUserFacts.length,
|
||||||
scheduleEntryCount: context.scheduleEntries.length,
|
scheduleEntryCount: context.scheduleEntries.length,
|
||||||
});
|
});
|
||||||
|
|
||||||
const userMessage = [...input.messageHistory].reverse().find((message) => message.sender === 'user')?.content;
|
const userMessage = [...input.messageHistory]
|
||||||
let draft = ensureDraft(await this.options.models.conversation.generateReply({
|
.reverse()
|
||||||
|
.find((message) => message.sender === "user")?.content;
|
||||||
|
let draft = ensureDraft(
|
||||||
|
await this.options.models.conversation.generateReply({
|
||||||
persona,
|
persona,
|
||||||
now: toIso(input.datetime),
|
now: toIso(input.datetime),
|
||||||
mode: 'reply',
|
mode: "reply",
|
||||||
context,
|
context,
|
||||||
...(userMessage === undefined ? {} : { userMessage }),
|
...(userMessage === undefined ? {} : { userMessage }),
|
||||||
instruction: conversationInstruction(),
|
instruction: conversationInstruction(this.baseSystemPrompt),
|
||||||
}));
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
if (input.getLatestMessageHistory && this.options.models.rewrite) {
|
if (input.getLatestMessageHistory && this.options.models.rewrite) {
|
||||||
const latest = await input.getLatestMessageHistory();
|
const latest = await input.getLatestMessageHistory();
|
||||||
@@ -186,20 +272,28 @@ export class Persona {
|
|||||||
draft,
|
draft,
|
||||||
context: latestContext,
|
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) {
|
if (decision.rewrite) {
|
||||||
draft = ensureDraft(decision.draft ?? await this.options.models.conversation.generateReply({
|
draft = ensureDraft(
|
||||||
|
decision.draft ??
|
||||||
|
(await this.options.models.conversation.generateReply({
|
||||||
persona,
|
persona,
|
||||||
now: toIso(input.datetime),
|
now: toIso(input.datetime),
|
||||||
mode: 'reply',
|
mode: "reply",
|
||||||
context: latestContext,
|
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;
|
return draft;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,9 +303,13 @@ export class Persona {
|
|||||||
}): Promise<OutgoingMessageDraft> {
|
}): Promise<OutgoingMessageDraft> {
|
||||||
const persona = await this.ready();
|
const persona = await this.ready();
|
||||||
if (!this.options.models?.conversation) {
|
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({
|
const context = await buildMandatoryConversationContext({
|
||||||
persona,
|
persona,
|
||||||
now: input.datetime,
|
now: input.datetime,
|
||||||
@@ -219,14 +317,18 @@ export class Persona {
|
|||||||
messages: input.messageHistory,
|
messages: input.messageHistory,
|
||||||
availability,
|
availability,
|
||||||
});
|
});
|
||||||
const draft = ensureDraft(await this.options.models.conversation.generateReply({
|
const draft = ensureDraft(
|
||||||
|
await this.options.models.conversation.generateReply({
|
||||||
persona,
|
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', { messageCount: draft.messages.length });
|
);
|
||||||
|
await this.emit("persona.conversation.started", {
|
||||||
|
messageCount: draft.messages.length,
|
||||||
|
});
|
||||||
return draft;
|
return draft;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,65 +337,118 @@ 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, ['persona', persona.displayName, 'user']);
|
const contextFacts = await this.memory.findFacts(persona.id, [
|
||||||
const drafts = await this.options.models.memoryExtraction.extract({
|
"persona",
|
||||||
persona,
|
persona.displayName,
|
||||||
now: toIso(input.datetime),
|
"user",
|
||||||
formattedMessageHistory: formatMessageHistory({ personaName: persona.displayName, messages: input.messageHistory }),
|
]);
|
||||||
contextFacts,
|
const statement = [
|
||||||
instruction: memoryExtractionInstruction(toIso(input.datetime)),
|
`Current objective time: ${toIso(input.datetime)}.`,
|
||||||
});
|
"Read the message history and extract durable facts worth remembering.",
|
||||||
for (const draft of drafts) {
|
"Objectivize subjective statements before storage.",
|
||||||
await this.memory.addFact(persona.id, {
|
'Example: "I started TypeScript in 2025" becomes "The user started TypeScript in 2025."',
|
||||||
...draft,
|
"Prefer facts about the persona, the user, their relationship, preferences, history, schedule-relevant events, and stable traits.",
|
||||||
topics: [...draft.topics, 'sleepMemory'],
|
"",
|
||||||
source: draft.source ?? 'boxbrain.sleepMemory',
|
"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) => 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 });
|
await this.emit("persona.memory.sleep.persisted", {
|
||||||
return drafts;
|
factCount: extractedFacts.length,
|
||||||
|
});
|
||||||
|
return extractedFacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async initialize(): Promise<MemorySpace> {
|
private async initialize(): Promise<MemorySpace> {
|
||||||
const now = toIso(this.options.now ?? new Date());
|
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);
|
const existing = await this.memory.getSpace(this.mode.spaceId);
|
||||||
if (!existing) throw new Error(`Persona space not found: ${this.mode.spaceId}`);
|
if (!existing)
|
||||||
await this.emit('persona.loaded', { displayName: existing.displayName });
|
throw new Error(`Persona space not found: ${this.mode.spaceId}`);
|
||||||
|
await this.emit("persona.loaded", { displayName: existing.displayName });
|
||||||
await this.refreshAvailability(now, existing);
|
await this.refreshAvailability(now, existing);
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
const space = await this.memory.createSpace({ displayName: this.mode.displayName, seedMessage: this.mode.seedMessage, now });
|
const space = await this.memory.createSpace({
|
||||||
const modelFacts = this.options.models?.initialization
|
|
||||||
? await this.options.models.initialization.extractInitialFacts({
|
|
||||||
displayName: this.mode.displayName,
|
displayName: this.mode.displayName,
|
||||||
seedMessage: this.mode.seedMessage,
|
seedMessage: this.mode.seedMessage,
|
||||||
now,
|
now,
|
||||||
})
|
});
|
||||||
: undefined;
|
if (this.options.models?.factExtractor) {
|
||||||
const facts = modelFacts ?? [defaultInitialFact(this.mode.displayName, this.mode.seedMessage)];
|
const statement = `Persona: ${this.mode.displayName}\nSeed: ${this.mode.seedMessage}`;
|
||||||
for (const fact of facts) {
|
const extracteds = (
|
||||||
|
await extractFacts(statement, this.options.models.factExtractor)
|
||||||
|
).map((fact) => extractedToDraft(fact, statement));
|
||||||
|
|
||||||
|
for (const fact of extracteds) {
|
||||||
await this.memory.addFact(space.id, fact);
|
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);
|
await this.refreshAvailability(now, space);
|
||||||
return space;
|
return space;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async refreshAvailability(datetime: DateTimeInput, knownPersona?: MemorySpace): Promise<void> {
|
private async refreshAvailability(
|
||||||
const persona = knownPersona ?? await this.ready();
|
datetime: DateTimeInput,
|
||||||
|
knownPersona?: MemorySpace,
|
||||||
|
): Promise<void> {
|
||||||
|
const persona = knownPersona ?? (await this.ready());
|
||||||
const start = startOfUtcDay(datetime);
|
const start = startOfUtcDay(datetime);
|
||||||
const end = addUtcDays(start, 2);
|
const end = addUtcDays(start, 2);
|
||||||
const entries = await this.memory.listScheduleEntries(persona.id, start.toISOString(), end.toISOString());
|
const entries = await this.memory.listScheduleEntries(
|
||||||
this.availabilitySnapshot = buildAvailabilitySnapshot({ now: datetime, entries });
|
persona.id,
|
||||||
await this.emit('persona.availability.refreshed', { rangeCount: this.availabilitySnapshot.ranges.length }, 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;
|
if (!this.options.debug) return;
|
||||||
const event: DebugEvent = {
|
const event: DebugEvent = {
|
||||||
name,
|
name,
|
||||||
|
|||||||
131
src/schedule.ts
131
src/schedule.ts
@@ -3,12 +3,11 @@ import type {
|
|||||||
AvailabilityRange,
|
AvailabilityRange,
|
||||||
DateTimeInput,
|
DateTimeInput,
|
||||||
MemorySpace,
|
MemorySpace,
|
||||||
ScheduleActivity,
|
ScheduleBlock,
|
||||||
ScheduleEntry,
|
ScheduleEntry,
|
||||||
ScheduledAvailabilitySnapshot,
|
ScheduledAvailabilitySnapshot,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
const TEN_MINUTES_MS = 10 * 60 * 1000;
|
|
||||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
export function toDate(input: DateTimeInput): Date {
|
export function toDate(input: DateTimeInput): Date {
|
||||||
@@ -36,135 +35,87 @@ export function scheduleTargetDay(now: DateTimeInput): Date {
|
|||||||
return addUtcDays(now, 1);
|
return addUtcDays(now, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pick(activity: ScheduleActivity): { title: string; mode: AvailabilityMode } {
|
export function daysInMonth(date: Date): number {
|
||||||
switch (activity) {
|
const year = date.getUTCFullYear();
|
||||||
case 'sleep':
|
const month = date.getUTCMonth();
|
||||||
return { title: 'Sleep', mode: 'offline' };
|
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
||||||
case 'work':
|
|
||||||
return { title: 'Work', mode: 'do-not-disturb' };
|
|
||||||
case 'study':
|
|
||||||
return { title: 'Study', mode: 'do-not-disturb' };
|
|
||||||
case 'job-search':
|
|
||||||
return { title: 'Job search', mode: 'do-not-disturb' };
|
|
||||||
case 'travel':
|
|
||||||
return { title: 'Travel', mode: 'do-not-disturb' };
|
|
||||||
case 'commute':
|
|
||||||
return { title: 'Commute', mode: 'do-not-disturb' };
|
|
||||||
case 'exercise':
|
|
||||||
return { title: 'Exercise', mode: 'online' };
|
|
||||||
case 'meal':
|
|
||||||
return { title: 'Meal', mode: 'online' };
|
|
||||||
case 'social':
|
|
||||||
return { title: 'Social time', mode: 'online' };
|
|
||||||
case 'errand':
|
|
||||||
return { title: 'Errand', mode: 'online' };
|
|
||||||
case 'free-time':
|
|
||||||
return { title: 'Free time', mode: 'online' };
|
|
||||||
case 'rest':
|
|
||||||
return { title: 'Rest', mode: 'online' };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function chooseDaytimeActivity(message: string): ScheduleActivity {
|
export function scheduleInstruction(): string {
|
||||||
const lower = message.toLowerCase();
|
return [
|
||||||
if (lower.includes('travel') || lower.includes('trip') || lower.includes('여행')) return 'travel';
|
'Generate realistic schedule blocks for the persona based on their profile and the provided message.',
|
||||||
if (lower.includes('study') || lower.includes('exam') || lower.includes('공부') || lower.includes('시험')) return 'study';
|
'Activity types should be creative and context-appropriate; do not limit yourself to a fixed list.',
|
||||||
if (lower.includes('job') || lower.includes('취업') || lower.includes('구직')) return 'job-search';
|
'For each block, choose an availability mode: online (available to chat), do-not-disturb (busy but reachable for urgent matters), or offline (completely unavailable).',
|
||||||
if (lower.includes('work') || lower.includes('일') || lower.includes('회사')) return 'work';
|
'Return blocks covering the requested time range with startTime and endTime in HH:MM 24-hour format.',
|
||||||
return 'work';
|
].join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function activityForMinute(minuteOfDay: number, message: string): ScheduleActivity {
|
export function blocksToDailySchedule(input: {
|
||||||
const hour = Math.floor(minuteOfDay / 60);
|
|
||||||
if (hour < 7) return 'sleep';
|
|
||||||
if (hour === 7) return 'meal';
|
|
||||||
if (hour === 8) return 'commute';
|
|
||||||
if (hour >= 9 && hour < 12) return chooseDaytimeActivity(message);
|
|
||||||
if (hour === 12) return 'meal';
|
|
||||||
if (hour >= 13 && hour < 17) return chooseDaytimeActivity(message);
|
|
||||||
if (hour === 17) return 'commute';
|
|
||||||
if (hour === 18) return 'meal';
|
|
||||||
if (hour >= 19 && hour < 21) return message.toLowerCase().includes('study') || message.includes('공부') ? 'study' : 'free-time';
|
|
||||||
if (hour >= 21 && hour < 23) return 'rest';
|
|
||||||
return 'sleep';
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createTenMinuteDailySchedule(input: {
|
|
||||||
persona: MemorySpace;
|
persona: MemorySpace;
|
||||||
targetDay: DateTimeInput;
|
targetDay: DateTimeInput;
|
||||||
message: string;
|
message: string;
|
||||||
|
blocks: ScheduleBlock[];
|
||||||
}): ScheduleEntry[] {
|
}): ScheduleEntry[] {
|
||||||
const target = startOfUtcDay(input.targetDay);
|
const target = startOfUtcDay(input.targetDay);
|
||||||
const entries: ScheduleEntry[] = [];
|
return input.blocks.map((block) => {
|
||||||
|
const [startHour, startMinute] = block.startTime.split(':').map(Number) as [number, number];
|
||||||
for (let offset = 0; offset < DAY_MS; offset += TEN_MINUTES_MS) {
|
const [endHour, endMinute] = block.endTime.split(':').map(Number) as [number, number];
|
||||||
const start = new Date(target.getTime() + offset);
|
const start = new Date(target.getTime() + ((startHour * 60 + startMinute) * 60 * 1000));
|
||||||
const end = new Date(start.getTime() + TEN_MINUTES_MS);
|
const end = new Date(target.getTime() + ((endHour * 60 + endMinute) * 60 * 1000));
|
||||||
const minute = offset / (60 * 1000);
|
return {
|
||||||
const activity = activityForMinute(minute, input.message);
|
|
||||||
const picked = pick(activity);
|
|
||||||
entries.push({
|
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
spaceId: input.persona.id,
|
spaceId: input.persona.id,
|
||||||
startAt: start.toISOString(),
|
startAt: start.toISOString(),
|
||||||
endAt: end.toISOString(),
|
endAt: end.toISOString(),
|
||||||
activity,
|
activity: block.activity,
|
||||||
title: picked.title,
|
title: block.title,
|
||||||
description: `Realistic ${picked.title.toLowerCase()} block for ${input.persona.displayName}.`,
|
description: block.description ?? `Realistic ${block.title.toLowerCase()} block for ${input.persona.displayName}.`,
|
||||||
granularity: 'ten-minute',
|
granularity: 'ten-minute',
|
||||||
sourceMessage: input.message,
|
sourceMessage: input.message,
|
||||||
metadata: {
|
metadata: {
|
||||||
boxbrainType: 'schedule-entry',
|
boxbrainType: 'schedule-entry',
|
||||||
availabilityMode: picked.mode,
|
availabilityMode: block.availabilityMode,
|
||||||
targetDate: target.toISOString().slice(0, 10),
|
targetDate: target.toISOString().slice(0, 10),
|
||||||
},
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return entries;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createMonthlyScheduleEntries(input: {
|
export function blocksToMonthlySchedule(input: {
|
||||||
persona: MemorySpace;
|
persona: MemorySpace;
|
||||||
fromDay: DateTimeInput;
|
fromDay: DateTimeInput;
|
||||||
message: string;
|
message: string;
|
||||||
days?: number;
|
blocks: ScheduleBlock[];
|
||||||
}): ScheduleEntry[] {
|
}): ScheduleEntry[] {
|
||||||
const start = scheduleTargetDay(input.fromDay);
|
const start = startOfUtcDay(input.fromDay);
|
||||||
const count = input.days ?? 30;
|
return input.blocks.map((block, day) => {
|
||||||
const entries: ScheduleEntry[] = [];
|
|
||||||
for (let day = 0; day < count; day += 1) {
|
|
||||||
const dayStart = new Date(start.getTime() + day * DAY_MS);
|
const dayStart = new Date(start.getTime() + day * DAY_MS);
|
||||||
const travelHint = day > 0 && day % 90 === 0 ? ' travel' : '';
|
const [startHour, startMinute] = block.startTime.split(':').map(Number) as [number, number];
|
||||||
const activity = chooseDaytimeActivity(`${input.message}${travelHint}`);
|
const [endHour, endMinute] = block.endTime.split(':').map(Number) as [number, number];
|
||||||
const picked = pick(activity);
|
const entryStart = new Date(dayStart.getTime() + ((startHour * 60 + startMinute) * 60 * 1000));
|
||||||
entries.push({
|
const entryEnd = new Date(dayStart.getTime() + ((endHour * 60 + endMinute) * 60 * 1000));
|
||||||
|
return {
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
spaceId: input.persona.id,
|
spaceId: input.persona.id,
|
||||||
startAt: dayStart.toISOString(),
|
startAt: entryStart.toISOString(),
|
||||||
endAt: new Date(dayStart.getTime() + DAY_MS).toISOString(),
|
endAt: entryEnd.toISOString(),
|
||||||
activity,
|
activity: block.activity,
|
||||||
title: picked.title,
|
title: block.title,
|
||||||
description: `Daily outline for ${input.persona.displayName}.`,
|
description: block.description ?? `Daily outline for ${input.persona.displayName}.`,
|
||||||
granularity: 'day',
|
granularity: 'day',
|
||||||
sourceMessage: input.message,
|
sourceMessage: input.message,
|
||||||
metadata: {
|
metadata: {
|
||||||
boxbrainType: 'schedule-entry',
|
boxbrainType: 'schedule-entry',
|
||||||
availabilityMode: picked.mode,
|
availabilityMode: block.availabilityMode,
|
||||||
targetDate: dayStart.toISOString().slice(0, 10),
|
targetDate: dayStart.toISOString().slice(0, 10),
|
||||||
},
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
|
||||||
return entries;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function availabilityModeForEntry(entry: ScheduleEntry): AvailabilityMode {
|
export function availabilityModeForEntry(entry: ScheduleEntry): AvailabilityMode {
|
||||||
const mode = entry.metadata['availabilityMode'];
|
const mode = entry.metadata['availabilityMode'];
|
||||||
if (mode === 'online' || mode === 'do-not-disturb' || mode === 'offline') return mode;
|
if (mode === 'online' || mode === 'do-not-disturb' || mode === 'offline') return mode;
|
||||||
if (entry.activity === 'sleep') return 'offline';
|
|
||||||
if (entry.activity === 'work' || entry.activity === 'study' || entry.activity === 'job-search' || entry.activity === 'travel' || entry.activity === 'commute') {
|
|
||||||
return 'do-not-disturb';
|
|
||||||
}
|
|
||||||
return 'online';
|
return 'online';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
90
src/types.ts
90
src/types.ts
@@ -1,24 +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 =
|
export type ScheduleActivity = string;
|
||||||
| 'sleep'
|
|
||||||
| 'rest'
|
|
||||||
| 'meal'
|
|
||||||
| 'commute'
|
|
||||||
| 'work'
|
|
||||||
| 'study'
|
|
||||||
| 'job-search'
|
|
||||||
| 'travel'
|
|
||||||
| 'exercise'
|
|
||||||
| 'social'
|
|
||||||
| 'errand'
|
|
||||||
| 'free-time';
|
|
||||||
|
|
||||||
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;
|
||||||
@@ -69,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;
|
||||||
}
|
}
|
||||||
@@ -95,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;
|
||||||
@@ -129,31 +119,44 @@ export interface RewriteModel {
|
|||||||
decide(input: RewriteDecisionInput): Promise<RewriteDecision>;
|
decide(input: RewriteDecisionInput): Promise<RewriteDecision>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MemoryExtractionInput {
|
export interface ScheduleBlock {
|
||||||
|
startTime: string;
|
||||||
|
endTime: string;
|
||||||
|
activity: string;
|
||||||
|
title: string;
|
||||||
|
description?: string;
|
||||||
|
availabilityMode: AvailabilityMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DailyScheduleGenerationInput {
|
||||||
persona: MemorySpace;
|
persona: MemorySpace;
|
||||||
now: string;
|
targetDay: Date;
|
||||||
formattedMessageHistory: string;
|
message: string;
|
||||||
contextFacts: StoredFact[];
|
|
||||||
instruction: string;
|
instruction: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MemoryExtractionModel {
|
export interface MonthlyScheduleGenerationInput {
|
||||||
extract(input: MemoryExtractionInput): Promise<FactDraft[]>;
|
persona: MemorySpace;
|
||||||
|
fromDay: Date;
|
||||||
|
message: string;
|
||||||
|
days: number;
|
||||||
|
instruction: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PersonaInitializationModel {
|
export interface ScheduleModel {
|
||||||
extractInitialFacts(input: {
|
generateDailySchedule(
|
||||||
displayName: string;
|
input: DailyScheduleGenerationInput,
|
||||||
seedMessage: string;
|
): Promise<ScheduleBlock[]>;
|
||||||
now: string;
|
generateMonthlySchedule(
|
||||||
}): Promise<FactDraft[]>;
|
input: MonthlyScheduleGenerationInput,
|
||||||
|
): Promise<ScheduleBlock[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PersonaModels {
|
export interface PersonaModels {
|
||||||
initialization?: PersonaInitializationModel;
|
factExtractor?: FactExtractor;
|
||||||
conversation?: ConversationModel;
|
conversation?: ConversationModel;
|
||||||
rewrite?: RewriteModel;
|
rewrite?: RewriteModel;
|
||||||
memoryExtraction?: MemoryExtractionModel;
|
schedule?: ScheduleModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PersonaOptions {
|
export interface PersonaOptions {
|
||||||
@@ -161,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
19
src/utils.ts
Normal 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> }
|
||||||
|
: {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -15,6 +15,16 @@ describe('Conversation API', () => {
|
|||||||
return { messages: ['카페에 있었어.', '너는 뭐해?'] };
|
return { messages: ['카페에 있었어.', '너는 뭐해?'] };
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
schedule: {
|
||||||
|
async generateDailySchedule() {
|
||||||
|
return [
|
||||||
|
{ startTime: '09:00', endTime: '18:00', activity: 'work', title: 'Work', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
];
|
||||||
|
},
|
||||||
|
async generateMonthlySchedule() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const space = await persona.ready();
|
const space = await persona.ready();
|
||||||
@@ -45,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;
|
||||||
@@ -54,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',
|
||||||
@@ -114,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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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' });
|
||||||
|
|||||||
248
tests/schedule-utils.test.ts
Normal file
248
tests/schedule-utils.test.ts
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
addUtcDays,
|
||||||
|
availabilityModeForEntry,
|
||||||
|
blocksToDailySchedule,
|
||||||
|
blocksToMonthlySchedule,
|
||||||
|
buildAvailabilitySnapshot,
|
||||||
|
dateKeysAround,
|
||||||
|
daysInMonth,
|
||||||
|
scheduleTargetDay,
|
||||||
|
startOfUtcDay,
|
||||||
|
toDate,
|
||||||
|
toIso,
|
||||||
|
} from '../src';
|
||||||
|
|
||||||
|
describe('toDate', () => {
|
||||||
|
it('accepts a Date instance', () => {
|
||||||
|
const original = new Date('2026-05-01T12:34:56.789Z');
|
||||||
|
const result = toDate(original);
|
||||||
|
expect(result.toISOString()).toBe('2026-05-01T12:34:56.789Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts an ISO string', () => {
|
||||||
|
const result = toDate('2026-05-01T12:00:00.000Z');
|
||||||
|
expect(result.toISOString()).toBe('2026-05-01T12:00:00.000Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts a timestamp number', () => {
|
||||||
|
const ts = new Date('2026-05-01T00:00:00.000Z').getTime();
|
||||||
|
const result = toDate(ts);
|
||||||
|
expect(result.toISOString()).toBe('2026-05-01T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on invalid input', () => {
|
||||||
|
expect(() => toDate('not-a-date')).toThrow('Invalid datetime: not-a-date');
|
||||||
|
expect(() => toDate(NaN)).toThrow('Invalid datetime: NaN');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toIso', () => {
|
||||||
|
it('converts any DateTimeInput to an ISO string', () => {
|
||||||
|
expect(toIso(new Date('2026-05-01T12:00:00.000Z'))).toBe('2026-05-01T12:00:00.000Z');
|
||||||
|
expect(toIso('2026-05-01T12:00:00.000Z')).toBe('2026-05-01T12:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('startOfUtcDay', () => {
|
||||||
|
it('strips time to 00:00:00 UTC', () => {
|
||||||
|
const result = startOfUtcDay('2026-05-01T15:30:45.000Z');
|
||||||
|
expect(result.toISOString()).toBe('2026-05-01T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works across month boundaries', () => {
|
||||||
|
const result = startOfUtcDay('2026-03-31T23:59:59.999Z');
|
||||||
|
expect(result.toISOString()).toBe('2026-03-31T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('addUtcDays', () => {
|
||||||
|
it('adds days', () => {
|
||||||
|
const result = addUtcDays('2026-05-01T12:00:00.000Z', 5);
|
||||||
|
expect(result.toISOString()).toBe('2026-05-06T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('subtracts days', () => {
|
||||||
|
const result = addUtcDays('2026-05-01T12:00:00.000Z', -1);
|
||||||
|
expect(result.toISOString()).toBe('2026-04-30T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('scheduleTargetDay', () => {
|
||||||
|
it('returns tomorrow at UTC midnight', () => {
|
||||||
|
const result = scheduleTargetDay('2026-05-01T10:00:00.000Z');
|
||||||
|
expect(result.toISOString()).toBe('2026-05-02T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('daysInMonth', () => {
|
||||||
|
it('returns 31 for January', () => {
|
||||||
|
expect(daysInMonth(new Date('2026-01-15T00:00:00.000Z'))).toBe(31);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 28 for February in a non-leap year', () => {
|
||||||
|
expect(daysInMonth(new Date('2026-02-15T00:00:00.000Z'))).toBe(28);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 29 for February in a leap year', () => {
|
||||||
|
expect(daysInMonth(new Date('2024-02-15T00:00:00.000Z'))).toBe(29);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 30 for April', () => {
|
||||||
|
expect(daysInMonth(new Date('2026-04-15T00:00:00.000Z'))).toBe(30);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 31 for December', () => {
|
||||||
|
expect(daysInMonth(new Date('2026-12-15T00:00:00.000Z'))).toBe(31);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('blocksToDailySchedule', () => {
|
||||||
|
it('converts schedule blocks to entries for the target day', () => {
|
||||||
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
||||||
|
const blocks = [
|
||||||
|
{ startTime: '00:00', endTime: '07:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
||||||
|
{ startTime: '07:00', endTime: '09:00', activity: 'morning routine', title: 'Morning routine', availabilityMode: 'online' as const },
|
||||||
|
];
|
||||||
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T10:00:00.000Z', message: 'msg', blocks });
|
||||||
|
|
||||||
|
expect(entries).toHaveLength(2);
|
||||||
|
expect(entries[0]!.startAt).toBe('2026-05-02T00:00:00.000Z');
|
||||||
|
expect(entries[0]!.endAt).toBe('2026-05-02T07:00:00.000Z');
|
||||||
|
expect(entries[0]!.activity).toBe('sleep');
|
||||||
|
expect(entries[0]!.granularity).toBe('ten-minute');
|
||||||
|
expect(entries[0]!.metadata['availabilityMode']).toBe('offline');
|
||||||
|
expect(entries[1]!.startAt).toBe('2026-05-02T07:00:00.000Z');
|
||||||
|
expect(entries[1]!.endAt).toBe('2026-05-02T09:00:00.000Z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses block description when provided', () => {
|
||||||
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
||||||
|
const blocks = [
|
||||||
|
{ startTime: '09:00', endTime: '18:00', activity: 'work', title: 'Work', description: 'Custom desc', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
];
|
||||||
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
||||||
|
expect(entries[0]!.description).toBe('Custom desc');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('supports 24:00 as end time to reach next midnight', () => {
|
||||||
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
||||||
|
const blocks = [
|
||||||
|
{ startTime: '18:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
||||||
|
];
|
||||||
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
||||||
|
expect(entries[0]!.endAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('blocksToMonthlySchedule', () => {
|
||||||
|
it('maps each block to consecutive days starting from fromDay', () => {
|
||||||
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
||||||
|
const blocks = [
|
||||||
|
{ startTime: '00:00', endTime: '24:00', activity: 'work', title: 'Work', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
{ startTime: '00:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
||||||
|
];
|
||||||
|
const entries = blocksToMonthlySchedule({ persona, fromDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
||||||
|
|
||||||
|
expect(entries).toHaveLength(2);
|
||||||
|
expect(entries[0]!.startAt).toBe('2026-05-02T00:00:00.000Z');
|
||||||
|
expect(entries[0]!.endAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
|
expect(entries[1]!.startAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
|
expect(entries[1]!.endAt).toBe('2026-05-04T00:00:00.000Z');
|
||||||
|
expect(entries[0]!.granularity).toBe('day');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('availabilityModeForEntry', () => {
|
||||||
|
it('reads availabilityMode from metadata', () => {
|
||||||
|
expect(
|
||||||
|
availabilityModeForEntry({
|
||||||
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'offline' },
|
||||||
|
}),
|
||||||
|
).toBe('offline');
|
||||||
|
expect(
|
||||||
|
availabilityModeForEntry({
|
||||||
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'do-not-disturb' },
|
||||||
|
}),
|
||||||
|
).toBe('do-not-disturb');
|
||||||
|
expect(
|
||||||
|
availabilityModeForEntry({
|
||||||
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'online' },
|
||||||
|
}),
|
||||||
|
).toBe('online');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to online when metadata is missing or invalid', () => {
|
||||||
|
expect(
|
||||||
|
availabilityModeForEntry({
|
||||||
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'work', title: '', granularity: 'day', metadata: {},
|
||||||
|
}),
|
||||||
|
).toBe('online');
|
||||||
|
expect(
|
||||||
|
availabilityModeForEntry({
|
||||||
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'sleep', title: '', granularity: 'day', metadata: { availabilityMode: 'invalid' },
|
||||||
|
}),
|
||||||
|
).toBe('online');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('buildAvailabilitySnapshot', () => {
|
||||||
|
it('filters, sorts, and merges contiguous ranges with the same mode', () => {
|
||||||
|
const entries = [
|
||||||
|
{
|
||||||
|
id: 'e1', spaceId: 's', startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-01T06:00:00.000Z',
|
||||||
|
activity: 'sleep', title: 'Sleep', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'offline' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e2', spaceId: 's', startAt: '2026-05-01T06:00:00.000Z', endAt: '2026-05-01T12:00:00.000Z',
|
||||||
|
activity: 'work', title: 'Work', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e3', spaceId: 's', startAt: '2026-05-01T12:00:00.000Z', endAt: '2026-05-01T18:00:00.000Z',
|
||||||
|
activity: 'work', title: 'Work', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e4', spaceId: 's', startAt: '2026-05-01T18:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z',
|
||||||
|
activity: 'rest', title: 'Rest', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'online' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const snapshot = buildAvailabilitySnapshot({ now: '2026-05-01T12:00:00.000Z', entries });
|
||||||
|
|
||||||
|
expect(snapshot.windowStartAt).toBe('2026-05-01T00:00:00.000Z');
|
||||||
|
expect(snapshot.windowEndAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
|
expect(snapshot.ranges).toHaveLength(3);
|
||||||
|
expect(snapshot.ranges[0]).toMatchObject({ startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-01T06:00:00.000Z', mode: 'offline' });
|
||||||
|
expect(snapshot.ranges[1]).toMatchObject({ startAt: '2026-05-01T06:00:00.000Z', endAt: '2026-05-01T18:00:00.000Z', mode: 'do-not-disturb' });
|
||||||
|
expect(snapshot.ranges[2]).toMatchObject({ startAt: '2026-05-01T18:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z', mode: 'online' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludes entries outside the 2-day window', () => {
|
||||||
|
const entries = [
|
||||||
|
{
|
||||||
|
id: 'e1', spaceId: 's', startAt: '2026-04-30T00:00:00.000Z', endAt: '2026-04-30T23:59:59.000Z',
|
||||||
|
activity: 'sleep', title: 'Sleep', granularity: 'day' as const, metadata: { availabilityMode: 'offline' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e2', spaceId: 's', startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z',
|
||||||
|
activity: 'work', title: 'Work', granularity: 'day' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const snapshot = buildAvailabilitySnapshot({ now: '2026-05-01T12:00:00.000Z', entries });
|
||||||
|
expect(snapshot.ranges).toHaveLength(1);
|
||||||
|
expect(snapshot.ranges[0]!.startAt).toBe('2026-05-01T00:00:00.000Z');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('dateKeysAround', () => {
|
||||||
|
it('returns yesterday, today, and tomorrow as YYYY-MM-DD strings', () => {
|
||||||
|
const keys = dateKeysAround('2026-05-01T12:00:00.000Z');
|
||||||
|
expect(keys).toEqual(['2026-04-30', '2026-05-01', '2026-05-02']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles month boundaries correctly', () => {
|
||||||
|
const keys = dateKeysAround('2026-03-01T00:00:00.000Z');
|
||||||
|
expect(keys).toEqual(['2026-02-28', '2026-03-01', '2026-03-02']);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -4,25 +4,63 @@ import { InMemoryMemoryStore, Persona } from '../src';
|
|||||||
describe('Persona schedules and availability', () => {
|
describe('Persona schedules and availability', () => {
|
||||||
it('creates tomorrow as a ten-minute daily schedule and persists it in memory', async () => {
|
it('creates tomorrow as a ten-minute daily schedule and persists it in memory', async () => {
|
||||||
const memory = new InMemoryMemoryStore();
|
const memory = new InMemoryMemoryStore();
|
||||||
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', { memory, now: '2026-05-01T10:00:00.000Z' });
|
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', {
|
||||||
|
memory,
|
||||||
|
now: '2026-05-01T10:00:00.000Z',
|
||||||
|
models: {
|
||||||
|
schedule: {
|
||||||
|
async generateDailySchedule() {
|
||||||
|
return [
|
||||||
|
{ startTime: '00:00', endTime: '07:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
||||||
|
{ startTime: '07:00', endTime: '09:00', activity: 'morning routine', title: 'Morning routine', availabilityMode: 'online' as const },
|
||||||
|
{ startTime: '09:00', endTime: '18:00', activity: 'deep work', title: 'Deep work', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
{ startTime: '18:00', endTime: '24:00', activity: 'free time', title: 'Free time', availabilityMode: 'online' as const },
|
||||||
|
];
|
||||||
|
},
|
||||||
|
async generateMonthlySchedule() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
const space = await persona.ready();
|
const space = await persona.ready();
|
||||||
|
|
||||||
const entries = await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
const entries = await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
||||||
|
|
||||||
expect(entries).toHaveLength(144);
|
expect(entries).toHaveLength(4);
|
||||||
expect(entries[0]).toMatchObject({
|
expect(entries[0]).toMatchObject({
|
||||||
spaceId: space.id,
|
spaceId: space.id,
|
||||||
startAt: '2026-05-02T00:00:00.000Z',
|
startAt: '2026-05-02T00:00:00.000Z',
|
||||||
endAt: '2026-05-02T00:10:00.000Z',
|
endAt: '2026-05-02T07:00:00.000Z',
|
||||||
granularity: 'ten-minute',
|
granularity: 'ten-minute',
|
||||||
|
activity: 'sleep',
|
||||||
});
|
});
|
||||||
expect(entries.at(-1)?.endAt).toBe('2026-05-03T00:00:00.000Z');
|
expect(entries.at(-1)?.endAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
await expect(memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z')).resolves.toHaveLength(144);
|
await expect(
|
||||||
|
memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z'),
|
||||||
|
).resolves.toHaveLength(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('derives online, do-not-disturb, and offline availability from the in-memory schedule window', async () => {
|
it('derives online, do-not-disturb, and offline availability from the in-memory schedule window', async () => {
|
||||||
const memory = new InMemoryMemoryStore();
|
const memory = new InMemoryMemoryStore();
|
||||||
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', { memory, now: '2026-05-01T10:00:00.000Z' });
|
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', {
|
||||||
|
memory,
|
||||||
|
now: '2026-05-01T10:00:00.000Z',
|
||||||
|
models: {
|
||||||
|
schedule: {
|
||||||
|
async generateDailySchedule() {
|
||||||
|
return [
|
||||||
|
{ startTime: '00:00', endTime: '07:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
||||||
|
{ startTime: '07:00', endTime: '09:00', activity: 'morning routine', title: 'Morning routine', availabilityMode: 'online' as const },
|
||||||
|
{ startTime: '09:00', endTime: '18:00', activity: 'deep work', title: 'Deep work', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
];
|
||||||
|
},
|
||||||
|
async generateMonthlySchedule() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
await persona.ready();
|
await persona.ready();
|
||||||
|
|
||||||
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
||||||
@@ -30,21 +68,46 @@ describe('Persona schedules and availability', () => {
|
|||||||
|
|
||||||
expect(availability.windowStartAt).toBe('2026-05-01T00:00:00.000Z');
|
expect(availability.windowStartAt).toBe('2026-05-01T00:00:00.000Z');
|
||||||
expect(availability.windowEndAt).toBe('2026-05-03T00:00:00.000Z');
|
expect(availability.windowEndAt).toBe('2026-05-03T00:00:00.000Z');
|
||||||
expect(new Set(availability.ranges.map((range) => range.mode))).toEqual(new Set(['offline', 'online', 'do-not-disturb']));
|
expect(new Set(availability.ranges.map((range) => range.mode))).toEqual(
|
||||||
expect(availability.ranges.find((range) => range.mode === 'offline')?.startAt).toBe('2026-05-02T00:00:00.000Z');
|
new Set(['offline', 'online', 'do-not-disturb']),
|
||||||
|
);
|
||||||
|
expect(availability.ranges.find((range) => range.mode === 'offline')?.startAt).toBe(
|
||||||
|
'2026-05-02T00:00:00.000Z',
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('prunes schedule entries before a caller-provided cutoff', async () => {
|
it('prunes schedule entries before a caller-provided cutoff', async () => {
|
||||||
const memory = new InMemoryMemoryStore();
|
const memory = new InMemoryMemoryStore();
|
||||||
const persona = new Persona('Mina', 'Mina works weekdays.', { memory, now: '2026-05-01T10:00:00.000Z' });
|
const persona = new Persona('Mina', 'Mina works weekdays.', {
|
||||||
|
memory,
|
||||||
|
now: '2026-05-01T10:00:00.000Z',
|
||||||
|
models: {
|
||||||
|
schedule: {
|
||||||
|
async generateDailySchedule() {
|
||||||
|
return [
|
||||||
|
{ startTime: '00:00', endTime: '06:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
||||||
|
{ startTime: '06:00', endTime: '12:00', activity: 'work', title: 'Work', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
{ startTime: '12:00', endTime: '18:00', activity: 'study', title: 'Study', availabilityMode: 'do-not-disturb' as const },
|
||||||
|
{ startTime: '18:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
||||||
|
];
|
||||||
|
},
|
||||||
|
async generateMonthlySchedule() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
const space = await persona.ready();
|
const space = await persona.ready();
|
||||||
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
||||||
|
|
||||||
const deleted = await persona.deleteSchedulesBefore('2026-05-02T12:00:00.000Z');
|
const deleted = await persona.deleteSchedulesBefore('2026-05-02T12:00:00.000Z');
|
||||||
|
|
||||||
expect(deleted).toBe(72);
|
expect(deleted).toBe(2);
|
||||||
await expect(memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z')).resolves.toHaveLength(72);
|
await expect(
|
||||||
|
memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z'),
|
||||||
|
).resolves.toHaveLength(2);
|
||||||
const deletionFacts = await memory.findFacts(space.id, ['persona.schedule.deleted']);
|
const deletionFacts = await memory.findFacts(space.id, ['persona.schedule.deleted']);
|
||||||
expect(deletionFacts[0]?.metadata?.['deleted']).toBe(72);
|
expect(deletionFacts[0]?.metadata?.['deleted']).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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,
|
||||||
},
|
}];
|
||||||
];
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user