Skip to content
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

RSDK-9621: Add Discover Service and GetModelsFromModules to Typescript #436

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions src/robot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,13 @@ export class RobotClient extends EventDispatcher implements Robot {
return resp.discovery;
}

// GET MODELS FROM MODULES

async getModelsFromModules() {
const resp = await this.robotService.getModelsFromModules();
return resp.model;
}

// GET CLOUD METADATA

async getCloudMetadata() {
Expand Down
10 changes: 10 additions & 0 deletions src/robot/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Struct } from '@bufbuild/protobuf';
import { MachineConnectionEvent } from '../events';
import type { PoseInFrame, Transform } from '../gen/common/v1/common_pb';
import type proto from '../gen/robot/v1/robot_pb';
import type { ComponentConfig } from '../gen/app/v1/robot_pb';
import type { ResourceName } from '../types';

export type CloudMetadata = proto.GetCloudMetadataResponse;
Expand Down Expand Up @@ -113,6 +114,15 @@ export interface Robot {
queries: proto.DiscoveryQuery[]
): Promise<proto.Discovery[]>;

/**
* Get the list of discovered component configurations.
*
* @group ComponentConfig
* @alpha
*/
getModelsFromModules(
): Promise<ComponentConfig[]>;

/**
* Get a list of all resources on the robot.
*
Expand Down
2 changes: 2 additions & 0 deletions src/services/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DiscoveryClient } from './discovery/client';
export type { Discovery } from './discovery/discovery';
51 changes: 51 additions & 0 deletions src/services/discovery/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @vitest-environment happy-dom

import {
createPromiseClient,
createRouterTransport,
} from '@connectrpc/connect';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { DiscoveryService } from '../../gen/service/discovery/v1/discovery_connect';
import {
DiscoverResourcesRequest,
DiscoverResourcesResponse,
} from '../../gen/service/discovery/v1/discovery_pb';
import { RobotClient } from '../../robot';
import { DiscoveryClient } from './client';
import { ComponentConfig } from '../../gen/app/v1/robot_pb';
vi.mock('../../robot');
vi.mock('../../gen/service/discovery/v1/discovery_pb_service');

const discoveryClientName = 'test-discovery';

let discovery: DiscoveryClient;

const discoveries: ComponentConfig = new ComponentConfig();

describe('DiscoveryClient Tests', () => {
beforeEach(() => {
const mockTransport = createRouterTransport(({ service }) => {
service(DiscoveryService, {
discoverResources: () =>
new DiscoverResourcesResponse({ discovery: [discoveries] }),
});
});

RobotClient.prototype.createServiceClient = vi
.fn()
.mockImplementation(() =>
createPromiseClient(DiscoveryService, mockTransport)
);
discovery = new DiscoveryClient(new RobotClient('host'), discoveryClientName);
});

describe('Discovery Resources Tests', () => {
it('returns resources from a machine', async () => {
const expected = [discoveries];

await expect(
discovery.discoverResources('discovery')
).resolves.toStrictEqual(expected);
});
});
});
61 changes: 61 additions & 0 deletions src/services/discovery/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Struct, type JsonValue } from '@bufbuild/protobuf';
import type { CallOptions, PromiseClient } from '@connectrpc/connect';
import { DiscoveryService } from '../../gen/service/discover/v1/discovery_connect';
import {
DiscoverResourcesRequest,
DiscoverResourcesResponse,
} from '../../gen/service/discovery/v1/discovery_pb';
import type { RobotClient } from '../../robot';
import { doCommandFromClient } from '../../utils';
import type { Discovery } from './discovery';

/**
* A gRPC-web client for a Vision service.
*
* @group Clients
*/
export class DiscoveryClient implements Discovery {
private client: PromiseClient<typeof DiscoveryService>;
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(DiscoveryService);
this.name = name;
this.options = options;
}

async discoverResources(
discoveryName: string,
extra = {},
callOptions = this.callOptions
) {
const request = new DiscoverResourcesRequest({
name: this.name,
discoveryName,
extra: Struct.fromJson(extra),
});

this.options.requestLogger?.(request);

const resp = await this.client.discoverResources(
request,
callOptions
);
return resp.discovery;
}

async doCommand(
command: Struct,
callOptions = this.callOptions
): Promise<JsonValue> {
return doCommandFromClient(
this.client.doCommand,
this.name,
command,
this.options,
callOptions
);
}
}
17 changes: 17 additions & 0 deletions src/services/discovery/discovery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Struct } from '@bufbuild/protobuf';
import type { Resource } from '../../types';
import type { ComponentConfig } from '../../gen/app/v1/robot_pb';

/** A service that enables various computer vision algorithms */
export interface Discovery extends Resource {
/**
* Get a list of detections in the next image given a camera.
*
* @param discoveryName - The name of the discovery service.
* @returns - The list of ComponentConfigs.
*/
discoverResources: (
discoveryName: string,
extra?: Struct
) => Promise<ComponentConfig[]>;
}
Loading