@highstate/contract 0.20.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.20.0",
3
+ "version": "0.26.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -28,29 +28,28 @@
28
28
  "platform"
29
29
  ]
30
30
  },
31
+ "scripts": {
32
+ "build": "bun run --bun highstate build",
33
+ "test": "bun run --bun vitest run",
34
+ "typecheck": "tsgo --noEmit --skipLibCheck",
35
+ "biome": "biome check --write --unsafe --error-on-warnings",
36
+ "biome:check": "biome check --error-on-warnings"
37
+ },
31
38
  "dependencies": {
32
- "@paralleldrive/cuid2": "^2.2.2",
33
39
  "@noble/hashes": "^2.0.0",
40
+ "@paralleldrive/cuid2": "^2.2.2",
34
41
  "remeda": "^2.21.0",
35
42
  "type-fest": "^4.41.0",
36
43
  "yaml": "^2.8.0",
37
- "zod": "^4.0.5"
44
+ "zod": "^4.3.6"
38
45
  },
39
46
  "devDependencies": {
40
47
  "@biomejs/biome": "2.2.0",
41
48
  "@typescript/native-preview": "^7.0.0-dev.20250920.1",
42
49
  "@vitest/coverage-v8": "2.1.8",
43
- "highstate-cli-bootstrap": "npm:@highstate/cli",
44
50
  "vitest": "^3.2.4"
45
51
  },
46
52
  "repository": {
47
53
  "url": "https://github.com/highstate-io/highstate"
48
- },
49
- "scripts": {
50
- "build": "highstate build",
51
- "test": "vitest run",
52
- "typecheck": "tsgo --noEmit --skipLibCheck",
53
- "biome": "biome check --write --unsafe --error-on-warnings",
54
- "biome:check": "biome check --error-on-warnings"
55
54
  }
56
- }
55
+ }
@@ -1,9 +1,10 @@
1
1
  import { beforeEach, describe, expect, it } from "vitest"
2
2
  import { z } from "zod"
3
- import { defineComponent, kind } from "./component"
3
+ import { defineComponent } from "./component"
4
4
  import { defineEntity } from "./entity"
5
- import { boundaryInput, getRuntimeInstances, resetEvaluation } from "./evaluation"
5
+ import { getRuntimeInstances, resetEvaluation } from "./evaluation"
6
6
  import { type EntityInput, type InstanceInput, selectInput } from "./instance"
7
+ import { boundaryInput, kind } from "./shared"
7
8
 
