@principal-ai/principal-view-core 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +126 -0
- package/dist/ConfigurationLoader.d.ts +76 -0
- package/dist/ConfigurationLoader.d.ts.map +1 -0
- package/dist/ConfigurationLoader.js +144 -0
- package/dist/ConfigurationLoader.js.map +1 -0
- package/dist/ConfigurationValidator.d.ts +31 -0
- package/dist/ConfigurationValidator.d.ts.map +1 -0
- package/dist/ConfigurationValidator.js +242 -0
- package/dist/ConfigurationValidator.js.map +1 -0
- package/dist/EventProcessor.d.ts +49 -0
- package/dist/EventProcessor.d.ts.map +1 -0
- package/dist/EventProcessor.js +215 -0
- package/dist/EventProcessor.js.map +1 -0
- package/dist/EventRecorderService.d.ts +305 -0
- package/dist/EventRecorderService.d.ts.map +1 -0
- package/dist/EventRecorderService.js +463 -0
- package/dist/EventRecorderService.js.map +1 -0
- package/dist/LibraryLoader.d.ts +63 -0
- package/dist/LibraryLoader.d.ts.map +1 -0
- package/dist/LibraryLoader.js +188 -0
- package/dist/LibraryLoader.js.map +1 -0
- package/dist/PathBasedEventProcessor.d.ts +90 -0
- package/dist/PathBasedEventProcessor.d.ts.map +1 -0
- package/dist/PathBasedEventProcessor.js +239 -0
- package/dist/PathBasedEventProcessor.js.map +1 -0
- package/dist/SessionManager.d.ts +194 -0
- package/dist/SessionManager.d.ts.map +1 -0
- package/dist/SessionManager.js +299 -0
- package/dist/SessionManager.js.map +1 -0
- package/dist/ValidationEngine.d.ts +31 -0
- package/dist/ValidationEngine.d.ts.map +1 -0
- package/dist/ValidationEngine.js +158 -0
- package/dist/ValidationEngine.js.map +1 -0
- package/dist/helpers/GraphInstrumentationHelper.d.ts +93 -0
- package/dist/helpers/GraphInstrumentationHelper.d.ts.map +1 -0
- package/dist/helpers/GraphInstrumentationHelper.js +248 -0
- package/dist/helpers/GraphInstrumentationHelper.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/config.d.ts +57 -0
- package/dist/rules/config.d.ts.map +1 -0
- package/dist/rules/config.js +382 -0
- package/dist/rules/config.js.map +1 -0
- package/dist/rules/engine.d.ts +70 -0
- package/dist/rules/engine.d.ts.map +1 -0
- package/dist/rules/engine.js +252 -0
- package/dist/rules/engine.js.map +1 -0
- package/dist/rules/implementations/connection-type-references.d.ts +7 -0
- package/dist/rules/implementations/connection-type-references.d.ts.map +1 -0
- package/dist/rules/implementations/connection-type-references.js +104 -0
- package/dist/rules/implementations/connection-type-references.js.map +1 -0
- package/dist/rules/implementations/dead-end-states.d.ts +17 -0
- package/dist/rules/implementations/dead-end-states.d.ts.map +1 -0
- package/dist/rules/implementations/dead-end-states.js +72 -0
- package/dist/rules/implementations/dead-end-states.js.map +1 -0
- package/dist/rules/implementations/index.d.ts +24 -0
- package/dist/rules/implementations/index.d.ts.map +1 -0
- package/dist/rules/implementations/index.js +62 -0
- package/dist/rules/implementations/index.js.map +1 -0
- package/dist/rules/implementations/library-node-type-match.d.ts +17 -0
- package/dist/rules/implementations/library-node-type-match.d.ts.map +1 -0
- package/dist/rules/implementations/library-node-type-match.js +123 -0
- package/dist/rules/implementations/library-node-type-match.js.map +1 -0
- package/dist/rules/implementations/minimum-node-sources.d.ts +22 -0
- package/dist/rules/implementations/minimum-node-sources.d.ts.map +1 -0
- package/dist/rules/implementations/minimum-node-sources.js +54 -0
- package/dist/rules/implementations/minimum-node-sources.js.map +1 -0
- package/dist/rules/implementations/no-unknown-fields.d.ts +7 -0
- package/dist/rules/implementations/no-unknown-fields.d.ts.map +1 -0
- package/dist/rules/implementations/no-unknown-fields.js +211 -0
- package/dist/rules/implementations/no-unknown-fields.js.map +1 -0
- package/dist/rules/implementations/orphaned-edge-types.d.ts +7 -0
- package/dist/rules/implementations/orphaned-edge-types.d.ts.map +1 -0
- package/dist/rules/implementations/orphaned-edge-types.js +47 -0
- package/dist/rules/implementations/orphaned-edge-types.js.map +1 -0
- package/dist/rules/implementations/orphaned-node-types.d.ts +7 -0
- package/dist/rules/implementations/orphaned-node-types.d.ts.map +1 -0
- package/dist/rules/implementations/orphaned-node-types.js +50 -0
- package/dist/rules/implementations/orphaned-node-types.js.map +1 -0
- package/dist/rules/implementations/required-metadata.d.ts +7 -0
- package/dist/rules/implementations/required-metadata.d.ts.map +1 -0
- package/dist/rules/implementations/required-metadata.js +57 -0
- package/dist/rules/implementations/required-metadata.js.map +1 -0
- package/dist/rules/implementations/state-transition-references.d.ts +7 -0
- package/dist/rules/implementations/state-transition-references.d.ts.map +1 -0
- package/dist/rules/implementations/state-transition-references.js +135 -0
- package/dist/rules/implementations/state-transition-references.js.map +1 -0
- package/dist/rules/implementations/unreachable-states.d.ts +7 -0
- package/dist/rules/implementations/unreachable-states.d.ts.map +1 -0
- package/dist/rules/implementations/unreachable-states.js +80 -0
- package/dist/rules/implementations/unreachable-states.js.map +1 -0
- package/dist/rules/implementations/valid-action-patterns.d.ts +17 -0
- package/dist/rules/implementations/valid-action-patterns.d.ts.map +1 -0
- package/dist/rules/implementations/valid-action-patterns.js +109 -0
- package/dist/rules/implementations/valid-action-patterns.js.map +1 -0
- package/dist/rules/implementations/valid-color-format.d.ts +7 -0
- package/dist/rules/implementations/valid-color-format.d.ts.map +1 -0
- package/dist/rules/implementations/valid-color-format.js +91 -0
- package/dist/rules/implementations/valid-color-format.js.map +1 -0
- package/dist/rules/implementations/valid-edge-types.d.ts +7 -0
- package/dist/rules/implementations/valid-edge-types.d.ts.map +1 -0
- package/dist/rules/implementations/valid-edge-types.js +244 -0
- package/dist/rules/implementations/valid-edge-types.js.map +1 -0
- package/dist/rules/implementations/valid-node-types.d.ts +7 -0
- package/dist/rules/implementations/valid-node-types.d.ts.map +1 -0
- package/dist/rules/implementations/valid-node-types.js +175 -0
- package/dist/rules/implementations/valid-node-types.js.map +1 -0
- package/dist/rules/index.d.ts +28 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +45 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/types.d.ts +309 -0
- package/dist/rules/types.d.ts.map +1 -0
- package/dist/rules/types.js +35 -0
- package/dist/rules/types.js.map +1 -0
- package/dist/types/canvas.d.ts +409 -0
- package/dist/types/canvas.d.ts.map +1 -0
- package/dist/types/canvas.js +70 -0
- package/dist/types/canvas.js.map +1 -0
- package/dist/types/index.d.ts +311 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +13 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/library.d.ts +185 -0
- package/dist/types/library.d.ts.map +1 -0
- package/dist/types/library.js +15 -0
- package/dist/types/library.js.map +1 -0
- package/dist/types/path-based-config.d.ts +230 -0
- package/dist/types/path-based-config.d.ts.map +1 -0
- package/dist/types/path-based-config.js +9 -0
- package/dist/types/path-based-config.js.map +1 -0
- package/dist/utils/CanvasConverter.d.ts +118 -0
- package/dist/utils/CanvasConverter.d.ts.map +1 -0
- package/dist/utils/CanvasConverter.js +315 -0
- package/dist/utils/CanvasConverter.js.map +1 -0
- package/dist/utils/GraphConverter.d.ts +18 -0
- package/dist/utils/GraphConverter.d.ts.map +1 -0
- package/dist/utils/GraphConverter.js +61 -0
- package/dist/utils/GraphConverter.js.map +1 -0
- package/dist/utils/LibraryConverter.d.ts +113 -0
- package/dist/utils/LibraryConverter.d.ts.map +1 -0
- package/dist/utils/LibraryConverter.js +166 -0
- package/dist/utils/LibraryConverter.js.map +1 -0
- package/dist/utils/PathMatcher.d.ts +55 -0
- package/dist/utils/PathMatcher.d.ts.map +1 -0
- package/dist/utils/PathMatcher.js +172 -0
- package/dist/utils/PathMatcher.js.map +1 -0
- package/dist/utils/YamlParser.d.ts +36 -0
- package/dist/utils/YamlParser.d.ts.map +1 -0
- package/dist/utils/YamlParser.js +63 -0
- package/dist/utils/YamlParser.js.map +1 -0
- package/package.json +47 -0
- package/src/ConfigurationLoader.test.ts +490 -0
- package/src/ConfigurationLoader.ts +185 -0
- package/src/ConfigurationValidator.test.ts +200 -0
- package/src/ConfigurationValidator.ts +283 -0
- package/src/EventProcessor.test.ts +405 -0
- package/src/EventProcessor.ts +250 -0
- package/src/EventRecorderService.test.ts +541 -0
- package/src/EventRecorderService.ts +744 -0
- package/src/LibraryLoader.ts +215 -0
- package/src/PathBasedEventProcessor.test.ts +567 -0
- package/src/PathBasedEventProcessor.ts +332 -0
- package/src/SessionManager.test.ts +424 -0
- package/src/SessionManager.ts +470 -0
- package/src/ValidationEngine.test.ts +371 -0
- package/src/ValidationEngine.ts +196 -0
- package/src/helpers/GraphInstrumentationHelper.test.ts +340 -0
- package/src/helpers/GraphInstrumentationHelper.ts +326 -0
- package/src/index.ts +85 -0
- package/src/rules/config.test.ts +278 -0
- package/src/rules/config.ts +459 -0
- package/src/rules/engine.test.ts +332 -0
- package/src/rules/engine.ts +318 -0
- package/src/rules/implementations/connection-type-references.ts +117 -0
- package/src/rules/implementations/dead-end-states.ts +101 -0
- package/src/rules/implementations/index.ts +73 -0
- package/src/rules/implementations/library-node-type-match.ts +148 -0
- package/src/rules/implementations/minimum-node-sources.ts +82 -0
- package/src/rules/implementations/no-unknown-fields.ts +342 -0
- package/src/rules/implementations/orphaned-edge-types.ts +55 -0
- package/src/rules/implementations/orphaned-node-types.ts +58 -0
- package/src/rules/implementations/required-metadata.ts +64 -0
- package/src/rules/implementations/state-transition-references.ts +151 -0
- package/src/rules/implementations/unreachable-states.ts +94 -0
- package/src/rules/implementations/valid-action-patterns.ts +136 -0
- package/src/rules/implementations/valid-color-format.ts +140 -0
- package/src/rules/implementations/valid-edge-types.ts +258 -0
- package/src/rules/implementations/valid-node-types.ts +189 -0
- package/src/rules/index.ts +95 -0
- package/src/rules/types.ts +426 -0
- package/src/types/canvas.ts +496 -0
- package/src/types/index.ts +382 -0
- package/src/types/library.ts +233 -0
- package/src/types/path-based-config.ts +281 -0
- package/src/utils/CanvasConverter.ts +431 -0
- package/src/utils/GraphConverter.test.ts +195 -0
- package/src/utils/GraphConverter.ts +71 -0
- package/src/utils/LibraryConverter.ts +245 -0
- package/src/utils/PathMatcher.test.ts +148 -0
- package/src/utils/PathMatcher.ts +183 -0
- package/src/utils/YamlParser.ts +75 -0
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EventRecorderService - Log ingestion and event recording service
|
|
3
|
+
*
|
|
4
|
+
* This service:
|
|
5
|
+
* 1. Accepts logs via message protocol or direct API
|
|
6
|
+
* 2. Processes logs through PathBasedEventProcessor
|
|
7
|
+
* 3. Manages sessions through SessionManager
|
|
8
|
+
* 4. Emits events to subscribers for visualization
|
|
9
|
+
*
|
|
10
|
+
* The service is transport-agnostic - it handles message parsing and responses
|
|
11
|
+
* but does not implement any specific transport (WebSocket, IPC, etc.).
|
|
12
|
+
* The host application is responsible for the actual transport layer.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { SessionManager } from './SessionManager';
|
|
16
|
+
import type { EventSession, CreateSessionOptions, EndSessionOptions } from './SessionManager';
|
|
17
|
+
import { PathBasedEventProcessor } from './PathBasedEventProcessor';
|
|
18
|
+
import type { LogEntry } from './PathBasedEventProcessor';
|
|
19
|
+
import type { PathBasedGraphConfiguration, PathBasedEvent } from './types/path-based-config';
|
|
20
|
+
import type { GraphEvent } from './types';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Protocol message types for log ingestion
|
|
24
|
+
*/
|
|
25
|
+
export type ProtocolMessageType =
|
|
26
|
+
| 'session_start'
|
|
27
|
+
| 'session_end'
|
|
28
|
+
| 'log'
|
|
29
|
+
| 'log_batch'
|
|
30
|
+
| 'ping'
|
|
31
|
+
| 'pong'
|
|
32
|
+
| 'error'
|
|
33
|
+
| 'ack';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Base protocol message structure
|
|
37
|
+
*/
|
|
38
|
+
export interface ProtocolMessage {
|
|
39
|
+
type: ProtocolMessageType;
|
|
40
|
+
timestamp: number;
|
|
41
|
+
requestId?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Session start message
|
|
46
|
+
*/
|
|
47
|
+
export interface SessionStartMessage extends ProtocolMessage {
|
|
48
|
+
type: 'session_start';
|
|
49
|
+
payload: {
|
|
50
|
+
name: string;
|
|
51
|
+
id?: string;
|
|
52
|
+
metadata?: {
|
|
53
|
+
testFile?: string;
|
|
54
|
+
testName?: string;
|
|
55
|
+
testSuite?: string;
|
|
56
|
+
tags?: string[];
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Session end message
|
|
63
|
+
*/
|
|
64
|
+
export interface SessionEndMessage extends ProtocolMessage {
|
|
65
|
+
type: 'session_end';
|
|
66
|
+
payload: {
|
|
67
|
+
sessionId: string;
|
|
68
|
+
result?: 'pass' | 'fail' | 'skip';
|
|
69
|
+
error?: string;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Single log message
|
|
75
|
+
*/
|
|
76
|
+
export interface LogMessage extends ProtocolMessage {
|
|
77
|
+
type: 'log';
|
|
78
|
+
payload: LogEntry;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Batch log message
|
|
83
|
+
*/
|
|
84
|
+
export interface LogBatchMessage extends ProtocolMessage {
|
|
85
|
+
type: 'log_batch';
|
|
86
|
+
payload: {
|
|
87
|
+
logs: LogEntry[];
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Ping message for keepalive
|
|
93
|
+
*/
|
|
94
|
+
export interface PingMessage extends ProtocolMessage {
|
|
95
|
+
type: 'ping';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Pong response
|
|
100
|
+
*/
|
|
101
|
+
export interface PongMessage extends ProtocolMessage {
|
|
102
|
+
type: 'pong';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Error response
|
|
107
|
+
*/
|
|
108
|
+
export interface ErrorMessage extends ProtocolMessage {
|
|
109
|
+
type: 'error';
|
|
110
|
+
payload: {
|
|
111
|
+
code: string;
|
|
112
|
+
message: string;
|
|
113
|
+
requestId?: string;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Acknowledgment response
|
|
119
|
+
*/
|
|
120
|
+
export interface AckMessage extends ProtocolMessage {
|
|
121
|
+
type: 'ack';
|
|
122
|
+
payload: {
|
|
123
|
+
requestId: string;
|
|
124
|
+
sessionId?: string;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Union type for all incoming messages
|
|
130
|
+
*/
|
|
131
|
+
export type IncomingMessage =
|
|
132
|
+
| SessionStartMessage
|
|
133
|
+
| SessionEndMessage
|
|
134
|
+
| LogMessage
|
|
135
|
+
| LogBatchMessage
|
|
136
|
+
| PingMessage;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Union type for all outgoing messages
|
|
140
|
+
*/
|
|
141
|
+
export type OutgoingMessage = PongMessage | ErrorMessage | AckMessage;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Recording mode
|
|
145
|
+
*/
|
|
146
|
+
export type RecordingMode = 'auto' | 'manual' | 'continuous';
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Event callback for processed events
|
|
150
|
+
*/
|
|
151
|
+
export type EventCallback = (event: PathBasedEvent | GraphEvent) => void;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Event batch callback
|
|
155
|
+
*/
|
|
156
|
+
export type EventBatchCallback = (events: Array<PathBasedEvent | GraphEvent>) => void;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Connection state
|
|
160
|
+
*/
|
|
161
|
+
export interface ConnectionState {
|
|
162
|
+
id: string;
|
|
163
|
+
connectedAt: number;
|
|
164
|
+
lastActivityAt: number;
|
|
165
|
+
activeSessionId: string | null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* EventRecorderService configuration
|
|
170
|
+
*/
|
|
171
|
+
export interface EventRecorderServiceConfig {
|
|
172
|
+
/** Graph configuration for PathBasedEventProcessor */
|
|
173
|
+
graphConfig: PathBasedGraphConfiguration;
|
|
174
|
+
|
|
175
|
+
/** Recording mode (default: 'manual') */
|
|
176
|
+
recordingMode?: RecordingMode;
|
|
177
|
+
|
|
178
|
+
/** Whether to emit events immediately or batch them (default: true) */
|
|
179
|
+
emitImmediately?: boolean;
|
|
180
|
+
|
|
181
|
+
/** Batch interval in ms when emitImmediately is false (default: 100) */
|
|
182
|
+
batchIntervalMs?: number;
|
|
183
|
+
|
|
184
|
+
/** SessionManager configuration */
|
|
185
|
+
sessionConfig?: {
|
|
186
|
+
maxEventsPerSession?: number;
|
|
187
|
+
maxSessions?: number;
|
|
188
|
+
sessionRetention?: number;
|
|
189
|
+
autoCleanup?: boolean;
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* EventRecorderService - Main service for log ingestion and event recording
|
|
195
|
+
*/
|
|
196
|
+
export class EventRecorderService {
|
|
197
|
+
private sessionManager: SessionManager;
|
|
198
|
+
private eventProcessor: PathBasedEventProcessor;
|
|
199
|
+
private config: Required<Omit<EventRecorderServiceConfig, 'graphConfig' | 'sessionConfig'>> & {
|
|
200
|
+
graphConfig: PathBasedGraphConfiguration;
|
|
201
|
+
};
|
|
202
|
+
private eventListeners: Set<EventCallback> = new Set();
|
|
203
|
+
private batchListeners: Set<EventBatchCallback> = new Set();
|
|
204
|
+
private eventBuffer: Array<PathBasedEvent | GraphEvent> = [];
|
|
205
|
+
private batchTimer: ReturnType<typeof setInterval> | null = null;
|
|
206
|
+
private connections: Map<string, ConnectionState> = new Map();
|
|
207
|
+
private isRecording: boolean = false;
|
|
208
|
+
|
|
209
|
+
constructor(config: EventRecorderServiceConfig) {
|
|
210
|
+
this.config = {
|
|
211
|
+
graphConfig: config.graphConfig,
|
|
212
|
+
recordingMode: config.recordingMode ?? 'manual',
|
|
213
|
+
emitImmediately: config.emitImmediately ?? true,
|
|
214
|
+
batchIntervalMs: config.batchIntervalMs ?? 100,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
this.sessionManager = new SessionManager(config.sessionConfig);
|
|
218
|
+
this.eventProcessor = new PathBasedEventProcessor(config.graphConfig);
|
|
219
|
+
|
|
220
|
+
// Start batch processing if not emitting immediately
|
|
221
|
+
if (!this.config.emitImmediately) {
|
|
222
|
+
this.startBatchProcessing();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// In continuous mode, start recording immediately
|
|
226
|
+
if (this.config.recordingMode === 'continuous') {
|
|
227
|
+
this.isRecording = true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Process an incoming protocol message
|
|
233
|
+
*/
|
|
234
|
+
public processMessage(message: IncomingMessage, connectionId: string): OutgoingMessage | null {
|
|
235
|
+
// Update connection activity
|
|
236
|
+
const connection = this.connections.get(connectionId);
|
|
237
|
+
if (connection) {
|
|
238
|
+
connection.lastActivityAt = Date.now();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
switch (message.type) {
|
|
242
|
+
case 'session_start':
|
|
243
|
+
return this.handleSessionStart(message, connectionId);
|
|
244
|
+
|
|
245
|
+
case 'session_end':
|
|
246
|
+
return this.handleSessionEnd(message, connectionId);
|
|
247
|
+
|
|
248
|
+
case 'log':
|
|
249
|
+
return this.handleLog(message, connectionId);
|
|
250
|
+
|
|
251
|
+
case 'log_batch':
|
|
252
|
+
return this.handleLogBatch(message, connectionId);
|
|
253
|
+
|
|
254
|
+
case 'ping':
|
|
255
|
+
return this.handlePing(message);
|
|
256
|
+
|
|
257
|
+
default: {
|
|
258
|
+
const exhaustiveCheck: never = message;
|
|
259
|
+
return this.createError('UNKNOWN_MESSAGE_TYPE', `Unknown message type: ${(exhaustiveCheck as ProtocolMessage).type}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Handle session start message
|
|
266
|
+
*/
|
|
267
|
+
private handleSessionStart(
|
|
268
|
+
message: SessionStartMessage,
|
|
269
|
+
connectionId: string
|
|
270
|
+
): OutgoingMessage {
|
|
271
|
+
try {
|
|
272
|
+
const session = this.sessionManager.createSession({
|
|
273
|
+
name: message.payload.name,
|
|
274
|
+
id: message.payload.id,
|
|
275
|
+
metadata: message.payload.metadata,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Associate session with connection
|
|
279
|
+
const connection = this.connections.get(connectionId);
|
|
280
|
+
if (connection) {
|
|
281
|
+
connection.activeSessionId = session.id;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
this.isRecording = true;
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
type: 'ack',
|
|
288
|
+
timestamp: Date.now(),
|
|
289
|
+
payload: {
|
|
290
|
+
requestId: message.requestId || '',
|
|
291
|
+
sessionId: session.id,
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
} catch (error) {
|
|
295
|
+
return this.createError(
|
|
296
|
+
'SESSION_START_FAILED',
|
|
297
|
+
error instanceof Error ? error.message : 'Failed to start session',
|
|
298
|
+
message.requestId
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Handle session end message
|
|
305
|
+
*/
|
|
306
|
+
private handleSessionEnd(
|
|
307
|
+
message: SessionEndMessage,
|
|
308
|
+
connectionId: string
|
|
309
|
+
): OutgoingMessage {
|
|
310
|
+
try {
|
|
311
|
+
this.sessionManager.endSession(message.payload.sessionId, {
|
|
312
|
+
result: message.payload.result,
|
|
313
|
+
error: message.payload.error,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// Clear session from connection
|
|
317
|
+
const connection = this.connections.get(connectionId);
|
|
318
|
+
if (connection && connection.activeSessionId === message.payload.sessionId) {
|
|
319
|
+
connection.activeSessionId = null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// In manual mode, stop recording when session ends
|
|
323
|
+
if (this.config.recordingMode === 'manual') {
|
|
324
|
+
this.isRecording = false;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
type: 'ack',
|
|
329
|
+
timestamp: Date.now(),
|
|
330
|
+
payload: {
|
|
331
|
+
requestId: message.requestId || '',
|
|
332
|
+
sessionId: message.payload.sessionId,
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
} catch (error) {
|
|
336
|
+
return this.createError(
|
|
337
|
+
'SESSION_END_FAILED',
|
|
338
|
+
error instanceof Error ? error.message : 'Failed to end session',
|
|
339
|
+
message.requestId
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Handle single log message
|
|
346
|
+
*/
|
|
347
|
+
private handleLog(message: LogMessage, connectionId: string): OutgoingMessage | null {
|
|
348
|
+
if (!this.isRecording) {
|
|
349
|
+
return null; // Silently ignore logs when not recording
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const events = this.eventProcessor.processLog(message.payload);
|
|
353
|
+
|
|
354
|
+
// Add events to active session
|
|
355
|
+
const activeSession = this.sessionManager.getActiveSession();
|
|
356
|
+
if (activeSession) {
|
|
357
|
+
for (const event of events) {
|
|
358
|
+
this.sessionManager.addEvent(activeSession.id, event as unknown as GraphEvent);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Emit events
|
|
363
|
+
this.emitEvents(events);
|
|
364
|
+
|
|
365
|
+
if (message.requestId) {
|
|
366
|
+
return {
|
|
367
|
+
type: 'ack',
|
|
368
|
+
timestamp: Date.now(),
|
|
369
|
+
payload: {
|
|
370
|
+
requestId: message.requestId,
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Handle batch log message
|
|
380
|
+
*/
|
|
381
|
+
private handleLogBatch(message: LogBatchMessage, connectionId: string): OutgoingMessage | null {
|
|
382
|
+
if (!this.isRecording) {
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const events = this.eventProcessor.processLogs(message.payload.logs);
|
|
387
|
+
|
|
388
|
+
// Add events to active session
|
|
389
|
+
const activeSession = this.sessionManager.getActiveSession();
|
|
390
|
+
if (activeSession) {
|
|
391
|
+
for (const event of events) {
|
|
392
|
+
this.sessionManager.addEvent(activeSession.id, event as unknown as GraphEvent);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Emit events
|
|
397
|
+
this.emitEvents(events);
|
|
398
|
+
|
|
399
|
+
if (message.requestId) {
|
|
400
|
+
return {
|
|
401
|
+
type: 'ack',
|
|
402
|
+
timestamp: Date.now(),
|
|
403
|
+
payload: {
|
|
404
|
+
requestId: message.requestId,
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Handle ping message
|
|
414
|
+
*/
|
|
415
|
+
private handlePing(message: PingMessage): PongMessage {
|
|
416
|
+
return {
|
|
417
|
+
type: 'pong',
|
|
418
|
+
timestamp: Date.now(),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Create an error response
|
|
424
|
+
*/
|
|
425
|
+
private createError(code: string, message: string, requestId?: string): ErrorMessage {
|
|
426
|
+
return {
|
|
427
|
+
type: 'error',
|
|
428
|
+
timestamp: Date.now(),
|
|
429
|
+
payload: {
|
|
430
|
+
code,
|
|
431
|
+
message,
|
|
432
|
+
requestId,
|
|
433
|
+
},
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Emit events to listeners
|
|
439
|
+
*/
|
|
440
|
+
private emitEvents(events: PathBasedEvent[]): void {
|
|
441
|
+
if (events.length === 0) return;
|
|
442
|
+
|
|
443
|
+
if (this.config.emitImmediately) {
|
|
444
|
+
// Emit immediately to all listeners
|
|
445
|
+
for (const event of events) {
|
|
446
|
+
for (const listener of this.eventListeners) {
|
|
447
|
+
try {
|
|
448
|
+
listener(event);
|
|
449
|
+
} catch (error) {
|
|
450
|
+
console.error('Event listener error:', error);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
} else {
|
|
455
|
+
// Buffer events for batch emission
|
|
456
|
+
this.eventBuffer.push(...events);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Start batch processing timer
|
|
462
|
+
*/
|
|
463
|
+
private startBatchProcessing(): void {
|
|
464
|
+
this.batchTimer = setInterval(() => {
|
|
465
|
+
if (this.eventBuffer.length > 0) {
|
|
466
|
+
const events = [...this.eventBuffer];
|
|
467
|
+
this.eventBuffer = [];
|
|
468
|
+
|
|
469
|
+
for (const listener of this.batchListeners) {
|
|
470
|
+
try {
|
|
471
|
+
listener(events);
|
|
472
|
+
} catch (error) {
|
|
473
|
+
console.error('Batch listener error:', error);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}, this.config.batchIntervalMs);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Stop batch processing timer
|
|
482
|
+
*/
|
|
483
|
+
private stopBatchProcessing(): void {
|
|
484
|
+
if (this.batchTimer) {
|
|
485
|
+
clearInterval(this.batchTimer);
|
|
486
|
+
this.batchTimer = null;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// === Direct API ===
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Process a log entry directly (without protocol message wrapper)
|
|
494
|
+
*/
|
|
495
|
+
public processLog(log: LogEntry): PathBasedEvent[] {
|
|
496
|
+
if (!this.isRecording) {
|
|
497
|
+
return [];
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const events = this.eventProcessor.processLog(log);
|
|
501
|
+
|
|
502
|
+
// Add events to active session
|
|
503
|
+
const activeSession = this.sessionManager.getActiveSession();
|
|
504
|
+
if (activeSession) {
|
|
505
|
+
for (const event of events) {
|
|
506
|
+
this.sessionManager.addEvent(activeSession.id, event as unknown as GraphEvent);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Emit events
|
|
511
|
+
this.emitEvents(events);
|
|
512
|
+
|
|
513
|
+
return events;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Process multiple log entries directly (without protocol message wrapper)
|
|
518
|
+
*/
|
|
519
|
+
public processLogs(logs: LogEntry[]): PathBasedEvent[] {
|
|
520
|
+
if (!this.isRecording) {
|
|
521
|
+
return [];
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const events = this.eventProcessor.processLogs(logs);
|
|
525
|
+
|
|
526
|
+
// Add events to active session
|
|
527
|
+
const activeSession = this.sessionManager.getActiveSession();
|
|
528
|
+
if (activeSession) {
|
|
529
|
+
for (const event of events) {
|
|
530
|
+
this.sessionManager.addEvent(activeSession.id, event as unknown as GraphEvent);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Emit events
|
|
535
|
+
this.emitEvents(events);
|
|
536
|
+
|
|
537
|
+
return events;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Start a new session directly
|
|
542
|
+
*/
|
|
543
|
+
public startSession(options: CreateSessionOptions): EventSession {
|
|
544
|
+
const session = this.sessionManager.createSession(options);
|
|
545
|
+
this.isRecording = true;
|
|
546
|
+
return session;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* End a session directly
|
|
551
|
+
*/
|
|
552
|
+
public endSession(sessionId: string, options?: EndSessionOptions): void {
|
|
553
|
+
this.sessionManager.endSession(sessionId, options);
|
|
554
|
+
|
|
555
|
+
if (this.config.recordingMode === 'manual') {
|
|
556
|
+
this.isRecording = false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* End the active session
|
|
562
|
+
*/
|
|
563
|
+
public endActiveSession(options?: EndSessionOptions): void {
|
|
564
|
+
this.sessionManager.endActiveSession(options);
|
|
565
|
+
|
|
566
|
+
if (this.config.recordingMode === 'manual') {
|
|
567
|
+
this.isRecording = false;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Start recording (for manual mode)
|
|
573
|
+
*/
|
|
574
|
+
public startRecording(): void {
|
|
575
|
+
this.isRecording = true;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Stop recording (for manual mode)
|
|
580
|
+
*/
|
|
581
|
+
public stopRecording(): void {
|
|
582
|
+
this.isRecording = false;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Check if currently recording
|
|
587
|
+
*/
|
|
588
|
+
public get recording(): boolean {
|
|
589
|
+
return this.isRecording;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// === Connection Management ===
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Register a new connection
|
|
596
|
+
*/
|
|
597
|
+
public registerConnection(connectionId: string): void {
|
|
598
|
+
this.connections.set(connectionId, {
|
|
599
|
+
id: connectionId,
|
|
600
|
+
connectedAt: Date.now(),
|
|
601
|
+
lastActivityAt: Date.now(),
|
|
602
|
+
activeSessionId: null,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Unregister a connection
|
|
608
|
+
*/
|
|
609
|
+
public unregisterConnection(connectionId: string): void {
|
|
610
|
+
const connection = this.connections.get(connectionId);
|
|
611
|
+
|
|
612
|
+
// End any active session for this connection
|
|
613
|
+
if (connection?.activeSessionId) {
|
|
614
|
+
try {
|
|
615
|
+
this.sessionManager.errorSession(
|
|
616
|
+
connection.activeSessionId,
|
|
617
|
+
'Connection closed unexpectedly'
|
|
618
|
+
);
|
|
619
|
+
} catch {
|
|
620
|
+
// Session may have already ended
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
this.connections.delete(connectionId);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Get connection state
|
|
629
|
+
*/
|
|
630
|
+
public getConnection(connectionId: string): ConnectionState | undefined {
|
|
631
|
+
return this.connections.get(connectionId);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Get all active connections
|
|
636
|
+
*/
|
|
637
|
+
public getConnections(): ConnectionState[] {
|
|
638
|
+
return Array.from(this.connections.values());
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// === Event Subscription ===
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Subscribe to individual events
|
|
645
|
+
*/
|
|
646
|
+
public onEvent(callback: EventCallback): () => void {
|
|
647
|
+
this.eventListeners.add(callback);
|
|
648
|
+
return () => {
|
|
649
|
+
this.eventListeners.delete(callback);
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Subscribe to event batches
|
|
655
|
+
*/
|
|
656
|
+
public onEventBatch(callback: EventBatchCallback): () => void {
|
|
657
|
+
this.batchListeners.add(callback);
|
|
658
|
+
return () => {
|
|
659
|
+
this.batchListeners.delete(callback);
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// === Session Access ===
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Get SessionManager for direct access
|
|
667
|
+
*/
|
|
668
|
+
public getSessionManager(): SessionManager {
|
|
669
|
+
return this.sessionManager;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Get active session
|
|
674
|
+
*/
|
|
675
|
+
public getActiveSession(): EventSession | undefined {
|
|
676
|
+
return this.sessionManager.getActiveSession();
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Get session by ID
|
|
681
|
+
*/
|
|
682
|
+
public getSession(id: string): EventSession | undefined {
|
|
683
|
+
return this.sessionManager.getSession(id);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* List all sessions
|
|
688
|
+
*/
|
|
689
|
+
public listSessions(): EventSession[] {
|
|
690
|
+
return this.sessionManager.listSessions();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// === Statistics ===
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Get service statistics
|
|
697
|
+
*/
|
|
698
|
+
public getStats(): {
|
|
699
|
+
isRecording: boolean;
|
|
700
|
+
recordingMode: RecordingMode;
|
|
701
|
+
activeConnections: number;
|
|
702
|
+
sessions: {
|
|
703
|
+
total: number;
|
|
704
|
+
active: number;
|
|
705
|
+
totalEvents: number;
|
|
706
|
+
};
|
|
707
|
+
processor: {
|
|
708
|
+
totalComponents: number;
|
|
709
|
+
componentsWithSources: number;
|
|
710
|
+
totalSourcePatterns: number;
|
|
711
|
+
};
|
|
712
|
+
} {
|
|
713
|
+
const sessionStats = this.sessionManager.getStats();
|
|
714
|
+
const processorStats = this.eventProcessor.getStats();
|
|
715
|
+
|
|
716
|
+
return {
|
|
717
|
+
isRecording: this.isRecording,
|
|
718
|
+
recordingMode: this.config.recordingMode,
|
|
719
|
+
activeConnections: this.connections.size,
|
|
720
|
+
sessions: {
|
|
721
|
+
total: sessionStats.totalSessions,
|
|
722
|
+
active: sessionStats.activeSessions,
|
|
723
|
+
totalEvents: sessionStats.totalEvents,
|
|
724
|
+
},
|
|
725
|
+
processor: {
|
|
726
|
+
totalComponents: processorStats.totalComponents,
|
|
727
|
+
componentsWithSources: processorStats.componentsWithSources,
|
|
728
|
+
totalSourcePatterns: processorStats.totalSourcePatterns,
|
|
729
|
+
},
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Dispose of the service
|
|
735
|
+
*/
|
|
736
|
+
public dispose(): void {
|
|
737
|
+
this.stopBatchProcessing();
|
|
738
|
+
this.sessionManager.dispose();
|
|
739
|
+
this.eventListeners.clear();
|
|
740
|
+
this.batchListeners.clear();
|
|
741
|
+
this.connections.clear();
|
|
742
|
+
this.eventBuffer = [];
|
|
743
|
+
}
|
|
744
|
+
}
|