@michelangelo-ai/rpc 0.12.0 → 0.13.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import { create, createFileRegistry } from '@bufbuild/protobuf';
2
+ import {
3
+ FieldDescriptorProto_Label,
4
+ FieldDescriptorProto_Type,
5
+ file_google_protobuf_any,
6
+ FileDescriptorProtoSchema,
7
+ } from '@bufbuild/protobuf/wkt';
8
+
9
+ const { OPTIONAL, REPEATED } = FieldDescriptorProto_Label;
10
+ const { MESSAGE, STRING } = FieldDescriptorProto_Type;
11
+
12
+ /**
13
+ * Builds the registry and `Tree` schema for a test-only proto file that covers every field kind
14
+ * a schema walk handles, without depending on the generated API schemas:
15
+ *
16
+ * ```proto
17
+ * package test;
18
+ * message Leaf { string name = 1; }
19
+ * message Tree {
20
+ * Leaf leaf = 1;
21
+ * repeated Tree children = 2;
22
+ * map<string, google.protobuf.Any> extras = 3;
23
+ * string label = 4;
24
+ * google.protobuf.Any payload = 5;
25
+ * }
26
+ * ```
27
+ */
28
+ export function createTestSchemas() {
29
+ const registry = createFileRegistry(
30
+ create(FileDescriptorProtoSchema, {
31
+ name: 'test.proto',
32
+ package: 'test',
33
+ syntax: 'proto3',
34
+ dependency: ['google/protobuf/any.proto'],
35
+ messageType: [
36
+ { name: 'Leaf', field: [{ name: 'name', number: 1, type: STRING, label: OPTIONAL }] },
37
+ {
38
+ name: 'Tree',
39
+ field: [
40
+ { name: 'leaf', number: 1, type: MESSAGE, typeName: '.test.Leaf', label: OPTIONAL },
41
+ { name: 'children', number: 2, type: MESSAGE, typeName: '.test.Tree', label: REPEATED },
42
+ {
43
+ name: 'extras',
44
+ number: 3,
45
+ type: MESSAGE,
46
+ typeName: '.test.Tree.ExtrasEntry',
47
+ label: REPEATED,
48
+ },
49
+ { name: 'label', number: 4, type: STRING, label: OPTIONAL },
50
+ {
51
+ name: 'payload',
52
+ number: 5,
53
+ type: MESSAGE,
54
+ typeName: '.google.protobuf.Any',
55
+ label: OPTIONAL,
56
+ },
57
+ ],
58
+ nestedType: [
59
+ {
60
+ name: 'ExtrasEntry',
61
+ options: { mapEntry: true },
62
+ field: [
63
+ { name: 'key', number: 1, type: STRING, label: OPTIONAL },
64
+ {
65
+ name: 'value',
66
+ number: 2,
67
+ type: MESSAGE,
68
+ typeName: '.google.protobuf.Any',
69
+ label: OPTIONAL,
70
+ },
71
+ ],
72
+ },
73
+ ],
74
+ },
75
+ ],
76
+ }),
77
+ (protoFileName) =>
78
+ protoFileName === 'google/protobuf/any.proto' ? file_google_protobuf_any : undefined
79
+ );
80
+
81
+ const TreeSchema = registry.getMessage('test.Tree');
82
+ if (!TreeSchema) throw new Error('test.Tree is not in the test registry');
83
+ return { registry, TreeSchema };
84
+ }
@@ -1,5 +1,3 @@
1
- import { create } from '@bufbuild/protobuf';
2
- import { anyPack, StringValueSchema } from '@bufbuild/protobuf/wkt';
3
1
  import { beforeEach, describe, expect, it, vi } from 'vitest';
4
2
 
5
3
  import { request } from '../request';
@@ -9,10 +7,6 @@ vi.mock('../runtime-config', () => ({
9
7
  getRuntimeConfig: () => Promise.resolve({ apiBaseUrl: 'http://test' }),
10
8
  }));
11
9
 
12
- // The real createConnectTransport (JSON mode) calls response.json() and decodes
13
- // it with fromJson(..., jsonOptions). If jsonOptions.registry doesn't include
14
- // TypedStructSchema, fromJson throws on the @type URL — so removing the registry
15
- // from services.ts breaks this test.
16
10
  global.fetch = vi.fn().mockResolvedValue({
17
11
  status: 200,
18
12
  headers: new Headers({ 'content-type': 'application/json' }),
@@ -42,12 +36,10 @@ it('decodes a ListPipelineRun response containing a TypedStruct Any field', asyn
42
36
  result as unknown as { pipelineRunList: { items: { status: { details: unknown[] } }[] } }
43
37
  ).pipelineRunList.items[0].status.details;
44
38
 
45
- // The registry decodes the Any to a binary TypedStruct, and toPlainObject unpacks it to
46
- // { typeUrl, value } where typeUrl names the inner config type and value is its plain
47
- // JSON. Without TypedStructSchema in the registry, fromJson throws before reaching here.
48
- expect(details[0]).toEqual({
39
+ // Responses are returned as Envoy emits them, so the Any comes through as-is with its '@type'.
40
+ expect(details[0]).toMatchObject({
41
+ '@type': 'type.googleapis.com/michelangelo.api.TypedStruct',
49
42
  typeUrl: 'type.googleapis.com/michelangelo.UniFlowConf',
50
- value: {},
51
43
  });
52
44
  });
53
45
 
@@ -136,26 +128,19 @@ describe('outgoing request — Any-packing through the real service client', ()
136
128
  });
137
129
  });
138
130
 
