Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import type { z } from 'zod';

import { ConnectionError, SessionError } from './errors.js';
import {
dispatchNotification,
ProtocolEngine,
type AskUserHandler,
type NotificationCallback,
type NotificationFilter,
type NotificationListener,
type PermissionHandler,
} from './protocol.js';
import type {
Expand Down Expand Up @@ -93,7 +95,6 @@ import {
SESSION_INIT_TIMEOUT,
} from './schemas/constants.js';
import { DroidServerMethod, ToolConfirmationOutcome } from './schemas/enums.js';
import { SessionNotificationParamsSchema } from './schemas/server.js';
import type {
AskUserRequestParams,
AskUserResult,
Expand All @@ -106,11 +107,6 @@ export type ClientPermissionHandler = PermissionHandler;

export type ClientAskUserHandler = AskUserHandler;

interface ClientNotificationListener {
readonly callback: NotificationCallback;
readonly filter?: NotificationFilter;
}

export interface DroidClientOptions {
/** A connected DroidClientTransport implementation. */
transport: DroidClientTransport;
Expand All @@ -124,11 +120,7 @@ export class DroidClient {
private _sessionId: string | null = null;
private _closed = false;

/**
* Client-level notification listeners.
* Each entry is [callback, optional notification filter].
*/
private readonly _notificationListeners: ClientNotificationListener[] = [];
private readonly _notificationListeners: NotificationListener[] = [];

/** Client-level permission handler. */
private _permissionHandler: ClientPermissionHandler | null = null;
Expand Down Expand Up @@ -463,7 +455,7 @@ export class DroidClient {
callback: NotificationCallback,
filter?: NotificationFilter
): () => void {
const entry: ClientNotificationListener = { callback, filter };
const entry: NotificationListener = { callback, filter };
this._notificationListeners.push(entry);

let unsubscribed = false;
Expand Down Expand Up @@ -508,29 +500,7 @@ export class DroidClient {
}

private _dispatchNotification(notification: Record<string, unknown>): void {
let notificationType: string | undefined;
const parsed = SessionNotificationParamsSchema.safeParse(
notification['params']
);
if (parsed.success) {
notificationType = parsed.data.notification.type;
}

const listeners = [...this._notificationListeners];
for (const listener of listeners) {
if (
listener.filter?.type != null &&
listener.filter.type !== notificationType
) {
continue;
}

try {
listener.callback(notification);
} catch {
// Notification listener raised — don't crash the client
}
}
dispatchNotification(notification, [...this._notificationListeners]);
}

private _dispatchPermissionRequest(
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export * from './errors.js';
export * from './types.js';
export { ProcessTransport } from './transport.js';

export { ProtocolEngine } from './protocol.js';
export { dispatchNotification, ProtocolEngine } from './protocol.js';
export type {
AskUserHandler,
NotificationCallback,
NotificationFilter,
NotificationListener,
PermissionHandler,
} from './protocol.js';

Expand Down
54 changes: 31 additions & 23 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,40 @@ interface PendingRequest {
readonly timer: ReturnType<typeof setTimeout>;
}

interface NotificationListener {
export interface NotificationListener {
readonly callback: NotificationCallback;
readonly filter?: NotificationFilter;
}

/** Dispatch a notification to matching listeners, swallowing listener errors. */
export function dispatchNotification(
notification: Record<string, unknown>,
listeners: Iterable<NotificationListener>
): void {
let notificationType: string | undefined;
const parsed = SessionNotificationParamsSchema.safeParse(
notification['params']
);
if (parsed.success) {
notificationType = parsed.data.notification.type;
}

for (const listener of listeners) {
if (
listener.filter?.type != null &&
listener.filter.type !== notificationType
) {
continue;
}

try {
listener.callback(notification);
} catch {
// Notification listener raised — don't crash the dispatch loop
}
}
}

export class ProtocolEngine {
private readonly _transport: DroidClientTransport;
private readonly _defaultTimeout: number;
Expand Down Expand Up @@ -277,28 +306,7 @@ export class ProtocolEngine {
}

private _handleNotification(notification: Record<string, unknown>): void {
let notificationType: string | undefined;
const parsed = SessionNotificationParamsSchema.safeParse(
notification['params']
);
if (parsed.success) {
notificationType = parsed.data.notification.type;
}

for (const listener of this._notificationListeners) {
if (
listener.filter?.type != null &&
listener.filter.type !== notificationType
) {
continue;
}

try {
listener.callback(notification);
} catch {
// Notification listener raised — don't crash the engine
}
}
dispatchNotification(notification, this._notificationListeners);
}

private async _handleServerRequest(
Expand Down
Loading
Loading