Skip to content

Add ml model service #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 31, 2025
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
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export { GantryClient, type Gantry } from './components/gantry';
*/
export * as gantryApi from './gen/component/gantry/v1/gantry_pb';

export { MLModelClient, type MLModel } from './services/ml-model';

export { MotorClient, type Motor } from './components/motor';
/**
* Raw Protobuf interfaces for a Motor component.
Expand Down
16 changes: 16 additions & 0 deletions src/robot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { clientHeaders } from '../utils';
import GRPCConnectionManager from './grpc-connection-manager';
import type { Robot } from './robot';
import SessionManager from './session-manager';
import { MLModelService } from '../gen/service/mlmodel/v1/mlmodel_connect';

interface WebRTCOptions {
enabled: boolean;
Expand Down Expand Up @@ -116,6 +117,10 @@ export class RobotClient extends EventDispatcher implements Robot {
| PromiseClient<typeof GripperService>
| undefined;

private mlModelServiceClient:
| PromiseClient<typeof MLModelService>
| undefined;

private movementSensorServiceClient:
| PromiseClient<typeof MovementSensorService>
| undefined;
Expand Down Expand Up @@ -318,6 +323,13 @@ export class RobotClient extends EventDispatcher implements Robot {
return this.gripperServiceClient;
}

get mlModelService() {
if (!this.mlModelServiceClient) {
throw new Error(RobotClient.notConnectedYetStr);
}
return this.mlModelServiceClient;
}

get movementSensorService() {
if (!this.movementSensorServiceClient) {
throw new Error(RobotClient.notConnectedYetStr);
Expand Down Expand Up @@ -582,6 +594,10 @@ export class RobotClient extends EventDispatcher implements Robot {
GripperService,
clientTransport
);
this.mlModelServiceClient = createPromiseClient(
MLModelService,
clientTransport
);
this.movementSensorServiceClient = createPromiseClient(
MovementSensorService,
clientTransport
Expand Down
49 changes: 49 additions & 0 deletions src/services/ml-model/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
import type { FlatTensors, MLModel } from './ml-model';
import { Struct, type Options } from '../../types';
import type { RobotClient } from '../../robot';
import {
InferRequest,
MetadataRequest,
} from '../../gen/service/mlmodel/v1/mlmodel_pb';
import { MLModelService } from '../../gen/service/mlmodel/v1/mlmodel_connect';

export class MLModelClient implements MLModel {
private client: PromiseClient<typeof MLModelService>;
private readonly name: string;
private readonly options: Options;
public callOptions: CallOptions = { headers: {} as Record<string, string> };

constructor(client: RobotClient, name: string, options: Options = {}) {
this.client = client.createServiceClient(MLModelService);
this.name = name;
this.options = options;
}

async metadata(extra = {}, callOptions = this.callOptions) {
const request = new MetadataRequest({
name: this.name,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

return this.client.metadata(request, callOptions);
}

async infer(
inputTensors: FlatTensors,
extra = {},
callOptions = this.callOptions
) {
const request = new InferRequest({
name: this.name,
inputTensors,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

return this.client.infer(request, callOptions);
}
}
2 changes: 2 additions & 0 deletions src/services/ml-model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { MLModel, FlatTensors } from './ml-model';
export { MLModelClient } from './client';
13 changes: 13 additions & 0 deletions src/services/ml-model/ml-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Struct } from '@bufbuild/protobuf';
import type * as mlModelAPI from '../../gen/service/mlmodel/v1/mlmodel_pb';

export type FlatTensors = mlModelAPI.FlatTensors;

export interface MLModel {
metadata: (extra?: Struct) => Promise<mlModelAPI.MetadataResponse>;

infer: (
inputTensors: FlatTensors,
extra?: Struct
) => Promise<mlModelAPI.InferResponse>;
}