feat: LLM-based schedule generation
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { InMemoryMemoryStore } from './memory';
|
||||
import {
|
||||
addUtcDays,
|
||||
blocksToDailySchedule,
|
||||
blocksToMonthlySchedule,
|
||||
buildAvailabilitySnapshot,
|
||||
createMonthlyScheduleEntries,
|
||||
createTenMinuteDailySchedule,
|
||||
daysInMonth,
|
||||
scheduleInstruction,
|
||||
scheduleTargetDay,
|
||||
startOfUtcDay,
|
||||
toIso,
|
||||
@@ -84,8 +86,17 @@ export class Persona {
|
||||
|
||||
async createDailySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> {
|
||||
const persona = await this.ready();
|
||||
if (!this.options.models?.schedule) {
|
||||
throw new Error('createDailySchedule requires options.models.schedule.');
|
||||
}
|
||||
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.memory.saveScheduleEntries(persona.id, entries);
|
||||
await this.refreshAvailability(datetime);
|
||||
@@ -94,7 +105,19 @@ export class Persona {
|
||||
|
||||
async createMonthlySchedule(datetime: DateTimeInput, message: string): Promise<ScheduleEntry[]> {
|
||||
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.memory.saveScheduleEntries(persona.id, entries);
|
||||
await this.refreshAvailability(datetime);
|
||||
|
||||
135
src/schedule.ts
135
src/schedule.ts
@@ -3,12 +3,11 @@ import type {
|
||||
AvailabilityRange,
|
||||
DateTimeInput,
|
||||
MemorySpace,
|
||||
ScheduleActivity,
|
||||
ScheduleBlock,
|
||||
ScheduleEntry,
|
||||
ScheduledAvailabilitySnapshot,
|
||||
} from './types';
|
||||
|
||||
const TEN_MINUTES_MS = 10 * 60 * 1000;
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
export function toDate(input: DateTimeInput): Date {
|
||||
@@ -36,135 +35,87 @@ export function scheduleTargetDay(now: DateTimeInput): Date {
|
||||
return addUtcDays(now, 1);
|
||||
}
|
||||
|
||||
function pick(activity: ScheduleActivity): { title: string; mode: AvailabilityMode } {
|
||||
switch (activity) {
|
||||
case 'sleep':
|
||||
return { title: 'Sleep', mode: 'offline' };
|
||||
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' };
|
||||
}
|
||||
export function daysInMonth(date: Date): number {
|
||||
const year = date.getUTCFullYear();
|
||||
const month = date.getUTCMonth();
|
||||
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
|
||||
}
|
||||
|
||||
function chooseDaytimeActivity(message: string): ScheduleActivity {
|
||||
const lower = message.toLowerCase();
|
||||
if (lower.includes('travel') || lower.includes('trip') || lower.includes('여행')) return 'travel';
|
||||
if (lower.includes('study') || lower.includes('exam') || lower.includes('공부') || lower.includes('시험')) return 'study';
|
||||
if (lower.includes('job') || lower.includes('취업') || lower.includes('구직')) return 'job-search';
|
||||
if (lower.includes('work') || lower.includes('일') || lower.includes('회사')) return 'work';
|
||||
return 'work';
|
||||
export function scheduleInstruction(): string {
|
||||
return [
|
||||
'Generate realistic schedule blocks for the persona based on their profile and the provided message.',
|
||||
'Activity types should be creative and context-appropriate; do not limit yourself to a fixed list.',
|
||||
'For each block, choose an availability mode: online (available to chat), do-not-disturb (busy but reachable for urgent matters), or offline (completely unavailable).',
|
||||
'Return blocks covering the requested time range with startTime and endTime in HH:MM 24-hour format.',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function activityForMinute(minuteOfDay: number, message: string): ScheduleActivity {
|
||||
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: {
|
||||
export function blocksToDailySchedule(input: {
|
||||
persona: MemorySpace;
|
||||
targetDay: DateTimeInput;
|
||||
message: string;
|
||||
blocks: ScheduleBlock[];
|
||||
}): ScheduleEntry[] {
|
||||
const target = startOfUtcDay(input.targetDay);
|
||||
const entries: ScheduleEntry[] = [];
|
||||
|
||||
for (let offset = 0; offset < DAY_MS; offset += TEN_MINUTES_MS) {
|
||||
const start = new Date(target.getTime() + offset);
|
||||
const end = new Date(start.getTime() + TEN_MINUTES_MS);
|
||||
const minute = offset / (60 * 1000);
|
||||
const activity = activityForMinute(minute, input.message);
|
||||
const picked = pick(activity);
|
||||
entries.push({
|
||||
return input.blocks.map((block) => {
|
||||
const [startHour, startMinute] = block.startTime.split(':').map(Number) as [number, number];
|
||||
const [endHour, endMinute] = block.endTime.split(':').map(Number) as [number, number];
|
||||
const start = new Date(target.getTime() + ((startHour * 60 + startMinute) * 60 * 1000));
|
||||
const end = new Date(target.getTime() + ((endHour * 60 + endMinute) * 60 * 1000));
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
spaceId: input.persona.id,
|
||||
startAt: start.toISOString(),
|
||||
endAt: end.toISOString(),
|
||||
activity,
|
||||
title: picked.title,
|
||||
description: `Realistic ${picked.title.toLowerCase()} block for ${input.persona.displayName}.`,
|
||||
activity: block.activity,
|
||||
title: block.title,
|
||||
description: block.description ?? `Realistic ${block.title.toLowerCase()} block for ${input.persona.displayName}.`,
|
||||
granularity: 'ten-minute',
|
||||
sourceMessage: input.message,
|
||||
metadata: {
|
||||
boxbrainType: 'schedule-entry',
|
||||
availabilityMode: picked.mode,
|
||||
availabilityMode: block.availabilityMode,
|
||||
targetDate: target.toISOString().slice(0, 10),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function createMonthlyScheduleEntries(input: {
|
||||
export function blocksToMonthlySchedule(input: {
|
||||
persona: MemorySpace;
|
||||
fromDay: DateTimeInput;
|
||||
message: string;
|
||||
days?: number;
|
||||
blocks: ScheduleBlock[];
|
||||
}): ScheduleEntry[] {
|
||||
const start = scheduleTargetDay(input.fromDay);
|
||||
const count = input.days ?? 30;
|
||||
const entries: ScheduleEntry[] = [];
|
||||
for (let day = 0; day < count; day += 1) {
|
||||
const start = startOfUtcDay(input.fromDay);
|
||||
return input.blocks.map((block, day) => {
|
||||
const dayStart = new Date(start.getTime() + day * DAY_MS);
|
||||
const travelHint = day > 0 && day % 90 === 0 ? ' travel' : '';
|
||||
const activity = chooseDaytimeActivity(`${input.message}${travelHint}`);
|
||||
const picked = pick(activity);
|
||||
entries.push({
|
||||
const [startHour, startMinute] = block.startTime.split(':').map(Number) as [number, number];
|
||||
const [endHour, endMinute] = block.endTime.split(':').map(Number) as [number, number];
|
||||
const entryStart = new Date(dayStart.getTime() + ((startHour * 60 + startMinute) * 60 * 1000));
|
||||
const entryEnd = new Date(dayStart.getTime() + ((endHour * 60 + endMinute) * 60 * 1000));
|
||||
return {
|
||||
id: crypto.randomUUID(),
|
||||
spaceId: input.persona.id,
|
||||
startAt: dayStart.toISOString(),
|
||||
endAt: new Date(dayStart.getTime() + DAY_MS).toISOString(),
|
||||
activity,
|
||||
title: picked.title,
|
||||
description: `Daily outline for ${input.persona.displayName}.`,
|
||||
startAt: entryStart.toISOString(),
|
||||
endAt: entryEnd.toISOString(),
|
||||
activity: block.activity,
|
||||
title: block.title,
|
||||
description: block.description ?? `Daily outline for ${input.persona.displayName}.`,
|
||||
granularity: 'day',
|
||||
sourceMessage: input.message,
|
||||
metadata: {
|
||||
boxbrainType: 'schedule-entry',
|
||||
availabilityMode: picked.mode,
|
||||
availabilityMode: block.availabilityMode,
|
||||
targetDate: dayStart.toISOString().slice(0, 10),
|
||||
},
|
||||
});
|
||||
}
|
||||
return entries;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function availabilityModeForEntry(entry: ScheduleEntry): AvailabilityMode {
|
||||
const mode = entry.metadata['availabilityMode'];
|
||||
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';
|
||||
}
|
||||
|
||||
|
||||
44
src/types.ts
44
src/types.ts
@@ -4,19 +4,7 @@ export type PersonaConstructorMode = 'create' | 'load';
|
||||
|
||||
export type ScheduleGranularity = 'day' | 'ten-minute';
|
||||
|
||||
export type ScheduleActivity =
|
||||
| 'sleep'
|
||||
| 'rest'
|
||||
| 'meal'
|
||||
| 'commute'
|
||||
| 'work'
|
||||
| 'study'
|
||||
| 'job-search'
|
||||
| 'travel'
|
||||
| 'exercise'
|
||||
| 'social'
|
||||
| 'errand'
|
||||
| 'free-time';
|
||||
export type ScheduleActivity = string;
|
||||
|
||||
export type AvailabilityMode = 'online' | 'do-not-disturb' | 'offline';
|
||||
|
||||
@@ -141,6 +129,35 @@ export interface MemoryExtractionModel {
|
||||
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 {
|
||||
extractInitialFacts(input: {
|
||||
displayName: string;
|
||||
@@ -154,6 +171,7 @@ export interface PersonaModels {
|
||||
conversation?: ConversationModel;
|
||||
rewrite?: RewriteModel;
|
||||
memoryExtraction?: MemoryExtractionModel;
|
||||
schedule?: ScheduleModel;
|
||||
}
|
||||
|
||||
export interface PersonaOptions {
|
||||
|
||||
Reference in New Issue
Block a user