File size: 2,462 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
/* oxlint-disable typescript-eslint/no-unsafe-declaration-merging, eslint-plugin-import/namespace -- Event2 class+payload-interface declaration merging is the sanctioned event-declaration idiom. */
import { z } from 'zod';

import { AgentStatusUpdated } from '#/agent/usage/usageEvents';
import { AgentEvent2, Event2 } from '#/app/event/event2';
import { defineState } from '#/state/state';

const towerModeEnterSchema = z.object({
  agentId: z.string(),
  sessionId: z.string().optional(),
  base: z.string().optional(),
});

export class TowerModeEnter extends AgentEvent2<z.infer<typeof towerModeEnterSchema>> {
  static override readonly type = 'tower_mode.enter';
  static override readonly durable = true;
  static override readonly schema = towerModeEnterSchema;
}
export interface TowerModeEnter {
  readonly agentId: string;
  readonly sessionId?: string;
  readonly base?: string;
}

const towerModeExitSchema = z.object({ agentId: z.string() });

export class TowerModeExit extends AgentEvent2<z.infer<typeof towerModeExitSchema>> {
  static override readonly type = 'tower_mode.exit';
  static override readonly durable = true;
  static override readonly schema = towerModeExitSchema;
}
export interface TowerModeExit {
  readonly agentId: string;
}

export interface TowerInboxSentPayload {
  readonly from: string;
  readonly to: string;
  readonly subject: string;
}

export class TowerInboxSent extends Event2<TowerInboxSentPayload> {
  static override readonly type = 'tower.inbox.sent';
  static override readonly observable = true;
}
export interface TowerInboxSent extends TowerInboxSentPayload {}

export const towerKey = defineState('tower', () => false).replayable({
  schema: z.boolean(),
})
  .on(TowerModeEnter, (_s, e, ctx) => {
    ctx.emit(new AgentStatusUpdated({ agentId: e.agentId, towerMode: true }));
    return true;
  })
  .on(TowerModeExit, (_s, e, ctx) => {
    ctx.emit(new AgentStatusUpdated({ agentId: e.agentId, towerMode: false }));
    return false;
  });

export const towerOwnerKey = defineState('tower.owner', () => undefined as string | undefined)
  .replayable({
    schema: z.string().optional(),
  })
  .on(TowerModeEnter, (_s, e) => e.sessionId)
  .on(TowerModeExit, () => undefined);

export const towerBaseKey = defineState('tower.base', (): string | null => null)
  .replayable({
    schema: z.custom<string | null>(),
  })
  .on(TowerModeEnter, (_s, e) => e.base ?? null)
  .on(TowerModeExit, () => null);