Skip to content

Commit f7f862d

Browse files
authored
Add ml model service (#469)
1 parent 7ea6e41 commit f7f862d

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ export { GantryClient, type Gantry } from './components/gantry';
199199
*/
200200
export * as gantryApi from './gen/component/gantry/v1/gantry_pb';
201201

202+
export { MLModelClient, type MLModel } from './services/ml-model';
203+
202204
export { MotorClient, type Motor } from './components/motor';
203205
/**
204206
* Raw Protobuf interfaces for a Motor component.

src/robot/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { clientHeaders } from '../utils';
3939
import GRPCConnectionManager from './grpc-connection-manager';
4040
import type { Robot } from './robot';
4141
import SessionManager from './session-manager';
42+
import { MLModelService } from '../gen/service/mlmodel/v1/mlmodel_connect';
4243

4344
interface WebRTCOptions {
4445
enabled: boolean;
@@ -116,6 +117,10 @@ export class RobotClient extends EventDispatcher implements Robot {
116117
| PromiseClient<typeof GripperService>
117118
| undefined;
118119

120+
private mlModelServiceClient:
121+
| PromiseClient<typeof MLModelService>
122+
| undefined;
123+
119124
private movementSensorServiceClient:
120125
| PromiseClient<typeof MovementSensorService>
121126
| undefined;
@@ -318,6 +323,13 @@ export class RobotClient extends EventDispatcher implements Robot {
318323
return this.gripperServiceClient;
319324
}
320325

326+
get mlModelService() {
327+
if (!this.mlModelServiceClient) {
328+
throw new Error(RobotClient.notConnectedYetStr);
329+
}
330+
return this.mlModelServiceClient;
331+
}
332+
321333
get movementSensorService() {
322334
if (!this.movementSensorServiceClient) {
323335
throw new Error(RobotClient.notConnectedYetStr);
@@ -582,6 +594,10 @@ export class RobotClient extends EventDispatcher implements Robot {
582594
GripperService,
583595
clientTransport
584596
);
597+
this.mlModelServiceClient = createPromiseClient(
598+
MLModelService,
599+
clientTransport
600+
);
585601
this.movementSensorServiceClient = createPromiseClient(
586602
MovementSensorService,
587603
clientTransport

src/services/ml-model/client.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
2+
import type { FlatTensors, MLModel } from './ml-model';
3+
import { Struct, type Options } from '../../types';
4+
import type { RobotClient } from '../../robot';
5+
import {
6+
InferRequest,
7+
MetadataRequest,
8+
} from '../../gen/service/mlmodel/v1/mlmodel_pb';
9+
import { MLModelService } from '../../gen/service/mlmodel/v1/mlmodel_connect';
10+
11+
export class MLModelClient implements MLModel {
12+
private client: PromiseClient<typeof MLModelService>;
13+
private readonly name: string;
14+
private readonly options: Options;
15+
public callOptions: CallOptions = { headers: {} as Record<string, string> };
16+
17+
constructor(client: RobotClient, name: string, options: Options = {}) {
18+
this.client = client.createServiceClient(MLModelService);
19+
this.name = name;
20+
this.options = options;
21+
}
22+
23+
async metadata(extra = {}, callOptions = this.callOptions) {
24+
const request = new MetadataRequest({
25+
name: this.name,
26+
extra: Struct.fromJson(extra),
27+
});
28+
29+
this.options.requestLogger?.(request);
30+
31+
return this.client.metadata(request, callOptions);
32+
}
33+
34+
async infer(
35+
inputTensors: FlatTensors,
36+
extra = {},
37+
callOptions = this.callOptions
38+
) {
39+
const request = new InferRequest({
40+
name: this.name,
41+
inputTensors,
42+
extra: Struct.fromJson(extra),
43+
});
44+
45+
this.options.requestLogger?.(request);
46+
47+
return this.client.infer(request, callOptions);
48+
}
49+
}

src/services/ml-model/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type { MLModel, FlatTensors } from './ml-model';
2+
export { MLModelClient } from './client';

src/services/ml-model/ml-model.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Struct } from '@bufbuild/protobuf';
2+
import type * as mlModelAPI from '../../gen/service/mlmodel/v1/mlmodel_pb';
3+
4+
export type FlatTensors = mlModelAPI.FlatTensors;
5+
6+
export interface MLModel {
7+
metadata: (extra?: Struct) => Promise<mlModelAPI.MetadataResponse>;
8+
9+
infer: (
10+
inputTensors: FlatTensors,
11+
extra?: Struct
12+
) => Promise<mlModelAPI.InferResponse>;
13+
}

0 commit comments

Comments
 (0)