3 Commits

Author SHA1 Message Date
8bd6926a95 v0.3.0
All checks were successful
CI / verify (push) Successful in 10s
Publish / publish (push) Successful in 18s
2026-05-17 15:43:50 +09:00
bedbd01807 ci: add publish workflow 2026-05-17 15:42:48 +09:00
90214cec5c feat: LLM-based schedule generation 2026-05-17 15:40:13 +09:00
8 changed files with 463 additions and 121 deletions

View 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 }}

View File

@@ -1,6 +1,6 @@
{ {
"name": "boxbrain", "name": "boxbrain",
"version": "0.2.0", "version": "0.3.0",
"description": "Human-like persona harness framework powered by LLMs and IdentityDB.", "description": "Human-like persona harness framework powered by LLMs and IdentityDB.",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",

View File

@@ -1,9 +1,11 @@
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,
@@ -84,8 +86,17 @@ export class Persona {
async createDailySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> { 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({
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.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);
@@ -94,7 +105,19 @@ export class Persona {
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) {
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.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);

View File

@@ -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';
} }

View File

@@ -4,19 +4,7 @@ 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';
@@ -141,6 +129,35 @@ export interface MemoryExtractionModel {
extract(input: MemoryExtractionInput): Promise<FactDraft[]>; extract(input: MemoryExtractionInput): Promise<FactDraft[]>;
} }
export interface ScheduleBlock {
startTime: string;
endTime: string;
activity: string;
title: string;
description?: string;
availabilityMode: AvailabilityMode;
}
export interface DailyScheduleGenerationInput {
persona: MemorySpace;
targetDay: Date;
message: string;
instruction: string;
}
export interface MonthlyScheduleGenerationInput {
persona: MemorySpace;
fromDay: Date;
message: string;
days: number;
instruction: string;
}
export interface ScheduleModel {
generateDailySchedule(input: DailyScheduleGenerationInput): Promise<ScheduleBlock[]>;
generateMonthlySchedule(input: MonthlyScheduleGenerationInput): Promise<ScheduleBlock[]>;
}
export interface PersonaInitializationModel { export interface PersonaInitializationModel {
extractInitialFacts(input: { extractInitialFacts(input: {
displayName: string; displayName: string;
@@ -154,6 +171,7 @@ export interface PersonaModels {
conversation?: ConversationModel; conversation?: ConversationModel;
rewrite?: RewriteModel; rewrite?: RewriteModel;
memoryExtraction?: MemoryExtractionModel; memoryExtraction?: MemoryExtractionModel;
schedule?: ScheduleModel;
} }
export interface PersonaOptions { export interface PersonaOptions {

View File

@@ -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();

View 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']);
});
});

View File

@@ -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);
}); });
}); });