@highstate/contract 0.21.1 → 0.26.0

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.
package/dist/index.js CHANGED
@@ -256,6 +256,22 @@ function isEntityIncludeRef(value) {
256
256
  const entity = value.entity;
257
257
  return typeof entity === "function" || isEntity(entity);
258
258
  }
259
+ var objectEntity = {
260
+ type: "system.object.v1",
261
+ schema: z2.object().loose(),
262
+ model: {
263
+ type: "system.object.v1",
264
+ definitionHash: 0,
265
+ schema: z2.object().loose().toJSONSchema(),
266
+ meta: {
267
+ title: "Object",
268
+ description: "The common ancestor of all entities. Any entity can be assigned to this entity.",
269
+ color: "#9e9e9e",
270
+ icon: "mdi:cube",
271
+ iconColor: "#9e9e9e"
272
+ }
273
+ }
274
+ };
259
275
  function defineEntity(options) {
260
276
  try {
261
277
  entityModelSchema.shape.type.parse(options.type);
@@ -292,28 +308,37 @@ function defineEntity(options) {
292
308
  shape[includeRef.field] = schema;
293
309
  return shape;
294
310
  }, {});
295
- let finalSchema = Object.values(options.extends ?? {}).reduce((schema, entity) => z2.intersection(schema, entity.schema), options.schema);
311
+ const inheritedExtensions = [...Object.values(options.extends ?? {}), objectEntity].filter((entity) => entity.type !== options.type);
312
+ const uniqueExtensions = Array.from(new Map(inheritedExtensions.map((entity) => [entity.type, entity])).values());
313
+ const schemaExtensions = uniqueExtensions.filter((entity) => entity.type !== objectEntity.type);
314
+ const strictIntersectionBase = (schema) => {
315
+ if (schema instanceof z2.ZodObject) {
316
+ return schema.strict();
317
+ }
318
+ return schema;
319
+ };
320
+ let finalSchema = schemaExtensions.reduce((schema, entity) => z2.intersection(schema, strictIntersectionBase(entity.schema)), strictIntersectionBase(options.schema));
296
321
  if (includeRefs.length > 0) {
297
- finalSchema = z2.intersection(finalSchema, z2.object(inclusionShape));
322
+ finalSchema = z2.intersection(finalSchema, strictIntersectionBase(z2.object(inclusionShape)));
298
323
  }
299
- finalSchema = z2.intersection(finalSchema, entityWithMetaSchema);
324
+ finalSchema = z2.intersection(finalSchema, strictIntersectionBase(entityWithMetaSchema));
300
325
  const directInclusions = () => includeRefs.map((includeRef) => ({
301
326
  type: includeRef.getEntity().type,
302
327
  required: includeRef.required,
303
328
  multiple: includeRef.multiple,
304
329
  field: includeRef.field
305
330
  }));
306
- const directExtensions = Object.values(options.extends ?? {}).map((entity) => entity.type);
331
+ const directExtensions = uniqueExtensions.map((entity) => entity.type);
307
332
  const getInclusions = () => {
308
333
  const incs = [...directInclusions()];
309
- for (const entity of Object.values(options.extends ?? {})) {
334
+ for (const entity of uniqueExtensions) {
310
335
  if (entity.model.inclusions) {
311
336
  incs.push(...entity.model.inclusions);
312
337
  }
313
338
  }
314
339
  return incs;
315
340
  };
316
- const extensions = Object.values(options.extends ?? {}).reduce((exts, entity) => {
341
+ const extensions = uniqueExtensions.reduce((exts, entity) => {
317
342
  exts.push(...entity.model.extensions ?? [], entity.type);
318
343
  return exts;
319
344
  }, []);
@@ -1416,9 +1441,65 @@ var unitWorkerSchema = z11.object({
1416
1441
  var workerRunOptionsSchema = z11.object({
1417
1442
  projectId: z11.cuid2(),
1418
1443
  workerVersionId: z11.cuid2(),
1444
+ workerInstanceId: z11.cuid2(),
1419
1445
  apiUrl: z11.url(),
1446
+ dataEndpoint: z11.string().min(1),
1420
1447
  apiKey: z11.string()
1421
1448
  });
1449
+ // src/runtime.ts
1450
+ import { z as z12 } from "zod";
1451
+ var runtimeConfigGetInputSchema = z12.void();
1452
+ var runtimeConfigGetOutputSchema = unitConfigSchema;
1453
+ var runtimeResultSubmitInputSchema = z12.record(z12.string(), z12.unknown());
1454
+ var runtimeResultSubmitOutputSchema = z12.object({});
1455
+ var runtimeSidecarFileSchema = z12.object({
1456
+ path: z12.string(),
1457
+ content: z12.string(),
1458
+ secret: z12.boolean().default(false),
1459
+ mode: z12.number().int().optional()
1460
+ });
1461
+ var runtimeSidecarPortSchema = z12.object({
1462
+ name: z12.string(),
1463
+ containerPort: z12.number().int().positive().max(65535),
1464
+ protocol: z12.literal("tcp").default("tcp")
1465
+ });
1466
+ var runtimeSidecarReadinessSchema = z12.discriminatedUnion("type", [
1467
+ z12.object({
1468
+ type: z12.literal("tcp"),
1469
+ port: z12.string(),
1470
+ timeoutSeconds: z12.number().int().positive().default(30)
1471
+ }),
1472
+ z12.object({
1473
+ type: z12.literal("http"),
1474
+ port: z12.string(),
1475
+ path: z12.string(),
1476
+ statuses: z12.number().int().positive().array().default([200]),
1477
+ timeoutSeconds: z12.number().int().positive().default(30)
1478
+ }),
1479
+ z12.object({
1480
+ type: z12.literal("log"),
1481
+ pattern: z12.string(),
1482
+ timeoutSeconds: z12.number().int().positive().default(30)
1483
+ })
1484
+ ]);
1485
+ var runtimeSidecarStartInputSchema = z12.object({
1486
+ identity: z12.string().regex(/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/),
1487
+ image: z12.string(),
1488
+ command: z12.string().array().optional(),
1489
+ args: z12.string().array().default([]),
1490
+ env: z12.record(z12.string(), z12.string()).default({}),
1491
+ files: runtimeSidecarFileSchema.array().default([]),
1492
+ ports: runtimeSidecarPortSchema.array().default([]),
1493
+ readiness: runtimeSidecarReadinessSchema.optional()
1494
+ });
1495
+ var runtimeSidecarStartOutputSchema = z12.object({
1496
+ id: z12.string(),
1497
+ host: z12.string(),
1498
+ ports: z12.record(z12.string(), z12.object({
1499
+ host: z12.string(),
1500
+ port: z12.number().int().positive().max(65535)
1501
+ }))
1502
+ });
1422
1503
  // src/utils.ts
1423
1504
  import { isNonNullish as isNonNullish2, pickBy as pickBy2 } from "remeda";
1424
1505
  function text(strings, ...values) {
@@ -1469,9 +1550,9 @@ function stripNullish(obj) {
1469
1550
  }
1470
1551
 
1471
1552
  // src/index.ts
1472
- import { z as z12 } from "zod";
1553
+ import { z as z13 } from "zod";
1473
1554
  export {
1474
- z12 as z,
1555
+ z13 as z,
1475
1556
  yamlValueSchema,
1476
1557
  workerRunOptionsSchema,
1477
1558
  versionedNameSchema,
@@ -1497,7 +1578,16 @@ export {
1497
1578
  serviceAccountMetaSchema,
1498
1579
  selectInput,
1499
1580
  secretSchema,
1581
+ runtimeSidecarStartOutputSchema,
1582
+ runtimeSidecarStartInputSchema,
1583
+ runtimeSidecarReadinessSchema,
1584
+ runtimeSidecarPortSchema,
1585
+ runtimeSidecarFileSchema,
1500
1586
  runtimeSchema,
1587
+ runtimeResultSubmitOutputSchema,
1588
+ runtimeResultSubmitInputSchema,
1589
+ runtimeConfigGetOutputSchema,
1590
+ runtimeConfigGetInputSchema,
1501
1591
  resetEvaluation,
1502
1592
  registerKnownAbbreviations,
1503
1593
  positionSchema,
@@ -1507,6 +1597,7 @@ export {
1507
1597
  pageBlockSchema,
1508
1598
  originalCreate,
1509
1599
  objectMetaSchema,
1600
+ objectEntity,
1510
1601
  kind,
1511
1602
  isUnitModel,
1512
1603
  isSecret,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.21.1",
3
+ "version": "0.26.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
package/src/entity.ts CHANGED
@@ -383,6 +383,37 @@ type AddSchemaInclusions<
383
383
  ? TSchema
384
384
  : z.ZodIntersection<TSchema, z.ZodObject<InclusionShape<TImplements>>>
385
385
 
386
+ /**
387
+ * The common ancestor of all entities. Any entity can be assigned to this entity.
388
+ */
389
+ export const objectEntity = {
390
+ type: "system.object.v1",
391
+ schema: z.object().loose(),
392
+ model: {
393
+ type: "system.object.v1",
394
+ definitionHash: 0,
395
+ schema: z.object().loose().toJSONSchema(),
396
+ meta: {
397
+ title: "Object",
398
+ description:
399
+ "The common ancestor of all entities. Any entity can be assigned to this entity.",
400
+ color: "#9e9e9e",
401
+ icon: "mdi:cube",
402
+ iconColor: "#9e9e9e",
403
+ },
404
+ },
405
+ } as unknown as Entity<"system.object.v1", { "system.object.v1": true }, z.ZodUnknown>
406
+
407
+ type DefineEntityTypeExtensions<
408
+ TImplementedTypes extends EntityTypes,
409
+ TExtends extends Record<string, Entity>,
410
+ > = AddTypeExtensions<TImplementedTypes, TExtends> & (typeof objectEntity)[typeof implementedTypes]
411
+
412
+ type DefineEntitySchemaExtensions<
413
+ TSchema extends z.ZodTypeAny,
414
+ TExtends extends Record<string, Entity>,
415
+ > = AddSchemaExtensions<TSchema, TExtends>
416
+
386
417
  export function defineEntity<
387
418
  TType extends VersionedName,
388
419
  TSchema extends z.ZodType,
@@ -392,8 +423,8 @@ export function defineEntity<
392
423
  options: EntityOptions<TType, TSchema, TExtends, TIncludes>,
393
424
  ): Entity<
394
425
  TType,
395
- Simplify<AddTypeExtensions<{ [K in TType]: true }, TExtends>>,
396
- MakeEntitySchema<AddSchemaExtensions<AddSchemaInclusions<TSchema, TIncludes>, TExtends>>,
426
+ DefineEntityTypeExtensions<{ [K in TType]: true }, TExtends>,
427
+ MakeEntitySchema<DefineEntitySchemaExtensions<AddSchemaInclusions<TSchema, TIncludes>, TExtends>>,
397
428
  DefineEntityValue<TSchema, TExtends, AddIncludeExtensions<TIncludes, TExtends>>,
398
429
  DefineEntityInput<TSchema, TExtends, AddIncludeExtensions<TIncludes, TExtends>>,
399
430
  AddIncludeExtensions<TIncludes, TExtends>
@@ -455,16 +486,34 @@ export function defineEntity<
455
486
  {} as Record<string, z.ZodTypeAny>,
456
487
  )
457
488
 
458
- let finalSchema = Object.values(options.extends ?? {}).reduce(
459
- (schema, entity) => z.intersection(schema, entity.schema),
460
- options.schema as z.ZodType,
489
+ const inheritedExtensions = [...Object.values(options.extends ?? {}), objectEntity].filter(
490
+ entity => entity.type !== options.type,
491
+ )
492
+
493
+ const uniqueExtensions = Array.from(
494
+ new Map(inheritedExtensions.map(entity => [entity.type, entity])).values(),
495
+ )
496
+
497
+ const schemaExtensions = uniqueExtensions.filter(entity => entity.type !== objectEntity.type)
498
+
499
+ const strictIntersectionBase = (schema: z.ZodTypeAny): z.ZodTypeAny => {
500
+ if (schema instanceof z.ZodObject) {
501
+ return schema.strict()
502
+ }
503
+
504
+ return schema
505
+ }
506
+
507
+ let finalSchema = schemaExtensions.reduce(
508
+ (schema, entity) => z.intersection(schema, strictIntersectionBase(entity.schema)),
509
+ strictIntersectionBase(options.schema as z.ZodTypeAny),
461
510
  )
462
511
 
463
512
  if (includeRefs.length > 0) {
464
- finalSchema = z.intersection(finalSchema, z.object(inclusionShape))
513
+ finalSchema = z.intersection(finalSchema, strictIntersectionBase(z.object(inclusionShape)))
465
514
  }
466
515
 
467
- finalSchema = z.intersection(finalSchema, entityWithMetaSchema)
516
+ finalSchema = z.intersection(finalSchema, strictIntersectionBase(entityWithMetaSchema))
468
517
 
469
518
  const directInclusions = () =>
470
519
  includeRefs.map(includeRef => ({
@@ -473,12 +522,12 @@ export function defineEntity<
473
522
  multiple: includeRef.multiple,
474
523
  field: includeRef.field,
475
524
  }))
476
- const directExtensions = Object.values(options.extends ?? {}).map(entity => entity.type)
525
+ const directExtensions = uniqueExtensions.map(entity => entity.type)
477
526
 
478
527
  const getInclusions = () => {
479
528
  const incs = [...directInclusions()]
480
529
 
481
- for (const entity of Object.values(options.extends ?? {})) {
530
+ for (const entity of uniqueExtensions) {
482
531
  if (entity.model.inclusions) {
483
532
  incs.push(...entity.model.inclusions)
484
533
  }
@@ -487,7 +536,7 @@ export function defineEntity<
487
536
  return incs
488
537
  }
489
538
 
490
- const extensions = Object.values(options.extends ?? {}).reduce((exts, entity) => {
539
+ const extensions = uniqueExtensions.reduce((exts, entity) => {
491
540
  exts.push(...(entity.model.extensions ?? []), entity.type)
492
541
 
493
542
  return exts
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@ export {
12
12
  defineEntity,
13
13
  entityModelSchema,
14
14
  getEntityId,
15
+ objectEntity,
15
16
  } from "./entity"
16
17
 
17
18
  export * from "./instance"
@@ -23,6 +24,7 @@ export * from "./terminal"
23
24
  export * from "./page"
24
25
  export * from "./trigger"
25
26
  export * from "./worker"
27
+ export * from "./runtime"
26
28
  export * from "./cuidv2d"
27
29
 
28
30
  export {
package/src/runtime.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { z } from "zod"
2
+ import { unitConfigSchema } from "./pulumi"
3
+
4
+ export const runtimeConfigGetInputSchema = z.void()
5
+
6
+ export const runtimeConfigGetOutputSchema = unitConfigSchema
7
+
8
+ export const runtimeResultSubmitInputSchema = z.record(z.string(), z.unknown())
9
+
10
+ export const runtimeResultSubmitOutputSchema = z.object({})
11
+
12
+ export const runtimeSidecarFileSchema = z.object({
13
+ path: z.string(),
14
+ content: z.string(),
15
+ secret: z.boolean().default(false),
16
+ mode: z.number().int().optional(),
17
+ })
18
+
19
+ export const runtimeSidecarPortSchema = z.object({
20
+ name: z.string(),
21
+ containerPort: z.number().int().positive().max(65535),
22
+ protocol: z.literal("tcp").default("tcp"),
23
+ })
24
+
25
+ export const runtimeSidecarReadinessSchema = z.discriminatedUnion("type", [
26
+ z.object({
27
+ type: z.literal("tcp"),
28
+ port: z.string(),
29
+ timeoutSeconds: z.number().int().positive().default(30),
30
+ }),
31
+ z.object({
32
+ type: z.literal("http"),
33
+ port: z.string(),
34
+ path: z.string(),
35
+ statuses: z.number().int().positive().array().default([200]),
36
+ timeoutSeconds: z.number().int().positive().default(30),
37
+ }),
38
+ z.object({
39
+ type: z.literal("log"),
40
+ pattern: z.string(),
41
+ timeoutSeconds: z.number().int().positive().default(30),
42
+ }),
43
+ ])
44
+
45
+ export const runtimeSidecarStartInputSchema = z.object({
46
+ identity: z.string().regex(/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/),
47
+ image: z.string(),
48
+ command: z.string().array().optional(),
49
+ args: z.string().array().default([]),
50
+ env: z.record(z.string(), z.string()).default({}),
51
+ files: runtimeSidecarFileSchema.array().default([]),
52
+ ports: runtimeSidecarPortSchema.array().default([]),
53
+ readiness: runtimeSidecarReadinessSchema.optional(),
54
+ })
55
+
56
+ export const runtimeSidecarStartOutputSchema = z.object({
57
+ id: z.string(),
58
+ host: z.string(),
59
+ ports: z.record(
60
+ z.string(),
61
+ z.object({
62
+ host: z.string(),
63
+ port: z.number().int().positive().max(65535),
64
+ }),
65
+ ),
66
+ })
67
+
68
+ export type RuntimeConfigGetInput = z.infer<typeof runtimeConfigGetInputSchema>
69
+ export type RuntimeConfigGetOutput = z.infer<typeof runtimeConfigGetOutputSchema>
70
+ export type RuntimeResultSubmitInput = z.infer<typeof runtimeResultSubmitInputSchema>
71
+ export type RuntimeResultSubmitOutput = z.infer<typeof runtimeResultSubmitOutputSchema>
72
+ export type RuntimeSidecarReadiness = z.infer<typeof runtimeSidecarReadinessSchema>
73
+ export type RuntimeSidecarStartInput = z.infer<typeof runtimeSidecarStartInputSchema>
74
+ export type RuntimeSidecarStartOutput = z.infer<typeof runtimeSidecarStartOutputSchema>
package/src/worker.ts CHANGED
@@ -19,11 +19,21 @@ export const workerRunOptionsSchema = z.object({
19
19
  */
20
20
  workerVersionId: z.cuid2(),
21
21
 
22
+ /**
23
+ * The ID of the concrete worker instance.
24
+ */
25
+ workerInstanceId: z.cuid2(),
26
+
22
27
  /**
23
28
  * The URL of the backend API to connect to.
24
29
  */
25
30
  apiUrl: z.url(),
26
31
 
32
+ /**
33
+ * The host and port of the worker data endpoint reachable by the frontend gateway.
34
+ */
35
+ dataEndpoint: z.string().min(1),
36
+
27
37
  /**
28
38
  * The API key used to authenticate the worker with the backend.
29
39
  */