!feat(core): clarify mock stream event typing

This commit is contained in:
Adam Weidman
2026-03-20 14:33:30 -04:00
parent 139a5f3202
commit 18544cc994
+25 -19
View File
@@ -8,8 +8,8 @@ import type {
AgentEvent,
AgentEventCommon,
AgentEventData,
AgentSend,
AgentProtocol,
AgentSend,
Unsubscribe,
} from './types.js';
@@ -86,13 +86,7 @@ export class MockAgentProtocol implements AgentProtocol {
) {
const now = new Date().toISOString();
for (const eventData of events) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const event: AgentEvent = {
...eventData,
id: eventData.id ?? `e-${this._nextEventId++}`,
timestamp: eventData.timestamp ?? now,
streamId: eventData.streamId ?? streamId,
} as AgentEvent;
const event = this._normalizeEvent(eventData, now, streamId);
this._emit(event);
}
@@ -100,13 +94,14 @@ export class MockAgentProtocol implements AgentProtocol {
options?.close &&
!events.some((eventData) => eventData.type === 'agent_end')
) {
this._emit({
const endEvent = {
id: `e-${this._nextEventId++}`,
timestamp: now,
streamId,
type: 'agent_end',
reason: 'completed',
} as AgentEvent);
} satisfies AgentEvent<'agent_end'>;
this._emit(endEvent);
}
}
@@ -130,13 +125,7 @@ export class MockAgentProtocol implements AgentProtocol {
// All emitted events stay correlated to a stream even if this send does not
// start agent activity and therefore returns `streamId: null`.
const normalize = (eventData: MockAgentEvent): AgentEvent =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
({
...eventData,
id: eventData.id ?? `e-${this._nextEventId++}`,
timestamp: eventData.timestamp ?? now,
streamId: eventData.streamId ?? getCorrelationStreamId(),
}) as AgentEvent;
this._normalizeEvent(eventData, now, getCorrelationStreamId());
// 1. User/Update event (BEFORE agent_start)
if ('message' in payload && payload.message) {
@@ -228,16 +217,33 @@ export class MockAgentProtocol implements AgentProtocol {
return { streamId };
}
private _normalizeEvent(
eventData: MockAgentEvent,
timestamp: string,
streamId: string,
): AgentEvent {
// TypeScript loses the specific union member when we add common event
// fields here, so keep the narrowing local to this mock-only helper.
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return {
...eventData,
id: eventData.id ?? `e-${this._nextEventId++}`,
timestamp: eventData.timestamp ?? timestamp,
streamId: eventData.streamId ?? streamId,
} as AgentEvent;
}
async abort(): Promise<void> {
if (this._lastStreamId && this._activeStreamIds.has(this._lastStreamId)) {
const streamId = this._lastStreamId;
this._emit({
const endEvent = {
id: `e-${this._nextEventId++}`,
timestamp: new Date().toISOString(),
streamId,
type: 'agent_end',
reason: 'aborted',
} as AgentEvent);
} satisfies AgentEvent<'agent_end'>;
this._emit(endEvent);
}
}
}