|
| 1 | +import { assign, fromCallback, setup } from 'xstate'; |
| 2 | + |
| 3 | +import { IAgentProfileService } from '#/agent/profile/profile'; |
| 4 | +import { |
| 5 | + defineAgentRuntimeContract, |
| 6 | + defineAgentRuntimeProvider, |
| 7 | + type AgentRuntimeContext, |
| 8 | + type AgentRuntimeRestoreEvent, |
| 9 | +} from '#/agent/runtime/agentRuntime'; |
| 10 | +import { AgentReminder } from '#/features/reminder/reminderAgentRuntime'; |
| 11 | +import type { |
| 12 | + ContextInjectionContext, |
| 13 | + ContextInjectionResult, |
| 14 | +} from '#/features/reminder/types'; |
| 15 | +import { IHostClock } from '#/os/interface/hostClock'; |
| 16 | +import { IAgentLifecycleService } from '#/session/agentLifecycle/agentLifecycle'; |
| 17 | +import { ISessionContext } from '#/session/sessionContext/sessionContext'; |
| 18 | + |
| 19 | +import type { DateInjectionDisclosure } from './dateChange'; |
| 20 | +import { pickDisclosureBaseline } from './disclosureBaseline'; |
| 21 | + |
| 22 | +const DATE_CHANGE_INJECTION_VARIANT = 'date_change'; |
| 23 | + |
| 24 | +interface DateDisclosure { |
| 25 | + readonly localDate: string; |
| 26 | + readonly timeZone: string; |
| 27 | + readonly renderGeneration: number; |
| 28 | +} |
| 29 | + |
| 30 | +interface DateChangeActorContext { |
| 31 | + readonly seed: DateDisclosure | undefined; |
| 32 | + readonly runtime: AgentRuntimeContext<null>; |
| 33 | +} |
| 34 | + |
| 35 | +interface DateChangeDiscloseEvent { |
| 36 | + readonly type: 'dateChange.disclose'; |
| 37 | + readonly seed: DateDisclosure; |
| 38 | +} |
| 39 | + |
| 40 | +function currentDateDisclosure(clock: IHostClock): Omit<DateDisclosure, 'renderGeneration'> { |
| 41 | + const date = clock.now(); |
| 42 | + const timeZone = clock.timeZone(); |
| 43 | + const parts = new Intl.DateTimeFormat('en-US', { |
| 44 | + timeZone, |
| 45 | + year: 'numeric', |
| 46 | + month: '2-digit', |
| 47 | + day: '2-digit', |
| 48 | + }).formatToParts(date); |
| 49 | + const part = (type: Intl.DateTimeFormatPartTypes): string => |
| 50 | + parts.find((candidate) => candidate.type === type)?.value ?? ''; |
| 51 | + return { |
| 52 | + localDate: `${part('year')}-${part('month')}-${part('day')}`, |
| 53 | + timeZone, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +const dateChangeInjection = fromCallback(({ |
| 58 | + input, |
| 59 | +}: { |
| 60 | + input: { |
| 61 | + readonly runtime: AgentRuntimeContext<null>; |
| 62 | + }; |
| 63 | +}) => { |
| 64 | + const runtime = input.runtime; |
| 65 | + const reminder = runtime |
| 66 | + .get(IAgentLifecycleService) |
| 67 | + .resolve(runtime.agent, AgentReminder); |
| 68 | + const profile = runtime.get(IAgentProfileService); |
| 69 | + const clock = runtime.get(IHostClock); |
| 70 | + const sessionContext = runtime.get(ISessionContext); |
| 71 | + const belongsToCurrentCwd = (): boolean => { |
| 72 | + const environment = profile.data().environmentDisclosure; |
| 73 | + return !( |
| 74 | + environment !== undefined && |
| 75 | + environment.cwd !== '' && |
| 76 | + environment.cwd !== sessionContext.cwd |
| 77 | + ); |
| 78 | + }; |
| 79 | + const dateFromProfile = (): DateDisclosure | undefined => { |
| 80 | + if (!belongsToCurrentCwd()) return undefined; |
| 81 | + const profileData = profile.data(); |
| 82 | + const date = profileData.environmentDisclosure?.date; |
| 83 | + if (!date?.disclosed) return undefined; |
| 84 | + return { |
| 85 | + ...date.value, |
| 86 | + renderGeneration: profileData.renderGeneration ?? 0, |
| 87 | + }; |
| 88 | + }; |
| 89 | + const registration = reminder.register<DateInjectionDisclosure>( |
| 90 | + DATE_CHANGE_INJECTION_VARIANT, |
| 91 | + ({ |
| 92 | + lastDisclosure, |
| 93 | + }: ContextInjectionContext<DateInjectionDisclosure>): ContextInjectionResult<DateInjectionDisclosure> | undefined => { |
| 94 | + const profileData = profile.data(); |
| 95 | + if (!belongsToCurrentCwd()) return undefined; |
| 96 | + const renderGeneration = profileData.renderGeneration ?? 0; |
| 97 | + const current = currentDateDisclosure(clock); |
| 98 | + const profileDate = dateFromProfile(); |
| 99 | + const seed = runtime.getLogicState<DateChangeActorContext>().seed; |
| 100 | + const baseline = pickDisclosureBaseline<DateDisclosure>( |
| 101 | + lastDisclosure, |
| 102 | + profileDate, |
| 103 | + seed, |
| 104 | + ); |
| 105 | + if (baseline !== undefined && baseline.localDate !== current.localDate) { |
| 106 | + return { |
| 107 | + 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.`, |
| 108 | + disclosure: { |
| 109 | + kind: 'date', |
| 110 | + renderGeneration, |
| 111 | + localDate: current.localDate, |
| 112 | + timeZone: current.timeZone, |
| 113 | + }, |
| 114 | + }; |
| 115 | + } |
| 116 | + if (lastDisclosure !== undefined || profileDate !== undefined) return undefined; |
| 117 | + if (seed === undefined) { |
| 118 | + runtime.send({ |
| 119 | + type: 'dateChange.disclose', |
| 120 | + seed: { ...current, renderGeneration }, |
| 121 | + }); |
| 122 | + } |
| 123 | + return { |
| 124 | + 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.`, |
| 125 | + disclosure: { |
| 126 | + kind: 'date', |
| 127 | + renderGeneration, |
| 128 | + localDate: current.localDate, |
| 129 | + timeZone: current.timeZone, |
| 130 | + }, |
| 131 | + }; |
| 132 | + }, |
| 133 | + ); |
| 134 | + return () => { registration.dispose(); }; |
| 135 | +}); |
| 136 | + |
| 137 | +const dateChangeActorLogic = setup({ |
| 138 | + types: {} as { |
| 139 | + context: DateChangeActorContext; |
| 140 | + input: AgentRuntimeContext<null>; |
| 141 | + events: DateChangeDiscloseEvent | AgentRuntimeRestoreEvent; |
| 142 | + }, |
| 143 | + actors: { dateChangeInjection }, |
| 144 | +}).createMachine({ |
| 145 | + context: ({ input }) => ({ seed: undefined, runtime: input }), |
| 146 | + initial: 'beforeRestore', |
| 147 | + states: { |
| 148 | + beforeRestore: { |
| 149 | + on: { 'runtime.restore': 'active' }, |
| 150 | + }, |
| 151 | + active: { |
| 152 | + invoke: { |
| 153 | + src: 'dateChangeInjection', |
| 154 | + input: ({ context }) => ({ runtime: context.runtime }), |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + on: { |
| 159 | + 'dateChange.disclose': { |
| 160 | + actions: assign({ seed: ({ event }) => event.seed }), |
| 161 | + }, |
| 162 | + }, |
| 163 | +}); |
| 164 | + |
| 165 | +export class DateChangeRuntime {} |
| 166 | + |
| 167 | +export const AgentDateChange = defineAgentRuntimeContract<DateChangeRuntime>('dateChange'); |
| 168 | + |
| 169 | +export const dateChangeAgentRuntimeProvider = defineAgentRuntimeProvider<null, DateChangeRuntime>( |
| 170 | + AgentDateChange, |
| 171 | + { |
| 172 | + id: 'dateChange', |
| 173 | + logic: dateChangeActorLogic, |
| 174 | + eager: true, |
| 175 | + createApi: () => new DateChangeRuntime(), |
| 176 | + }, |
| 177 | +); |
0 commit comments