File size: 5,105 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
/* 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 { AgentEvent2, type AgentDomainTrait } from '#/app/event/event2';
import { defineState } from '#/state/state';
import {
  ContextApplyCompaction,
  ContextClear,
  type ContextApplyCompactionPayload,
} from '#/agent/contextMemory/contextEvents';
import type { WireLineRange } from '#/wire/record';

import type { CompactionBeginData, CompactionResult, CompactionSource } from './types';

export type CompactionPhase = 'idle' | 'running' | 'cancelled' | 'completed';

export interface CompactionState {
  readonly phase: CompactionPhase;
}

const fullCompactionBeginSchema = z.object({
  agentId: z.string(),
  instruction: z.string().optional(),
  source: z.custom<CompactionSource>(),
});

export class FullCompactionBegin extends AgentEvent2<
  z.infer<typeof fullCompactionBeginSchema>
> {
  static override readonly type = 'full_compaction.begin';
  static override readonly durable = true;
  static override readonly schema = fullCompactionBeginSchema;
}
export interface FullCompactionBegin extends CompactionBeginData {
  readonly agentId: string;
}

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

export class FullCompactionCancel extends AgentEvent2<
  z.infer<typeof fullCompactionCancelSchema>
> {
  static override readonly type = 'full_compaction.cancel';
  static override readonly durable = true;
  static override readonly schema = fullCompactionCancelSchema;
}
export interface FullCompactionCancel {
  readonly agentId: string;
}

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

export class FullCompactionComplete extends AgentEvent2<
  z.infer<typeof fullCompactionCompleteSchema>
> {
  static override readonly type = 'full_compaction.complete';
  static override readonly durable = true;
  static override readonly schema = fullCompactionCompleteSchema;
}
export interface FullCompactionComplete {
  readonly agentId: string;
}

export interface CompactionStartedPayload {
  readonly agentId: string;
  readonly trigger: CompactionSource;
  readonly instruction?: string;
}

export class CompactionStarted extends AgentEvent2<CompactionStartedPayload> {
  static override readonly type = 'compaction.started';
  static override readonly observable = true;
}
export interface CompactionStarted extends CompactionStartedPayload {}

export interface CompactionBlockedPayload {
  readonly agentId: string;
  readonly turnId?: number;
}

export class CompactionBlocked extends AgentEvent2<CompactionBlockedPayload> {
  static override readonly type = 'compaction.blocked';
  static override readonly observable = true;
}
export interface CompactionBlocked extends CompactionBlockedPayload {}

export class CompactionCancelled extends AgentEvent2<AgentDomainTrait> {
  static override readonly type = 'compaction.cancelled';
  static override readonly observable = true;
}
export interface CompactionCancelled {
  readonly agentId: string;
}

export interface CompactionCompletedPayload {
  readonly agentId: string;
  readonly result: CompactionResult;
}

export class CompactionCompleted extends AgentEvent2<CompactionCompletedPayload> {
  static override readonly type = 'compaction.completed';
  static override readonly observable = true;
}
export interface CompactionCompleted extends CompactionCompletedPayload {}

export interface CompactionStartedEvent extends Omit<CompactionStartedPayload, 'agentId'> {
  readonly type: 'compaction.started';
}

export interface CompactionBlockedEvent extends Omit<CompactionBlockedPayload, 'agentId'> {
  readonly type: 'compaction.blocked';
}

export interface CompactionCancelledEvent {
  readonly type: 'compaction.cancelled';
}

export interface CompactionCompletedEvent extends Omit<CompactionCompletedPayload, 'agentId'> {
  readonly type: 'compaction.completed';
}

export const fullCompactionKey = defineState(
  'fullCompaction',
  (): CompactionState => ({ phase: 'idle' }),
).replayable({ schema: z.custom<CompactionState>() })
  .on(FullCompactionBegin, (s, e, ctx) => {
    if (s.phase !== 'running') {
      s.phase = 'running';
    }
    ctx.emit(
      new CompactionStarted({
        agentId: e.agentId,
        trigger: e.source,
        instruction: e.instruction,
      }),
    );
  })
  .on(FullCompactionCancel, (s) => {
    if (s.phase !== 'idle') {
      s.phase = 'idle';
    }
  })
  .on(FullCompactionComplete, (s) => {
    if (s.phase !== 'idle') {
      s.phase = 'idle';
    }
  });

export const fullCompactionWireRangesKey = defineState<readonly WireLineRange[]>(
  'fullCompaction.wireRanges',
  () => [],
)
  .replayable({ schema: z.custom<readonly WireLineRange[]>() })
  .on(ContextApplyCompaction, (s, e) => {
    const wireLines = (e as unknown as ContextApplyCompactionPayload).wireLines;
    return wireLines === undefined ? undefined : [...s, wireLines];
  })
  .on(ContextClear, (s) => (s.length === 0 ? undefined : []));