139
- it('sends an already-packed Any (real typeUrl/value) through unchanged', async () => {
140
- const realAny = anyPack(
141
- StringValueSchema,
142
- create(StringValueSchema, { value: 'already-packed' })
143
- );
131
+ it('sends an already-packed Any (proto3 JSON @type form) through unchanged', async () => {
132
+ const packedAny = {
133
+ '@type': 'type.googleapis.com/google.protobuf.StringValue',
134
+ value: 'already-packed',
135
+ };
144
136
 
145
137
  await request('ListPipelineRun', {
146
138
  listOptionsExt: {
147
- operation: { criterion: [{ fieldName: 'x', operator: 1, matchValue: realAny }] },
139
+ operation: { criterion: [{ fieldName: 'x', operator: 1, matchValue: packedAny }] },
148
140
  },
149
141
  } as never);
150
142
 
151
- expectCriteria({
152
- fieldName: 'x',
153
- operator: 'CRITERION_OPERATOR_EQUAL',
154
- matchValue: {
155
- '@type': 'type.googleapis.com/google.protobuf.StringValue',
156
- value: 'already-packed',
157
- },
158
- });
143
+ expectCriteria({ fieldName: 'x', operator: 'CRITERION_OPERATOR_EQUAL', matchValue: packedAny });
159
144
  });
160
145
 
161
146
  it('packs Any values across every entry of a repeated field, and leaves fieldName untouched', async () => {
@@ -228,3 +213,111 @@ describe('outgoing request — Any-packing through the real service client', ()
228
213
  });
229
214
  });
230
215
  });
216
+
217
+ describe('outgoing request — proto3 JSON input', () => {
218
+ function lastRequestBody() {
219
+ const [, init] = vi.mocked(global.fetch).mock.calls.at(-1) as [string, RequestInit];
220
+ // cast: these tests only care about the shape they themselves constructed
221
+ return JSON.parse(init.body as string) as Record<string, Record<string, unknown>>;
222
+ }
223
+
224
+ it('accepts string enum names', async () => {
225
+ await request('CreateInferenceServer', {
226
+ metadata: { name: 'my-server' },
227
+ spec: { backendType: 'BACKEND_TYPE_TRITON' },
228
+ } as never);
229
+
230
+ expect(lastRequestBody().inferenceServer.spec).toMatchObject({
231
+ backendType: 'BACKEND_TYPE_TRITON',
232
+ });
233
+ });
234
+
235
+ it('accepts a oneof member set directly on its parent', async () => {
236
+ await request('CreateDeployment', {
237
+ metadata: { name: 'my-deployment' },
238
+ spec: { inferenceServer: { name: 'my-server', namespace: 'my-project' } },
239
+ } as never);
240
+
241
+ expect(lastRequestBody().deployment.spec).toMatchObject({
242
+ inferenceServer: { name: 'my-server', namespace: 'my-project' },
243
+ });
244
+ });
245
+
246
+ it('ignores keys that are not fields of the request message', async () => {
247
+ await request('CreateInferenceServer', {
248
+ metadata: { name: 'my-server' },
249
+ notAField: true,
250
+ } as never);
251
+
252
+ expect(lastRequestBody().inferenceServer).not.toHaveProperty('notAField');
253
+ });
254
+
255
+ it('drops metadata.deletionGracePeriodSeconds, including inside Any payloads', async () => {
256
+ await request('UpdatePipelineRun', {
257
+ metadata: { name: 'run', deletionGracePeriodSeconds: '0' },
258
+ status: {
259
+ details: [
260
+ {
261
+ '@type': 'type.googleapis.com/michelangelo.api.v2.Pipeline',
262
+ metadata: { name: 'pipeline', deletionGracePeriodSeconds: '0' },
263
+ },
264
+ ],
265
+ },
266
+ } as never);
267
+
268
+ const { pipelineRun } = lastRequestBody();
269
+ expect(pipelineRun.metadata).toEqual({ name: 'run' });
270
+ expect(pipelineRun.status).toEqual({
271
+ details: [
272
+ {
273
+ '@type': 'type.googleapis.com/michelangelo.api.v2.Pipeline',
274
+ metadata: { name: 'pipeline' },
275
+ },
276
+ ],
277
+ });
278
+ });
279
+ });
280
+
281
+ describe('Timestamp/Duration fields', () => {
282
+ function respondWith(json: unknown) {
283
+ vi.mocked(global.fetch).mockResolvedValueOnce({
284
+ status: 200,
285
+ headers: new Headers({ 'content-type': 'application/json' }),
286
+ json: () => Promise.resolve(json),
287
+ } as Response);
288
+ }
289
+
290
+ it('arrive as proto3 JSON strings', async () => {
291
+ const pipelineRun = {
292
+ metadata: { creationTimestamp: '2026-09-24T23:07:46Z' },
293
+ status: { steps: [{ startTime: '2026-09-28T23:11:19.992009180Z' }] },
294
+ };
295
+ respondWith({ pipelineRun });
296
+
297
+ const response = await request('GetPipelineRun', {} as never);
298
+
299
+ expect(response).toEqual({ pipelineRun });
300
+ });
301
+
302
+ it('are sent as proto3 JSON strings', async () => {
303
+ respondWith({});
304
+
305
+ await request('CreateTriggerRun', {
306
+ metadata: { name: 'backfill' },
307
+ spec: {
308
+ startTimestamp: '2023-11-14T22:13:20Z',
309
+ trigger: { intervalSchedule: { interval: '3600s' } },
310
+ },
311
+ } as never);
312
+
313
+ const [, init] = vi.mocked(global.fetch).mock.calls.at(-1) as [string, RequestInit];
314
+ // cast: this test only cares about the shape it itself constructed
315
+ const body = JSON.parse(init.body as string) as {
316
+ triggerRun: { spec: Record<string, unknown> };
317
+ };
318
+ expect(body.triggerRun.spec).toMatchObject({
319
+ startTimestamp: '2023-11-14T22:13:20Z',
320
+ trigger: { intervalSchedule: { interval: '3600s' } },
321
+ });
322
+ });
323
+ });
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { createTestSchemas } from '../__fixtures__/test-schemas';
4
+ import { walkMessage } from '../walk-message';
5
+
6
+ import type { MessageVisitor } from '../types';
7
+
8
+ describe('walkMessage', () => {
9
+ it('visits singular, repeated, and nested message values with their descriptors', () => {
10
+ const { TreeSchema } = createTestSchemas();
11
+ const visited: string[] = [];
12
+ const visit: MessageVisitor = (desc, value, descend) => {
13
+ visited.push(desc.typeName);
14
+ return descend(desc, value);
15
+ };
16
+
17
+ walkMessage(
18
+ TreeSchema,
19
+ { leaf: { name: 'a' }, children: [{ leaf: { name: 'b' } }], payload: 'x' },
20
+ visit
21
+ );
22
+
23
+ expect(visited).toEqual(['test.Leaf', 'test.Tree', 'test.Leaf', 'google.protobuf.Any']);
24
+ });
25
+
26
+ it('visits map values and puts the visitor result in their place', () => {
27
+ const { TreeSchema } = createTestSchemas();
28
+ const result = walkMessage(
29
+ TreeSchema,
30
+ { extras: { foo: 'bar', replicas: 3 } },
31
+ (desc, value) => `${desc.typeName}:${String(value)}`
32
+ );
33
+
34
+ expect(result).toEqual({
35
+ extras: { foo: 'google.protobuf.Any:bar', replicas: 'google.protobuf.Any:3' },
36
+ });
37
+ });
38
+
39
+ it('with a registry, walks an Any payload as the message its @type names', () => {
40
+ const { registry, TreeSchema } = createTestSchemas();
41
+ const visited: string[] = [];
42
+ const visit: MessageVisitor = (desc, value, descend) => {
43
+ visited.push(desc.typeName);
44
+ return descend(desc, value);
45
+ };
46
+ const known = { '@type': 'type.googleapis.com/test.Tree', leaf: { name: 'a' } };
47
+ const unknown = { '@type': 'type.googleapis.com/some.Unregistered', leaf: { name: 'b' } };
48
+
49
+ const result = walkMessage(TreeSchema, { extras: { known, unknown } }, visit, registry);
50
+
51
+ expect(visited).toEqual(['google.protobuf.Any', 'test.Leaf', 'google.protobuf.Any']);
52
+ expect(result).toEqual({ extras: { known, unknown } });
53
+ });
54
+
55
+ it('copies scalar fields and keys that are not fields unchanged', () => {
56
+ const { TreeSchema } = createTestSchemas();
57
+ const result = walkMessage(TreeSchema, { label: 'x', notAField: { nested: true } }, () => {
58
+ throw new Error('no message fields to visit');
59
+ });
60
+
61
+ expect(result).toEqual({ label: 'x', notAField: { nested: true } });
62
+ });
63
+ });
package/handlers.ts CHANGED
@@ -1,12 +1,9 @@
1
- import { create } from '@bufbuild/protobuf';
2
-
3
- import { UserInfoSchema } from './gen/michelangelo/api/v2/user_pb';
4
1
  import { getServices } from './services';