8
9
  describe("defineComponent", () => {
9
10
  beforeEach(resetEvaluation)
package/src/component.ts CHANGED
@@ -2,21 +2,20 @@
2
2
 
3
3
  import type { Simplify } from "type-fest"
4
4
  import type { Entity, implementedTypes } from "./entity"
5
+ import type {
6
+ EntityInput,
7
+ InstanceId,
8
+ InstanceInput,
9
+ MultipleInput,
10
+ RequiredInput,
11
+ RequiredMultipleInput,
12
+ RuntimeInput,
13
+ } from "./instance"
5
14
  import type { OptionalEmptyRecords, PartialKeys } from "./utils"
6
15
  import { isNonNullish, mapValues, pickBy, uniqueBy } from "remeda"
7
16
  import { z } from "zod"
8
- import { boundaryInput, registerInstance } from "./evaluation"
17
+ import { registerInstance } from "./evaluation"
9
18
  import { camelCaseToHumanReadable } from "./i18n"
10
- import {
11
- type EntityInput,
12
- type InstanceId,
13
- type InstanceInput,
14
- inputKey,
15
- type MultipleInput,
16
- type RequiredInput,
17
- type RequiredMultipleInput,
18
- type RuntimeInput,
19
- } from "./instance"
20
19
  import {
21
20
  createDeepOutputAccessor,
22
21
  createInput,
@@ -31,8 +30,14 @@ import {
31
30
  type VersionedName,
32
31
  versionedNameSchema,
33
32
  } from "./meta"
34
-
35
- export const runtimeSchema = Symbol("runtimeSchema")
33
+ import {
34
+ boundaryInput,
35
+ type ComponentKind,
36
+ componentKindSchema,
37
+ inputKey,
38
+ kind,
39
+ runtimeSchema,
40
+ } from "./shared"
36
41
 
37
42
  let validationEnabled = true
38
43
 
@@ -40,9 +45,6 @@ export function setValidationEnabled(enabled: boolean): void {
40
45
  validationEnabled = enabled
41
46
  }
42
47
 
43
- export const componentKindSchema = z.enum(["composite", "unit"])
44
- export type ComponentKind = z.infer<typeof componentKindSchema>
45
-
46
48
  export const componentArgumentSchema = z.object({
47
49
  /**
48
50
  * The JSON schema of the argument value.
@@ -478,7 +480,6 @@ type ValidateCallInputs<
478
480
  }
479
481
 
480
482
  export const originalCreate = Symbol("originalCreate")
481
- export const kind = Symbol("kind")
482
483
 
483
484
  export type Component<
484
485
  TType extends VersionedName = VersionedName,
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/evaluation.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import type { Component } from "./component"
2
2
  import type { InstanceInput, InstanceModel, RuntimeInput } from "./instance"
3
3
  import { mapValues } from "remeda"
4
+ import { boundaryInput } from "./shared"
4
5
 
5
6
  export type RuntimeInstance = {
6
7
  instance: InstanceModel
7
8
  component: Component
8
9
  }
9
10
 
10
- export const boundaryInput = Symbol("boundaryInput")
11
-
12
11
  function isStableInstanceInput(value: unknown): value is InstanceInput {
13
12
  return (
14
13
  typeof value === "object" &&
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 {
@@ -53,13 +55,11 @@ export {
53
55
  getInstanceId,
54
56
  // for unit API
55
57
  type ComponentInputSpec,
56
- runtimeSchema,
57
58
  // types
58
59
  type Component,
59
60
  type ComponentModel,
60
61
  type ComponentInput,
61
62
  type ComponentArgument,
62
- type ComponentKind,
63
63
  // extra types
64
64
  type ComponentArgumentOptions,
65
65
  type FullComponentArgumentOptions,
@@ -76,6 +76,15 @@ export {
76
76
  originalCreate,
77
77
  } from "./component"
78
78
 
79
+ export {
80
+ boundaryInput,
81
+ inputKey,
82
+ kind,
83
+ runtimeSchema,
84
+ componentKindSchema,
85
+ type ComponentKind,
86
+ } from "./shared"
87
+
79
88
  // for runner <-> stack communication
80
89
  export * from "./pulumi"
81
90
 
@@ -1,5 +1,5 @@
1
1
  import type { InstanceInput, MultipleInput, RequiredInput, RuntimeInput } from "./instance"
2
- import { boundaryInput } from "./evaluation"
2
+ import { boundaryInput } from "./shared"
3
3
 
4
4
  type CreateInputOptions = {
5
5
  boundary?: InstanceInput
package/src/instance.ts CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  import type { Entity, EntityIncludeRef, EntityTypes } from "./entity"
4
4
  import { z } from "zod"
5
- import { componentKindSchema } from "./component"
6
- import { boundaryInput } from "./evaluation"
7
5
  import { createInput, createNonProvidedInput } from "./instance-input"
8
6
  import {
9
7
  type GenericName,
@@ -12,6 +10,7 @@ import {
12
10
  type VersionedName,
13
11
  versionedNameSchema,
14
12
  } from "./meta"
13
+ import { boundaryInput, componentKindSchema } from "./shared"
15
14
 
16
15
  declare const type: unique symbol
17
16
 
@@ -240,12 +239,6 @@ type SelectInputResult<TInput extends RuntimeInput> = TInput extends RequiredInp
240
239
  ? TRuntime
241
240
  : TInput
242
241
 
243
- export function inputKey(input: InstanceInput): string {
244
- return input.path
245
- ? `${input.instanceId}:${input.output}:${input.path}`
246
- : `${input.instanceId}:${input.output}`
247
- }
248
-
249
242
  export const positionSchema = z.object({
250
243
  x: z.number(),
251
244
  y: z.number(),
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/shared.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { InstanceInput } from "./instance"
2
+ import { z } from "zod"
3
+
4
+ export const componentKindSchema = z.enum(["composite", "unit"])
5
+ export type ComponentKind = z.infer<typeof componentKindSchema>
6
+
7
+ export const runtimeSchema = Symbol("runtimeSchema")
8
+ export const kind = Symbol("kind")
9
+ export const boundaryInput = Symbol("boundaryInput")
10
+
11
+ export function inputKey(input: InstanceInput): string {
12
+ return input.path
13
+ ? `${input.instanceId}:${input.output}:${input.path}`
14
+ : `${input.instanceId}:${input.output}`
15
+ }
package/src/unit.ts CHANGED
@@ -19,12 +19,11 @@ import {
19
19
  componentModelSchema,
20
20
  defineComponent,
21
21
  type FullComponentArgumentOptions,
22
- kind,
23
22
  mapArgument,
24
23
  type ToFullComponentArgumentOptions,
25
24
  toFullComponentArgumentOptions,
26
25
  } from "./component"
27
- import { boundaryInput } from "./evaluation"
26
+ import { boundaryInput, kind } from "./shared"
28
27
 
29
28
  export const componentSecretSchema = componentArgumentSchema.extend({
30
29
  /**
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
  */
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Exeteres
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.