File size: 5,207 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
import { assign, fromCallback, setup, type Snapshot } from 'xstate';

import { createDecorator, IInstantiationService } from '#/_base/di/instantiation';
import type { Event } from '#/_base/event';
import { registerEvent2Class } from '#/app/event/event2';
import {
  AgentActorService,
  type AgentActorContext,
  type AgentActorRestoreEvent,
} from '#/agent/actorService/agentActorService';
import { IAgentContextMemoryService } from '#/agent/contextMemory/contextMemory';
import { IAgentScopeContext } from '#/agent/scopeContext/scopeContext';
import { IAgentToolPolicyService } from '#/agent/toolPolicy/toolPolicy';
import { IAgentReminderService } from '#/features/reminder/reminderService';
import { MAIN_AGENT_ID } from '#/session/agentLifecycle/agentLifecycle';
import { IEventDispatcher } from '#/state/eventDispatcher';

import { TODO_LIST_TOOL_NAME, readTodoItems, type TodoItem } from './todoItem';
import { TODO_LIST_REMINDER_VARIANT, todoListStaleReminder } from './todoListReminder';
import { ToolsUpdateStore, type TodoState } from './todoOps';

import '#/agent/contextMemory/conversationTime';

registerEvent2Class(ToolsUpdateStore);

interface TodoActorContext {
  readonly todos: TodoState;
  readonly runtime: AgentActorContext<TodoState>;
  readonly used: boolean;
}

interface TodoCommitEvent {
  readonly type: 'todo.commit';
  readonly todos: TodoState;
}

interface TodoUsedEvent {
  readonly type: 'todo.used';
}

type TodoActorSnapshot = Snapshot<unknown> & { readonly context: TodoActorContext };

const todoReminder = fromCallback(({
  input,
}: {
  input: {
    readonly runtime: AgentActorContext<TodoState>;
  };
}) => {
  if (input.runtime.agent.agentId !== MAIN_AGENT_ID) return;
  const injector = input.runtime.get(IAgentReminderService);
  const memory = input.runtime.get(IAgentContextMemoryService);
  const toolPolicy = input.runtime.get(IAgentToolPolicyService);
  const registration = injector.register(TODO_LIST_REMINDER_VARIANT, () =>
    todoListStaleReminder({
      active: toolPolicy.isToolActive(TODO_LIST_TOOL_NAME, 'builtin'),
      history: memory.get(),
      todos: input.runtime.getState(),
    }),
  );
  return () => { registration.dispose(); };
});

const todoActorLogic = setup({
  types: {} as {
    context: TodoActorContext;
    input: AgentActorContext<TodoState>;
    events: TodoCommitEvent | TodoUsedEvent | AgentActorRestoreEvent;
  },
  actors: { todoReminder },
}).createMachine({
  context: ({ input }) => ({ todos: [], runtime: input, used: false }),
  initial: 'beforeRestore',
  states: {
    beforeRestore: {
      on: {
        'runtime.restore': [
          { target: 'reminding', guard: ({ context }) => context.used },
          { target: 'active' },
        ],
        'todo.used': { actions: assign({ used: true }) },
      },
    },
    active: {
      on: {
        'todo.used': { target: 'reminding', actions: assign({ used: true }) },
      },
    },
    reminding: {
      invoke: {
        src: 'todoReminder',
        input: ({ context }) => ({ runtime: context.runtime }),
      },
    },
  },
  on: {
    'todo.commit': {
      actions: assign({ todos: ({ event }) => event.todos }),
    },
  },
});

export interface IAgentTodoService {
  readonly _serviceBrand: undefined;
  readonly onDidChange: Event<TodoState>;
  get(): readonly TodoItem[];
  replace(todos: readonly TodoItem[]): Promise<void>;
  clear(): Promise<void>;
}

export const IAgentTodoService = createDecorator<IAgentTodoService>('agentTodoService');

export class AgentTodoService extends AgentActorService<TodoState> implements IAgentTodoService {
  declare readonly _serviceBrand: undefined;
  readonly onDidChange: IAgentTodoService['onDidChange'];

  private readonly actor: AgentActorContext<TodoState>;

  constructor(
    @IEventDispatcher dispatcher: IEventDispatcher,
    @IAgentScopeContext scopeContext: IAgentScopeContext,
    @IInstantiationService instantiation: IInstantiationService,
  ) {
    super(dispatcher, scopeContext, instantiation);
    this.actor = this.attachActor(todoActorLogic, {
      id: 'todo',
      durable: {
        events: [ToolsUpdateStore],
        undoable: true,
        transition: (_state, event) => {
          if (!(event instanceof ToolsUpdateStore) || event.key !== 'todo') return;
          return readTodoItems(event.value);
        },
        read: (snapshot) => (snapshot as TodoActorSnapshot).context.todos,
        commit: (actor, todos) => { actor.send({ type: 'todo.commit', todos }); },
      },
    });
    this.onDidChange = this.actor.onDidChange;
  }

  get(): readonly TodoItem[] {
    this.actor.send({ type: 'todo.used' });
    return this.actor.getState();
  }

  replace(todos: readonly TodoItem[]): Promise<void> {
    this.actor.send({ type: 'todo.used' });
    return this.actor.dispatch(new ToolsUpdateStore({
      agentId: this.actor.agent.agentId,
      key: 'todo',
      value: todos.map((todo) => ({ title: todo.title, status: todo.status })),
    }));
  }

  clear(): Promise<void> {
    this.actor.send({ type: 'todo.used' });
    return this.actor.dispatch(new ToolsUpdateStore({
      agentId: this.actor.agent.agentId,
      key: 'todo',
      value: [],
    }));
  }
}