Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions packages/lms-communication-client/src/ClientPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function defaultErrorDeserializer(
return fromSerializedError(serialized, directCause, stack);
}

export interface ClientPortCommunicationWarning {
direction: "produced" | "received";
warning: string;
}

export class ClientPort<
TRpcEndpoints extends RpcEndpointsSpecBase,
TChannelEndpoints extends ChannelEndpointsSpecBase,
Expand Down Expand Up @@ -104,6 +109,9 @@ export class ClientPort<
stack?: string,
) => Error;
private verboseErrorMessage: boolean;
private readonly onCommunicationWarning?: (
communicationWarning: ClientPortCommunicationWarning,
) => void;

public constructor(
public readonly backendInterface: BackendInterface<
Expand All @@ -118,6 +126,7 @@ export class ClientPort<
parentLogger,
errorDeserializer,
verboseErrorMessage,
onCommunicationWarning,
}: {
parentLogger?: LoggerInterface;
errorDeserializer?: (
Expand All @@ -126,11 +135,13 @@ export class ClientPort<
stack?: string,
) => Error;
verboseErrorMessage?: boolean;
onCommunicationWarning?: (communicationWarning: ClientPortCommunicationWarning) => void;
} = {},
) {
this.logger = new SimpleLogger("ClientPort", parentLogger);
this.errorDeserializer = errorDeserializer ?? defaultErrorDeserializer;
this.verboseErrorMessage = verboseErrorMessage ?? true;
this.onCommunicationWarning = onCommunicationWarning;
this.transport = factory(this.receivedMessage, this.onConnected, this.errored, this.logger);
}

Expand All @@ -151,6 +162,10 @@ export class ClientPort<
},
"communicationWarning",
);
this.reportCommunicationWarning({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this. In the case that a onCommunicationWarning callback is set, no need to log the warning above.

direction: "produced",
warning,
});
this.producedCommunicationWarningsCount++;
if (this.producedCommunicationWarningsCount >= 5) {
this.logger.errorText`
Expand Down Expand Up @@ -439,6 +454,21 @@ export class ClientPort<

Note: This warning was received from the server and is printed on the client for convenience.
`;
this.reportCommunicationWarning({
direction: "received",
warning: message.warning,
});
}

private reportCommunicationWarning(communicationWarning: ClientPortCommunicationWarning): void {
if (this.onCommunicationWarning === undefined) {
return;
}
try {
this.onCommunicationWarning(communicationWarning);
} catch (error) {
this.logger.error("Error in onCommunicationWarning callback:", error);
}
}

private receivedKeepAliveAck(_message: ServerToClientMessage & { type: "keepAliveAck" }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/lms-communication-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { AuthenticatedWsClientTransport } from "./AuthenticatedWsClientTransport.js";
export { ClientPort, InferClientPort } from "./ClientPort.js";
export { ClientPort, type ClientPortCommunicationWarning, InferClientPort } from "./ClientPort.js";
export { GenericClientTransport } from "./GenericClientTransport.js";
export { LMStudioHostedEnv, getHostedEnv } from "./LMStudioHostedEnv.js";
export { WsClientTransport } from "./WsClientTransport.js";