@telorun/sdk 0.2.5 → 0.2.7

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.
Files changed (43) hide show
  1. package/dist/capabilities/invokable.d.ts +2 -2
  2. package/dist/capabilities/invokable.d.ts.map +1 -1
  3. package/dist/capability-definition.d.ts +8 -0
  4. package/dist/capability-definition.d.ts.map +1 -0
  5. package/dist/capability-definition.js +21 -0
  6. package/dist/cel-environment.d.ts +3 -0
  7. package/dist/cel-environment.d.ts.map +1 -0
  8. package/dist/cel-environment.js +13 -0
  9. package/dist/compiled-value.d.ts +9 -0
  10. package/dist/compiled-value.d.ts.map +1 -0
  11. package/dist/compiled-value.js +3 -0
  12. package/dist/evaluation-context.d.ts +85 -26
  13. package/dist/evaluation-context.d.ts.map +1 -1
  14. package/dist/evaluation-context.js +227 -89
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +2 -0
  18. package/dist/module-context.d.ts +6 -4
  19. package/dist/module-context.d.ts.map +1 -1
  20. package/dist/module-context.js +31 -4
  21. package/dist/ref.d.ts +47 -0
  22. package/dist/ref.d.ts.map +1 -0
  23. package/dist/ref.js +11 -0
  24. package/dist/resource-context.d.ts +19 -8
  25. package/dist/resource-context.d.ts.map +1 -1
  26. package/dist/resource-instance.d.ts +4 -9
  27. package/dist/resource-instance.d.ts.map +1 -1
  28. package/dist/runtime-error.d.ts +8 -1
  29. package/dist/runtime-error.d.ts.map +1 -1
  30. package/dist/types.d.ts +24 -10
  31. package/dist/types.d.ts.map +1 -1
  32. package/dist/types.js +3 -1
  33. package/package.json +2 -5
  34. package/src/capabilities/invokable.ts +2 -2
  35. package/src/compiled-value.ts +11 -0
  36. package/src/evaluation-context.ts +283 -101
  37. package/src/index.ts +2 -0
  38. package/src/module-context.ts +33 -5
  39. package/src/ref.ts +61 -0
  40. package/src/resource-context.ts +19 -8
  41. package/src/resource-instance.ts +11 -14
  42. package/src/runtime-error.ts +15 -1
  43. package/src/types.ts +24 -14
package/src/ref.ts ADDED
@@ -0,0 +1,61 @@
1
+ import type { ResourceInstance } from "./resource-instance.js";
2
+
3
+ /** Marker type for x-telo-ref fields. Carries the live instance type at the TypeScript level;
4
+ * at runtime this field holds `{ kind, name }` until Phase 5 injection replaces it.
5
+ * T is a phantom type — any capability interface (Invocable, Runnable, …) or ResourceInstance. */
6
+ export interface KindRef<T = ResourceInstance> {
7
+ readonly kind: string;
8
+ readonly name: string;
9
+ readonly __type?: T;
10
+ }
11
+
12
+ /** Marker type for x-telo-scope fields. Has no runtime value — used only as a discriminant
13
+ * for Injected<T> to transform the field to ScopeHandle. */
14
+ export interface ScopeRef {
15
+ readonly __scope: true;
16
+ }
17
+
18
+ /** Gives a controller access to the resources initialized within a scope. */
19
+ export interface ScopeContext {
20
+ /** Returns the initialized instance for the given name.
21
+ * Throws synchronously if the name was not declared in the scope —
22
+ * this is always a programming error; all scope members are statically
23
+ * validated in Phase 3 before the kernel ever reaches runtime. */
24
+ getInstance(name: string): ResourceInstance;
25
+ }
26
+
27
+ /** Returned by Phase 5 injection in place of an x-telo-scope manifest array.
28
+ * The controller calls run() to open the scope, execute work, and tear it down. */
29
+ export interface ScopeHandle {
30
+ run<T>(fn: (scope: ScopeContext) => Promise<T>): Promise<T>;
31
+ }
32
+
33
+ /** Transforms the raw config shape into the controller's view:
34
+ * - KindRef<U> → U (live instance, injected by Phase 5)
35
+ * - KindRef<U>[] → U[] (live instances, injected by Phase 5)
36
+ * - ScopeRef → ScopeHandle
37
+ * - everything else is unchanged */
38
+ export type Injected<T> = {
39
+ [K in keyof T]: T[K] extends KindRef<infer U>
40
+ ? U
41
+ : T[K] extends KindRef<infer U>[]
42
+ ? U[]
43
+ : NonNullable<T[K]> extends ScopeRef
44
+ ? ScopeHandle | Exclude<T[K], ScopeRef>
45
+ : T[K];
46
+ };
47
+
48
+ /** Returns a schema node that emits `x-telo-ref` for buildReferenceFieldMap and carries
49
+ * KindRef<T> as its TypeScript type. For TypeBox schemas use Type.Unsafe<KindRef<T>>(Ref(...)).
50
+ *
51
+ * @param ref Canonical ref string: "namespace/module-name#TypeName" or "kernel#TypeName" */
52
+ export const Ref = <T = ResourceInstance>(ref: string): KindRef<T> =>
53
+ ({ "x-telo-ref": ref } as unknown as KindRef<T>);
54
+
55
+ /** Returns a schema node that emits `x-telo-scope` for buildReferenceFieldMap and carries
56
+ * ScopeRef as its TypeScript type. For TypeBox schemas use Type.Unsafe<ScopeRef>(Scope(...)).
57
+ *
58
+ * @param visibilityPath JSON Pointer(s) (RFC 6901) declaring where x-telo-ref slots within
59
+ * this field can resolve to scoped resources. */
60
+ export const Scope = (visibilityPath: string | string[]): ScopeRef =>
61
+ ({ "x-telo-scope": visibilityPath } as unknown as ScopeRef);
@@ -8,6 +8,12 @@ export interface DataValidator {
8
8
  isValid(data: any): boolean;
9
9
  }
