File size: 5,646 Bytes
68d7816 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import { assign, fromCallback, setup } from 'xstate';
import { createDecorator, IInstantiationService } from '#/_base/di/instantiation';
import {
AgentActorService,
type AgentActorContext,
type AgentActorRestoreEvent,
} from '#/agent/actorService/agentActorService';
import { IAgentProfileService } from '#/agent/profile/profile';
import { IAgentScopeContext } from '#/agent/scopeContext/scopeContext';
import { IAgentReminderService } from '#/features/reminder/reminderService';
import type {
ContextInjectionContext,
ContextInjectionResult,
} from '#/features/reminder/types';
import { IHostClock } from '#/os/interface/hostClock';
import { ISessionContext } from '#/session/sessionContext/sessionContext';
import { IEventDispatcher } from '#/state/eventDispatcher';
import type { DateInjectionDisclosure } from './dateChange';
import { pickDisclosureBaseline } from './disclosureBaseline';
const DATE_CHANGE_INJECTION_VARIANT = 'date_change';
interface DateDisclosure {
readonly localDate: string;
readonly timeZone: string;
readonly renderGeneration: number;
}
interface DateChangeActorContext {
readonly seed: DateDisclosure | undefined;
readonly runtime: AgentActorContext<null>;
}
interface DateChangeDiscloseEvent {
readonly type: 'dateChange.disclose';
readonly seed: DateDisclosure;
}
function currentDateDisclosure(clock: IHostClock): Omit<DateDisclosure, 'renderGeneration'> {
const date = clock.now();
const timeZone = clock.timeZone();
const parts = new Intl.DateTimeFormat('en-US', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).formatToParts(date);
const part = (type: Intl.DateTimeFormatPartTypes): string =>
parts.find((candidate) => candidate.type === type)?.value ?? '';
return {
localDate: `${part('year')}-${part('month')}-${part('day')}`,
timeZone,
};
}
const dateChangeInjection = fromCallback(({
input,
}: {
input: {
readonly runtime: AgentActorContext<null>;
};
}) => {
const runtime = input.runtime;
const reminder = runtime.get(IAgentReminderService);
const profile = runtime.get(IAgentProfileService);
const clock = runtime.get(IHostClock);
const sessionContext = runtime.get(ISessionContext);
const belongsToCurrentCwd = (): boolean => {
const environment = profile.data().environmentDisclosure;
return !(
environment !== undefined &&
environment.cwd !== '' &&
environment.cwd !== sessionContext.cwd
);
};
const registration = reminder.register<DateInjectionDisclosure>(
DATE_CHANGE_INJECTION_VARIANT,
({
lastDisclosure,
}: ContextInjectionContext<DateInjectionDisclosure>): ContextInjectionResult<DateInjectionDisclosure> | undefined => {
const profileData = profile.data();
if (!belongsToCurrentCwd()) return undefined;
const renderGeneration = profileData.renderGeneration ?? 0;
const current = currentDateDisclosure(clock);
const seed = runtime.getLogicState<DateChangeActorContext>().seed;
const baseline = pickDisclosureBaseline<DateDisclosure>(lastDisclosure, seed);
if (baseline !== undefined && baseline.localDate !== current.localDate) {
return {
content: `The date has changed. Today's date is now ${current.localDate}. Rely on this reminder over any earlier date statement for the current date. DO NOT mention this to the user explicitly.`,
disclosure: {
kind: 'date',
renderGeneration,
localDate: current.localDate,
timeZone: current.timeZone,
},
};
}
if (lastDisclosure !== undefined) return undefined;
if (seed === undefined) {
runtime.send({
type: 'dateChange.disclose',
seed: { ...current, renderGeneration },
});
}
return {
content: `Today's date is ${current.localDate}. The current date is restated in a reminder whenever it changes; rely on the latest such reminder for the current date. DO NOT mention this to the user explicitly.`,
disclosure: {
kind: 'date',
renderGeneration,
localDate: current.localDate,
timeZone: current.timeZone,
},
};
},
);
return () => { registration.dispose(); };
});
const dateChangeActorLogic = setup({
types: {} as {
context: DateChangeActorContext;
input: AgentActorContext<null>;
events: DateChangeDiscloseEvent | AgentActorRestoreEvent;
},
actors: { dateChangeInjection },
}).createMachine({
context: ({ input }) => ({ seed: undefined, runtime: input }),
initial: 'beforeRestore',
states: {
beforeRestore: {
on: { 'runtime.restore': 'active' },
},
active: {
invoke: {
src: 'dateChangeInjection',
input: ({ context }) => ({ runtime: context.runtime }),
},
},
},
on: {
'dateChange.disclose': {
actions: assign({ seed: ({ event }) => event.seed }),
},
},
});
export interface IAgentDateChangeService {
readonly _serviceBrand: undefined;
}
export const IAgentDateChangeService = createDecorator<IAgentDateChangeService>('agentDateChangeService');
export class AgentDateChangeService extends AgentActorService<null> implements IAgentDateChangeService {
declare readonly _serviceBrand: undefined;
constructor(
@IEventDispatcher dispatcher: IEventDispatcher,
@IAgentScopeContext scopeContext: IAgentScopeContext,
@IInstantiationService instantiation: IInstantiationService,
) {
super(dispatcher, scopeContext, instantiation);
this.attachActor(dateChangeActorLogic, { id: 'dateChange' });
}
}
|