5
2
 
6
- import type { Deployment } from './gen/michelangelo/api/v2/deployment_pb';
7
- import type { InferenceServer } from './gen/michelangelo/api/v2/inference_server_pb';
8
- import type { PipelineRun } from './gen/michelangelo/api/v2/pipeline_run_pb';
9
- import type { TriggerRun } from './gen/michelangelo/api/v2/trigger_run_pb';
3
+ import type { DeploymentJson } from './gen/michelangelo/api/v2/deployment_pb';
4
+ import type { InferenceServerJson } from './gen/michelangelo/api/v2/inference_server_pb';
5
+ import type { PipelineRunJson } from './gen/michelangelo/api/v2/pipeline_run_pb';
6
+ import type { TriggerRunJson } from './gen/michelangelo/api/v2/trigger_run_pb';
10
7
  import type { ExtractUnaryRpc } from './types';
11
8
 
12
9
  let handlersPromise: Promise<Awaited<ReturnType<typeof createHandlers>>> | null = null;
@@ -39,24 +36,24 @@ async function createHandlers() {
39
36
  ListCluster: unary(services.ClusterService.listCluster),
40
37
  ListDeployment: unary(services.DeploymentService.listDeployment),
41
38
  GetDeployment: unary(services.DeploymentService.getDeployment),
42
- CreateDeployment: (record: Deployment, headers?: Record<string, string>) => {
39
+ CreateDeployment: (record: DeploymentJson, headers?: Record<string, string>) => {
43
40
  const actorName = headers?.['x-user-name'];
44
41
  if (actorName && record.spec) {
45
- record.spec.owner = create(UserInfoSchema, { name: actorName });
42
+ record.spec.owner = { name: actorName };
46
43
  }
47
44
  return services.DeploymentService.createDeployment({ deployment: record }, headers);
48
45
  },
49
- UpdateDeployment: (record: Deployment, headers?: Record<string, string>) => {
46
+ UpdateDeployment: (record: DeploymentJson, headers?: Record<string, string>) => {
50
47
  const actorName = headers?.['x-user-name'];
51
48
  if (actorName && record.spec) {
52
- record.spec.owner = create(UserInfoSchema, { name: actorName });
49
+ record.spec.owner = { name: actorName };
53
50
  }
54
51
  return services.DeploymentService.updateDeployment({ deployment: record }, headers);
55
52
  },
56
53
  DeleteDeployment: deleteCrd(services.DeploymentService.deleteDeployment),
57
54
  ListInferenceServer: unary(services.InferenceServerService.listInferenceServer),
58
55
  GetInferenceServer: unary(services.InferenceServerService.getInferenceServer),
59
- CreateInferenceServer: (record: InferenceServer, headers?: Record<string, string>) =>
56
+ CreateInferenceServer: (record: InferenceServerJson, headers?: Record<string, string>) =>
60
57
  services.InferenceServerService.createInferenceServer({ inferenceServer: record }, headers),
61
58
  ListProject: unary(services.ProjectService.listProject),
62
59
  GetProject: unary(services.ProjectService.getProject),
@@ -67,23 +64,23 @@ async function createHandlers() {
67
64
  GetPipelineRun: unary(services.PipelineRunService.getPipelineRun),
68
65
  ListTriggerRun: unary(services.TriggerRunService.listTriggerRun),
69
66
  GetTriggerRun: unary(services.TriggerRunService.getTriggerRun),
70
- CreateTriggerRun: (record: TriggerRun, headers?: Record<string, string>) => {
67
+ CreateTriggerRun: (record: TriggerRunJson, headers?: Record<string, string>) => {
71
68
  const actorName = headers?.['x-user-name'];
72
69
  if (actorName && record.spec) {
73
- record.spec.actor = create(UserInfoSchema, { name: actorName });
70
+ record.spec.actor = { name: actorName };
74
71
  }
75
72
  return services.TriggerRunService.createTriggerRun({ triggerRun: record }, headers);
76
73
  },
77
- UpdateTriggerRun: (record: TriggerRun, headers?: Record<string, string>) =>
74
+ UpdateTriggerRun: (record: TriggerRunJson, headers?: Record<string, string>) =>
78
75
  services.TriggerRunService.updateTriggerRun({ triggerRun: record }, headers),
79
- CreatePipelineRun: (record: PipelineRun, headers?: Record<string, string>) => {
76
+ CreatePipelineRun: (record: PipelineRunJson, headers?: Record<string, string>) => {
80
77
  const actorName = headers?.['x-user-name'];
81
78
  if (actorName && record.spec) {
82
- record.spec.actor = create(UserInfoSchema, { name: actorName });
79
+ record.spec.actor = { name: actorName };
83
80
  }
84
81
  return services.PipelineRunService.createPipelineRun({ pipelineRun: record }, headers);
85
82
  },
86
- UpdatePipelineRun: (record: PipelineRun, headers?: Record<string, string>) =>
83
+ UpdatePipelineRun: (record: PipelineRunJson, headers?: Record<string, string>) =>
87
84
  services.PipelineRunService.updatePipelineRun({ pipelineRun: record }, headers),
88
85
  ListModel: unary(services.ModelService.listModel),
89
86
  GetModel: unary(services.ModelService.getModel),
@@ -0,0 +1,32 @@
1
+ import { ObjectMetaSchema } from './gen/k8s.io/apimachinery/pkg/apis/meta/v1/generated_pb';
2
+ import { walkMessage } from './walk-message';
3
+
4
+ import type { DescMessage, Registry } from '@bufbuild/protobuf';
5
+
6
+ /**
7
+ * Removes `deletionGracePeriodSeconds` from every Kubernetes object metadata in a request,
8
+ * including metadata inside Any payloads (e.g. Revision content).
9
+ *
10
+ * Envoy prints default values into responses (`always_print_primitive_fields`), and that
11
+ * includes this unset proto2 field as `"0"`. Sending a fetched record back on update would
12
+ * then set it explicitly, which the API server rejects as a change to an immutable field.
13
+ * The server owns this field, so requests never need to carry it.
14
+ */
15
+ export function omitDeletionGracePeriod(
16
+ desc: DescMessage,
17
+ value: unknown,
18
+ registry: Registry
19
+ ): unknown {
20
+ return walkMessage(
21
+ desc,
22
+ value,
23
+ (message, item, descend) => {
24
+ if (message.typeName !== ObjectMetaSchema.typeName) return descend(message, item);
25
+ if (item === null || typeof item !== 'object') return item;
26
+ // cast: object metadata in proto3 JSON is a plain object
27
+ const { deletionGracePeriodSeconds: _omitted, ...rest } = item as Record<string, unknown>;
28
+ return rest;
29
+ },
30
+ registry
31
+ );
32
+ }
@@ -1,92 +1,57 @@
1
- import { create } from '@bufbuild/protobuf';
2
1
  import {
3
- anyPack,
4
2
  BoolValueSchema,
5
3
  DoubleValueSchema,
6
4
  Int64ValueSchema,
7
5
  StringValueSchema,
8
6
  } from '@bufbuild/protobuf/wkt';
9
7
 
10
- import type { DescField, DescMessage } from '@bufbuild/protobuf';
8
+ import { walkMessage } from './walk-message';
9
+
10
+ import type { DescMessage } from '@bufbuild/protobuf';
11
11
 
12
12
  const ANY_TYPE_NAME = 'google.protobuf.Any';
13
+ const TYPE_URL_PREFIX = 'type.googleapis.com/';
13
14
 
14
15
  /**
15
- * Walks a request object against its proto descriptor, packing JS primitives into well-known
16
- * wrapper types wherever the schema has a `google.protobuf.Any` field.
16
+ * Walks a proto3 JSON request object against its proto descriptor, packing JS primitives into
17
+ * the JSON form of well-known wrapper types wherever the schema has a `google.protobuf.Any` field.
17
18
  *
18
19
  * @example
19
20
  * packAnyFields(CriterionSchema, { fieldName: "x", matchValue: "my-pipeline" })
20
- * // matchValue -> anyPack(StringValueSchema, create(StringValueSchema, { value: "my-pipeline" }))
21
+ * // matchValue -> { "@type": "type.googleapis.com/google.protobuf.StringValue", value: "my-pipeline" }
21
22
  */
22
23
  export function packAnyFields(desc: DescMessage, value: unknown): unknown {
23
- if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
24
-
25
- const result: Record<string, unknown> = {};
26
- for (const [key, val] of Object.entries(value)) {
27
- const field: DescField | undefined = desc.field[key];
28
- result[key] = field ? packField(field, val) : val;
29
- }
30
- return result;
31
- }
32
-
33
- function packField(field: DescField, value: unknown): unknown {
34
- if (value === null || value === undefined) return value;
35
-
36
- switch (field.fieldKind) {
37
- case 'message':
38
- return packMessageValue(field.message, value);
39
- case 'list':
40
- if (field.listKind === 'message') {
41
- // cast: repeated fields are always arrays at runtime
42
- return (value as unknown[]).map((item) => packMessageValue(field.message, item));
43
- }
44
- return value;
45
- case 'map': {
46
- const mapValueMessage = field.message;
47
- if (!mapValueMessage) return value;
48
- // cast: map fields are always plain objects at runtime, keyed by the (stringified)
49
- // map key regardless of its declared scalar type
50
- const mapValue = value as Record<string, unknown>;
51
- return Object.fromEntries(
52
- Object.entries(mapValue).map(([key, item]) => [
53
- key,
54
- packMessageValue(mapValueMessage, item),
55
- ])
56
- );
57
- }
58
- default:
59
- return value;
60
- }
24
+ return walkMessage(desc, value, (message, item, descend) =>
25
+ message.typeName === ANY_TYPE_NAME ? packAny(item) : descend(message, item)
26
+ );
61
27
  }
62
28
 
63
- function packMessageValue(desc: DescMessage, value: unknown): unknown {
64
- if (desc.typeName !== ANY_TYPE_NAME) {
65
- return packAnyFields(desc, value);
66
- }
67
-
68
- // Already a packed Any object (has typeUrl) — pass through
69
- if (typeof value === 'object' && value !== null && 'typeUrl' in value) {
29
+ function packAny(value: unknown): unknown {
30
+ // Already a proto3 JSON Any (has @type) — pass through
31
+ if (typeof value === 'object' && value !== null && '@type' in value) {
70
32
  return value;
71
33
  }
72
34
 
73
35
  if (typeof value === 'string') {
74
- return anyPack(StringValueSchema, create(StringValueSchema, { value }));
36
+ return packWrapper(StringValueSchema, value);
75
37
  }
76
38
  if (typeof value === 'boolean') {
77
- return anyPack(BoolValueSchema, create(BoolValueSchema, { value }));
39
+ return packWrapper(BoolValueSchema, value);
78
40
  }
79
41
  if (typeof value === 'number') {
80
42
  if (Number.isInteger(value)) {
81
- return anyPack(Int64ValueSchema, create(Int64ValueSchema, { value: BigInt(value) }));
43
+ // proto3 JSON encodes int64 as a string
44
+ return packWrapper(Int64ValueSchema, String(value));
82
45
  }
83
- return anyPack(DoubleValueSchema, create(DoubleValueSchema, { value }));
46
+ return packWrapper(DoubleValueSchema, value);
84
47
  }
85
48
 
86
- // create() doesn't validate an Any's shape, so passing this through would silently produce
87
- // an empty Any (typeUrl: '') instead of a visible error.
88
49
  throw new Error(
89
50
  `packAnyFields: cannot auto-pack ${typeof value} into google.protobuf.Any — ` +
90
51
  `expected a string, number, or boolean primitive`
91
52
  );
92
53
  }
54
+
55
+ function packWrapper(schema: DescMessage, value: unknown) {
56
+ return { '@type': `${TYPE_URL_PREFIX}${schema.typeName}`, value };
57
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@michelangelo-ai/rpc",
3
3
  "license": "Apache-2.0",
4
4
  "type": "module",
5
- "version": "0.12.0",
5
+ "version": "0.13.0-rc.2",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/michelangelo-ai/michelangelo"
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@bufbuild/protobuf": "^2.2.5",
20
- "@michelangelo-ai/core": "0.12.0"
20
+ "@michelangelo-ai/core": "0.13.0-rc.2"
21
21
  },
22
22
  "exports": {
23
23
  ".": {
package/request.ts CHANGED
@@ -1,22 +1,16 @@
1
- import { fromBinary, toJson } from '@bufbuild/protobuf';
2
-
3
- import { TypedStructSchema } from './gen/michelangelo/api/typed_struct_pb';
4
1
  import { getRpcHandlers } from './handlers';
5
- import { typeRegistry } from './services';
6
2
 
7
- import type { Any } from '@bufbuild/protobuf/wkt';
8
- import type { OmitTypeName, RpcHandlerType } from './types';
3
+ import type { RpcHandlerType } from './types';
9
4
 
10
5
  /**
11
6
  * Makes a gRPC-web request to the Michelangelo API.
12
7
  *
13
- * Responses are decoded into plain objects — protobuf internals ($typeName, $unknown)
14
- * are stripped recursively.
8
+ * Responses are proto3 JSON as Envoy's grpc_json_transcoder emits it.
15
9
  *
16
10
  * @param rpcId - The ID of the RPC handler to call.
17
11
  * @param args - The arguments to pass to the RPC handler.
18
12
  * @param headers - Optional HTTP headers to send with the request (e.g. user identity).
19
- * @returns A promise that resolves to the RPC response as a plain object.
13
+ * @returns The RPC response.
20
14
  *
21
15
  * @example
22
16
  * ```ts
@@ -27,60 +21,13 @@ import type { OmitTypeName, RpcHandlerType } from './types';
27
21
  */
28
22
  export async function request<RpcId extends keyof RpcHandlerType>(
29
23
  rpcId: RpcId,
30
- args: OmitTypeName<Parameters<RpcHandlerType[RpcId]>[0]>,
24
+ args: Parameters<RpcHandlerType[RpcId]>[0],
31
25
  headers?: Record<string, string>
32
- ): Promise<OmitTypeName<Awaited<ReturnType<RpcHandlerType[RpcId]>>>> {
26
+ ): Promise<Awaited<ReturnType<RpcHandlerType[RpcId]>>> {
33
27
  const handlers = await getRpcHandlers();
34
28
  // cast: dynamic key lookup on handlers loses the specific RPC signature; we know RpcId is a valid
35
29
  // key with matching handler shape
36
30
  const handler = handlers[rpcId] as (a: unknown, h?: Record<string, string>) => Promise<unknown>;
37
31
  // cast: handler returns unknown via dynamic dispatch; RpcId determines the concrete return type
38
- const response = (await handler(args, headers)) as Awaited<ReturnType<RpcHandlerType[RpcId]>>;
39
- // cast: toPlainObject returns unknown; RpcId determines the proto shape after stripping $typeName
40
- // fields
41
- return toPlainObject(response) as OmitTypeName<Awaited<ReturnType<RpcHandlerType[RpcId]>>>;
42
- }
43
-
44
- // Recursively strips protobuf internals ($typeName, $unknown) from a response,
45
- // returning a plain object tree that is safe to spread and reconstruct into new requests.
46
- function toPlainObject(value: unknown): unknown {
47
- if (value === null || typeof value !== 'object') return value;
48
- if (value instanceof Uint8Array) return value; // preserve bytes fields (e.g. google.protobuf.Any.value)
49
- if (Array.isArray(value)) return value.map(toPlainObject);
50
-
51
- const unpacked = unpackAny(value);
52
- if (unpacked !== undefined) return unpacked;
53
-
54
- const result: Record<string, unknown> = {};
55
- for (const [key, val] of Object.entries(value)) {
56
- if (key === '$typeName' || key === '$unknown') continue;
57
- result[key] = toPlainObject(val);
58
- }
59
- return result;
60
- }
61
-
62
- const TYPE_URL_PREFIX = 'type.googleapis.com/';
63
- const TYPED_STRUCT_TYPE_URL = `${TYPE_URL_PREFIX}${TypedStructSchema.typeName}`;
64
-
65
- // google.protobuf.Any fields survive fromJson as { typeUrl, value: Uint8Array } — the binary
66
- // payload is useless to consumers.
67
- //
68
- // - A michelangelo.api.TypedStruct payload (e.g. PipelineManifest.content) expands to
69
- // { typeUrl, value } where typeUrl names the inner config type and value is its plain JSON.
70
- // - Any other payload whose type is in the RPC type registry (e.g. a Pipeline inside
71
- // Revision.spec.content) is decoded and flattened into a plain object shaped exactly like
72
- // that message would be if it arrived as a top-level response, so column paths such as
73
- // `spec.content.spec.type` read the same values (numeric enums, Timestamp objects) as they
74
- // would on the base resource.
75
- // - Unregistered payloads are left untouched.
76
- function unpackAny(value: object): unknown {
77
- if (!('$typeName' in value) || value.$typeName !== 'google.protobuf.Any') return undefined;
78
- // cast: the $typeName check above identifies this as a google.protobuf.Any message
79
- const any = value as Any;
80
- if (any.typeUrl === TYPED_STRUCT_TYPE_URL) {
81
- return toJson(TypedStructSchema, fromBinary(TypedStructSchema, any.value));
82
- }
83
- const schema = typeRegistry.getMessage(any.typeUrl.replace(TYPE_URL_PREFIX, ''));
84
- if (!schema) return undefined;
85
- return toPlainObject(fromBinary(schema, any.value));
32
+ return (await handler(args, headers)) as Awaited<ReturnType<RpcHandlerType[RpcId]>>;
86
33
  }
package/services.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { create, createRegistry, fromJson, toJson } from '@bufbuild/protobuf';
1
+ import { createRegistry, fromJson, toJson } from '@bufbuild/protobuf';
2
2
  import {
3
3
  BoolValueSchema,
4
4
  DoubleValueSchema,
@@ -19,17 +19,17 @@ import { PipelineService } from './gen/michelangelo/api/v2/pipeline_svc_pb';
19
19
  import { ProjectService } from './gen/michelangelo/api/v2/project_svc_pb';
20
20
  import { RevisionService } from './gen/michelangelo/api/v2/revision_svc_pb';
21
21
  import { TriggerRunService } from './gen/michelangelo/api/v2/trigger_run_svc_pb';
22
+ import { omitDeletionGracePeriod } from './omit-deletion-grace-period';
22
23
  import { packAnyFields } from './pack-any-fields';
23
24
  import { getRuntimeConfig } from './runtime-config';
24
25
 
25
- import type { DescService } from '@bufbuild/protobuf';
26
+ import type { DescService, JsonValue } from '@bufbuild/protobuf';
26
27
  import type { FetchTransport, ServiceClient, Services } from './types';
27
28
 
28
- // Every message type that can appear inside a google.protobuf.Any on the wire must be
29
- // registered here — protobuf-es resolves an Any's typeUrl against this registry both when
30
- // JSON-encoding requests (ListOptionsExt criteria packed by packAnyFields) and when decoding
31
- // responses (fromJson throws on an unregistered typeUrl). PipelineSchema covers
32
- // Revision.spec.content for Pipeline revisions.
29
+ // Every message type that can appear inside a google.protobuf.Any must be registered here:
30
+ // requests with an unregistered `@type` fail (fromJson/toJson throw). The wrapper types cover
31
+ // packed ListOptionsExt criteria; PipelineSchema covers Revision.spec.content for Pipeline
32
+ // revisions.
33
33
  export const typeRegistry = createRegistry(
34
34
  TypedStructSchema,
35
35
  PipelineSchema,
@@ -40,10 +40,8 @@ export const typeRegistry = createRegistry(
40
40
  );
41
41
 
42
42
  /**
43
- * Builds a service client whose methods JSON-encode the request, POST it
44
- * through the fetch transport, and decode the JSON response back into a
45
- * protobuf-es message. Envoy's grpc_json_transcoder handles the JSON<->binary
46
- * conversion on the wire, so this client only ever sees JSON.
43
+ * Builds a service client that sends and receives proto3 JSON. Responses are returned as
44
+ * Envoy's grpc_json_transcoder emits them, default values included.
47
45
  */
48
46
  function createServiceClient<T extends DescService>(
49
47
  service: T,
@@ -58,18 +56,18 @@ function createServiceClient<T extends DescService>(
58
56
  if (method.methodKind !== 'unary') continue;
59
57
 
60
58
  client[method.localName] = async (request, headers) => {
61
- // cast: packAnyFields recurses generically over `unknown`; called with a Record it
62
- // returns one, just with Any fields packed into the shape create() expects
63
- const packedRequest = packAnyFields(method.input, request) as Record<string, unknown>;
64
- const message = create(method.input, packedRequest);
59
+ const withoutGracePeriod = omitDeletionGracePeriod(method.input, request, typeRegistry);
60
+ // cast: each step returns the JSON object it was given, rewritten
61
+ const packedRequest = packAnyFields(method.input, withoutGracePeriod) as JsonValue;
62
+ // Parsing and re-serializing checks every value against its field type and drops keys
63
+ // that aren't fields. Callers pass whole records back (e.g. form state), so unknown keys
64
+ // are expected rather than an error.
65
+ const message = fromJson(method.input, packedRequest, {
66
+ registry: typeRegistry,
67
+ ignoreUnknownFields: true,
68
+ });
65
69
  const requestJson = toJson(method.input, message, { registry: typeRegistry });
66
- const responseJson = await transport.callUnary(
67
- service.typeName,
68
- method.name,
69
- requestJson,
70
- headers
71
- );
72
- return fromJson(method.output, responseJson, { registry: typeRegistry });
70
+ return transport.callUnary(service.typeName, method.name, requestJson, headers);
73
71
  };
74
72
  }
75
73
 
package/types.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import type {
2
+ DescMessage,
2
3
  DescMethodUnary,
3
4
  DescService,
4
5
  JsonValue,
5
- Message,
6
- MessageInitShape,
7
- MessageShape,
6
+ MessageJsonType,
8
7
  } from '@bufbuild/protobuf';
9
8
  import type { ClusterService } from './gen/michelangelo/api/v2/cluster_svc_pb';
10
9
  import type { DeploymentService } from './gen/michelangelo/api/v2/deployment_svc_pb';
@@ -54,11 +53,12 @@ export interface FetchTransport {
54
53
 
55
54
  /**
56
55
  * Maps a service's generated method descriptors to a client object shaped
57
- * like Connect's `Client<T>` — one async function per unary RPC.
56
+ * like Connect's `Client<T>`: one async function per unary RPC, taking and
57
+ * returning the generated `FooJson` type for its messages.
58
58
  */
59
59
  export type ServiceClient<T extends DescService> = {
60
60
  [K in keyof T['method']]: T['method'][K] extends DescMethodUnary<infer I, infer O>
61
- ? (request: MessageInitShape<I>, headers?: Record<string, string>) => Promise<MessageShape<O>>
61
+ ? (request: MessageJsonType<I>, headers?: Record<string, string>) => Promise<MessageJsonType<O>>
62
62
  : never;
63
63
  };
64
64
 
@@ -104,36 +104,11 @@ export type ExtractUnaryRpc<T> = T extends (
104
104
  : never;
105
105
 
106
106
  /**
107
- * @description
108
- * Removes the `$typeName` and `$unknown` properties from a message. These are properties
109
- * that are added by the protobuf-es library. We don't need them for our RPC calls.
110
- *
111
- * @example
112
- * ```ts
113
- * type MyMessage = {
114
- * $typeName: string;
115
- * $unknown: unknown;
116
- * myField: string;
117
- * };
118
- *
119
- * type MyMessageWithoutTypeName = OmitTypeName<MyMessage>;
120
- * const message: MyMessageWithoutTypeName = { myField: 'hello' };
121
- * ```
122
- *
123
- * @see https://github.com/bufbuild/protobuf-es/issues/1016
107
+ * Called by `walkMessage` on each message-typed value it reaches. Returns the value to put in
108
+ * its place; call `descend` to walk the message's own fields instead of replacing it.
124
109
  */
125
- export type OmitTypeName<T> = {
126
- [P in keyof T as P extends '$typeName' | '$unknown' ? never : P]: Recurse<T[P]>;
127
- };
128
-
129
- type Recurse<F> = F extends (infer U)[]
130
- ? Recurse<U>[]
131
- : F extends Message
132
- ? OmitTypeName<F>
133
- : F extends { case: infer C extends string; value: infer V extends Message }
134
- ? { case: C; value: OmitTypeName<V> }
135
- : F extends Record<string, infer V extends Message>
136
- ? Record<string, OmitTypeName<V>>
137
- : F extends Record<number, infer V extends Message>
138
- ? Record<number, OmitTypeName<V>>
139
- : F;
110
+ export type MessageVisitor = (
111
+ desc: DescMessage,
112
+ value: unknown,
113
+ descend: (desc: DescMessage, value: unknown) => unknown
114
+ ) => unknown;
@@ -0,0 +1,76 @@
1
+ import type { DescField, DescMessage, Registry } from '@bufbuild/protobuf';
2
+ import type { MessageVisitor } from './types';
3
+
4
+ const ANY_TYPE_NAME = 'google.protobuf.Any';
5
+
6
+ /**
7
+ * Copies a JSON object shaped like `desc`, passing every nested message value (singular,
8
+ * repeated, or map) through `visit`. Keys that aren't fields of `desc` are copied unchanged.
9
+ *
10
+ * With a `registry`, descending into a proto3 JSON `Any` walks its payload fields as the
11
+ * message its `@type` names. An `Any` whose type isn't in the registry is copied unchanged.
12
+ *
13
+ * @example
14
+ * // Replace every Any in a request; walk into all other messages
15
+ * walkMessage(ListPipelineRunRequestSchema, request, (desc, value, descend) =>
16
+ * desc.typeName === 'google.protobuf.Any' ? packAny(value) : descend(desc, value)
17
+ * );
18
+ */
19
+ export function walkMessage(
20
+ desc: DescMessage,
21
+ value: unknown,
22
+ visit: MessageVisitor,
23
+ registry?: Registry
24
+ ): unknown {
25
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
26
+
27
+ if (registry && desc.typeName === ANY_TYPE_NAME) {
28
+ // cast: an Any in proto3 JSON is an object whose @type names the payload message
29
+ const typeUrl = (value as { '@type'?: unknown })['@type'];
30
+ const payload =
31
+ typeof typeUrl === 'string' ? registry.getMessage(typeUrl.replace(/^.*\//, '')) : undefined;
32
+ return payload ? walkMessage(payload, value, visit, registry) : value;
33
+ }
34
+
35
+ const descend = (message: DescMessage, item: unknown) =>
36
+ walkMessage(message, item, visit, registry);
37
+ const visitValue = (message: DescMessage, item: unknown) => visit(message, item, descend);
38
+
39
+ const result: Record<string, unknown> = {};
40
+ for (const [key, val] of Object.entries(value)) {
41
+ const field: DescField | undefined = desc.field[key];
42
+ result[key] = field ? walkField(field, val, visitValue) : val;
43
+ }
44
+ return result;
45
+ }
46
+
47
+ function walkField(
48
+ field: DescField,
49
+ value: unknown,
50
+ visitValue: (message: DescMessage, item: unknown) => unknown
51
+ ): unknown {
52
+ if (value === null || value === undefined) return value;
53
+
54
+ switch (field.fieldKind) {
55
+ case 'message':
56
+ return visitValue(field.message, value);
57
+ case 'list':
58
+ if (field.listKind !== 'message') return value;
59
+ // cast: repeated fields are always arrays at runtime
60
+ return (value as unknown[]).map((item) => visitValue(field.message, item));
61
+ case 'map': {
62
+ const mapValueMessage = field.message;
63
+ if (!mapValueMessage) return value;
64
+ return Object.fromEntries(
65
+ // cast: map fields are always plain objects at runtime, keyed by the (stringified)
66
+ // map key regardless of its declared scalar type
67
+ Object.entries(value as Record<string, unknown>).map(([key, item]) => [
68
+ key,
69
+ visitValue(mapValueMessage, item),
70
+ ])
71
+ );
72
+ }
73
+ default:
74
+ return value;
75
+ }
76
+ }
@@ -1,153 +0,0 @@
1
- import { create, toBinary } from '@bufbuild/protobuf';
2
- import { anyPack } from '@bufbuild/protobuf/wkt';
3
- import { expect, it, vi } from 'vitest';
4
-
5
- import { TypedStructSchema } from '../gen/michelangelo/api/typed_struct_pb';
6
- import {
7
- PipelineManifest_Type,
8
- PipelineSchema,
9
- PipelineType,
10
- } from '../gen/michelangelo/api/v2/pipeline_pb';
11
- import { request } from '../request';
12
-
13
- vi.mock('../handlers', () => ({
14
- getRpcHandlers: vi.fn(),
15
- }));
16
-
17
- const { getRpcHandlers } = await import('../handlers');
18
- const mockGetRpcHandlers = getRpcHandlers as ReturnType<typeof vi.fn>;
19
-
20
- function mockHandler(response: unknown) {
21
- mockGetRpcHandlers.mockResolvedValue({
22
- GetPipelineRun: vi.fn().mockResolvedValue(response),
23
- });
24
- }
25
-
26
- it('strips $typeName and $unknown from response', async () => {
27
- mockHandler({ $typeName: 'foo.Bar', $unknown: [], name: 'test' });
28
-
29
- expect(await request('GetPipelineRun', {} as never)).toEqual({ name: 'test' });
30
- });
31
-
32
- it('recursively strips protobuf internals from nested objects', async () => {
33
- mockHandler({
34
- $typeName: 'outer',
35
- nested: { $typeName: 'inner', value: 1 },
36
- });
37
-
38
- expect(await request('GetPipelineRun', {} as never)).toEqual({ nested: { value: 1 } });
39
- });
40
-
41
- it('preserves Uint8Array fields without corrupting them into plain objects', async () => {
42
- const bytes = new Uint8Array([1, 2, 3]);
43
- mockHandler({ $typeName: 'foo.Any', typeUrl: 'type.googleapis.com/foo', value: bytes });
44
-
45
- const result = await request('GetPipelineRun', {} as never);
46
-
47
- expect((result as { value: Uint8Array }).value).toBeInstanceOf(Uint8Array);
48
- expect((result as { value: Uint8Array }).value).toEqual(bytes);
49
- });
50
-
51
- it('handles arrays containing objects with protobuf internals', async () => {
52
- mockHandler({
53
- items: [
54
- { $typeName: 'foo', x: 1 },
55
- { $typeName: 'bar', x: 2 },
56
- ],
57
- });
58
-
59
- expect(await request('GetPipelineRun', {} as never)).toEqual({ items: [{ x: 1 }, { x: 2 }] });
60
- });
61
-
62
- it('unpacks a registered Any payload (Pipeline) into a plain object', async () => {
63
- const pipeline = create(PipelineSchema, {
64
- metadata: { name: 'my-pipeline' },
65
- spec: { type: PipelineType.DATA_PREP, commit: { branch: 'main' } },
66
- });
67
- mockHandler({
68
- $typeName: 'michelangelo.api.v2.Revision',
69
- spec: {
70
- $typeName: 'michelangelo.api.v2.RevisionSpec',
71
- revisionId: 'abc',
72
- content: {
73
- $typeName: 'google.protobuf.Any',
74
- typeUrl: 'type.googleapis.com/michelangelo.api.v2.Pipeline',
75
- value: toBinary(PipelineSchema, pipeline),
76
- },
77
- },
78
- });
79
-
80
- const result = (await request('GetPipelineRun', {} as never)) as {
81
- spec: {
82
- content: { metadata: { name: string }; spec: { type: number; commit: { branch: string } } };
83
- };
84
- };
85
-
86
- expect(result.spec.content.metadata.name).toBe('my-pipeline');
87
- expect(result.spec.content.spec.type).toBe(PipelineType.DATA_PREP);
88
- expect(result.spec.content.spec.commit.branch).toBe('main');
89
- expect(result.spec.content).not.toHaveProperty('$typeName');
90
- });
91
-
92
- it('unpacks a registered Any payload whose own fields contain a nested TypedStruct Any', async () => {
93
- // The Pipeline packed into Revision.spec.content (a registry-typed Any) has its own
94
- // manifest.content field, which is a TypedStruct. Both must unpack in one pass: toPlainObject
95
- // recurses into the decoded Pipeline and finds the inner Any too.
96
- const manifestContent = anyPack(
97
- TypedStructSchema,
98
- create(TypedStructSchema, {
99
- typeUrl: 'type.googleapis.com/michelangelo.pipeline.dataprep.Config',
100
- value: { source: 'hive' },
101
- })
102
- );
103
- const pipeline = create(PipelineSchema, {
104
- metadata: { name: 'my-pipeline' },
105
- spec: {
106
- type: PipelineType.DATA_PREP,
107
- commit: { branch: 'main' },
108
- manifest: {
109
- type: PipelineManifest_Type.PIPELINE_MANIFEST_TYPE_YAML,
110
- content: manifestContent,
111
- },
112
- },
113
- });
114
- mockHandler({
115
- $typeName: 'michelangelo.api.v2.Revision',
116
- spec: {
117
- $typeName: 'michelangelo.api.v2.RevisionSpec',
118
- revisionId: 'abc',
119
- content: {
120
- $typeName: 'google.protobuf.Any',
121
- typeUrl: 'type.googleapis.com/michelangelo.api.v2.Pipeline',
122
- value: toBinary(PipelineSchema, pipeline),
123
- },
124
- },
125
- });
126
-
127
- const result = (await request('GetPipelineRun', {} as never)) as {
128
- spec: {
129
- content: {
130
- spec: { manifest: { content: { typeUrl: string; value: { source: string } } } };
131
- };
132
- };
133
- };
134
-
135
- expect(result.spec.content.spec.manifest.content).toEqual({
136
- typeUrl: 'type.googleapis.com/michelangelo.pipeline.dataprep.Config',
137
- value: { source: 'hive' },
138
- });
139
- });
140
-
141
- it('leaves an unregistered Any payload untouched', async () => {
142
- const bytes = new Uint8Array([9, 9]);
143
- mockHandler({
144
- $typeName: 'google.protobuf.Any',
145
- typeUrl: 'type.googleapis.com/unknown.Type',
146
- value: bytes,
147
- });
148
-
149
- expect(await request('GetPipelineRun', {} as never)).toEqual({
150
- typeUrl: 'type.googleapis.com/unknown.Type',
151
- value: bytes,
152
- });
153
- });