10
10
 
11
+ export interface TypeRule {
12
+ condition: string;
13
+ code: string;
14
+ message?: string;
15
+ }
16
+
11
17
  export class NoopValidator implements DataValidator {
12
18
  isValid() {
13
19
  return true;
@@ -18,30 +24,35 @@ export class NoopValidator implements DataValidator {
18
24
  }
19
25
  }
20
26
 
27
+ export type ParsedArgs = Partial<Record<string, string | boolean | string[]>> & { _: string[] };
28
+
21
29
  export interface ResourceContext extends ControllerContext {
30
+ readonly args: ParsedArgs;
22
31
  acquireHold(reason?: string): () => void;
23
32
  emitEvent(event: string, payload?: any): Promise<void>;
24
- invoke(kind: string, name: string, ...args: any[]): Promise<any>;
33
+ invoke<TInputs>(kind: string, name: string, inputs: TInputs, options?: any): Promise<any>;
25
34
  run(kind: string, name: string): Promise<void>;
26
35
  getResources(kind: string): RuntimeResource[];
27
36
  getResourcesByName(kind: string, name: string): RuntimeResource | null;
28
37
  registerManifest(resource: any): void;
29
38
  spawnChildContext(): EvaluationContext;
39
+ transientChild(context: Record<string, any>): EvaluationContext;
30
40
  withManifests<T>(manifests: any[], fn: () => T): T;
31
41
  resolveChildren(resource: any, resourceName?: string): { kind: string; name: string };
32
42
  validateSchema(value: any, schema: any): void;
33
43
  createSchemaValidator(schema: any): DataValidator;
34
44
  registerSchema(name: string, schema: object): void;
35
45
  lookupSchema(name: string): object | undefined;
46
+ registerTypeRules(name: string, rules: TypeRule[]): void;
47
+ lookupTypeRules(name: string): TypeRule[] | undefined;
48
+ /** Resolve a type reference (name string or inline schema) to a DataValidator. */
49
+ createTypeValidator(typeRef: string | Record<string, any> | undefined): DataValidator;
36
50
  registerController(moduleName: string, kindName: string, controllerInstance: any): Promise<void>;
37
51
  registerDefinition(definition: any): void;
38
52
  registerModuleImport(alias: string, targetModule: string, kinds: string[]): void;
39
- registerCapability(name: string, schema?: Record<string, any>): void;
40
- isCapabilityRegistered(name: string): boolean;
41
- getCapabilitySchema(name: string): Record<string, any> | null | undefined;
42
53
  teardownResource(kind: string, name: string): Promise<void>;
43
- moduleContext: ModuleContext;
44
- stdin: NodeJS.ReadableStream;
45
- stdout: NodeJS.WritableStream;
46
- stderr: NodeJS.WritableStream;
54
+ readonly moduleContext: ModuleContext;
55
+ readonly stdin: NodeJS.ReadableStream;
56
+ readonly stdout: NodeJS.WritableStream;
57
+ readonly stderr: NodeJS.WritableStream;
47
58
  }
@@ -1,15 +1,12 @@
1
- import { ResourceContext } from "./resource-context.js";
1
+ import type { Invocable } from "./capabilities/invokable.js";
2
+ import type { Runnable } from "./capabilities/runnable.js";
3
+ import type { ResourceContext } from "./resource-context.js";
2
4
 
3
- export type ResourceInstance = {
4
- init?(ctx?: ResourceContext): Promise<void>;
5
- run?(): void | Promise<void>;
6
- invoke?(input: any): any | Promise<any>;
7
- teardown?(): void | Promise<void>;
8
-
9
- /**
10
- * Optional method for debugging/snapshots
11
- * Called when taking runtime state snapshots
12
- * Should return serializable state data specific to this resource
13
- */
14
- snapshot?(): Record<string, any> | Promise<Record<string, any>>;
15
- };
5
+ export type ResourceInstance<TInput = Record<string, any>, TOutput = any> = Partial<
6
+ Invocable<TInput, TOutput>
7
+ > &
8
+ Partial<Runnable> & {
9
+ init?(ctx?: ResourceContext): Promise<void>;
10
+ teardown?(): void | Promise<void>;
11
+ snapshot?(): Record<string, any> | Promise<Record<string, any>>;
12
+ };
@@ -1,3 +1,11 @@
1
+ export interface RuntimeDiagnostic {
2
+ severity?: "error" | "warning";
3
+ message: string;
4
+ resource?: string;
5
+ code?: string;
6
+ }
7
+
8
+ /** Well-known kernel error codes. User-defined codes (e.g. from Type.JsonSchema rules) are also valid. */
1
9
  export type RuntimeErrorCode =
2
10
  | "ERR_RESOURCE_NOT_FOUND"
3
11
  | "ERR_RESOURCE_NOT_RUNNABLE"
@@ -9,4 +17,10 @@ export type RuntimeErrorCode =
9
17
  | "ERR_DUPLICATE_RESOURCE"
10
18
  | "ERR_EXECUTION_FAILED"
11
19
  | "ERR_INVALID_VALUE"
12
- | "ERR_VISIBILITY_DENIED";
20
+ | "ERR_VISIBILITY_DENIED"
21
+ | "ERR_MANIFEST_VALIDATION_FAILED"
22
+ | "ERR_CIRCULAR_DEPENDENCY"
23
+ | "ERR_SCOPE_RESOURCE_NOT_FOUND"
24
+ | "ERR_TYPE_NOT_FOUND"
25
+ | "ERR_TYPE_VALIDATION_FAILED"
26
+ | (string & {});
package/src/types.ts CHANGED
@@ -2,7 +2,7 @@ import { ControllerContext } from "./controller-context.js";
2
2
  import { ResourceContext } from "./resource-context.js";
3
3
  import { ResourceInstance } from "./resource-instance.js";
4
4
  import { ResourceManifest } from "./resource-manifest.js";
5
- import { RuntimeErrorCode } from "./runtime-error.js";
5
+ import { RuntimeDiagnostic, RuntimeErrorCode } from "./runtime-error.js";
6
6
  import { RuntimeResource } from "./runtime-resource.js";
7
7
 
8
8
  export interface KernelContext {
@@ -14,17 +14,15 @@ export interface ExecContext {
14
14
  [key: string]: any;
15
15
  }
16
16
 
17
- export type ResourceCapability = string;
18
-
19
17
  export interface ResourceDefinition {
20
18
  kind: string;
21
19
  metadata: {
22
20
  name: string;
23
21
  module: string;
24
22
  };
25
- schema: Record<string, any>; // JSON Schema
26
- capabilities: ResourceCapability[];
27
- events?: string[];
23
+ /** JSON Schema for the resource's compile-time configuration fields. */
24
+ schema?: Record<string, any>;
25
+ capability?: string;
28
26
  controllers?: Array<{
29
27
  runtime: string;
30
28
  entry: string;
@@ -44,19 +42,30 @@ export interface ControllerDefinition {
44
42
 
45
43
  /**
46
44
  * Controller instance - runtime representation of a controller that handles resource instances.
45
+ *
46
+ * TResource - the typed shape of the resource manifest (compile-time config)
47
+ * TInput - the typed shape of invoke() inputs (runtime)
48
+ * TOutput - the typed shape of invoke() outputs (runtime)
47
49
  */
48
- export interface ControllerInstance {
50
+ export interface ControllerInstance<
51
+ TResource extends ResourceManifest = ResourceManifest,
52
+ TInput = Record<string, any>,
53
+ TOutput = any,
54
+ > {
49
55
  execute?(name: string, inputs: any, ctx: ExecContext): Promise<any>;
50
- compile?(
51
- resource: ResourceManifest,
52
- ctx: ResourceContext,
53
- ): RuntimeResource | Promise<RuntimeResource>;
56
+ compile?(resource: TResource, ctx: ResourceContext): RuntimeResource | Promise<RuntimeResource>;
54
57
  register?(ctx: ControllerContext): void | Promise<void>;
55
58
  create?(
56
- resource: ResourceManifest,
59
+ resource: TResource,
57
60
  ctx: ResourceContext,
58
- ): ResourceInstance | null | Promise<ResourceInstance | null>;
59
- schema: any;
61
+ ): ResourceInstance<TInput, TOutput> | null | Promise<ResourceInstance<TInput, TOutput> | null>;
62
+ schema?: any;
63
+ /** CLI argument spec. Keys are arg names; values define type, alias, and description. */
64
+ args?: Record<string, { type: "string" | "boolean"; alias?: string; description?: string }>;
65
+ /** Type reference for invoke() inputs — name string or inline type definition. */
66
+ inputType?: string | Record<string, any>;
67
+ /** Type reference for invoke() outputs — name string or inline type definition. */
68
+ outputType?: string | Record<string, any>;
60
69
  }
61
70
 
62
71
  export interface Kernel {
@@ -76,6 +85,7 @@ export class RuntimeError extends Error {
76
85
  constructor(
77
86
  public code: RuntimeErrorCode,
78
87
  message: string,
88
+ public diagnostics?: RuntimeDiagnostic[],
79
89
  ) {
80
90
  super(message);
81
91
  this.name = "RuntimeError";