@telorun/sdk 0.2.7 → 0.2.8

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.
@@ -1,215 +1,34 @@
1
- import { Invocable } from "./capabilities/invokable.js";
2
- import { EmitEvent, EvaluationContext, InstanceFactory } from "./evaluation-context.js";
3
-
4
- /** Wraps process.env so that missing keys return null instead of throwing in CEL.
5
- * cel-js uses Object.hasOwn(obj, key) before accessing obj[key], so we must
6
- * intercept getOwnPropertyDescriptor to report every string key as "own". */
7
- function lenientEnv(env: Record<string, string | undefined>): Record<string, string | null> {
8
- return new Proxy(env as Record<string, string | null>, {
9
- get(target, key) {
10
- if (typeof key !== "string") return (target as any)[key];
11
- return key in target ? (target[key] ?? null) : null;
12
- },
13
- has() {
14
- return true;
15
- },
16
- getOwnPropertyDescriptor(target, key) {
17
- if (typeof key !== "string") return Object.getOwnPropertyDescriptor(target, key);
18
- const value = key in target ? (target[key] ?? null) : null;
19
- return { configurable: true, enumerable: true, writable: true, value };
20
- },
21
- });
22
- }
23
-
24
- function collectSecretValues(secrets: Record<string, unknown>): Set<string> {
25
- const values = new Set<string>();
26
- for (const value of Object.values(secrets)) {
27
- if (typeof value === "string" && value.length > 0) {
28
- values.add(value);
29
- }
30
- }
31
- return values;
32
- }
1
+ import type { Invocable } from "./capabilities/invokable.js";
2
+ import type { EvaluationContext } from "./evaluation-context.js";
33
3
 
34
4
  /**
35
- * Persistent, module-scoped context. Three reserved CEL namespaces:
36
- * variables, secrets, resources.
5
+ * Public contract for a persistent, module-scoped context.
37
6
  *
7
+ * Three reserved CEL namespaces: variables, secrets, resources.
38
8
  * Unlike the base EvaluationContext, ModuleContext is stateful and mutable:
39
- * variables/secrets/resources accumulate during multi-pass initialization and
40
- * the context record is rebuilt on each mutation. Import aliases are tracked
41
- * here for alias-prefixed kind resolution (e.g. MyImport.Http.Route).
9
+ * variables/secrets/resources accumulate during multi-pass initialization.
10
+ * Import aliases are tracked here for alias-prefixed kind resolution.
42
11
  *
43
- * Imported modules are surfaced under resources.<alias> alongside local
44
- * resources — no separate imports namespace needed.
12
+ * The class implementation lives in `@telorun/kernel`.
45
13
  */
46
- export class ModuleContext extends EvaluationContext {
47
- private _variables: Record<string, unknown>;
48
- private _secrets: Record<string, unknown>;
49
- private _resources: Record<string, unknown>;
50
-
51
- /** Maps import alias → real module name for kind resolution. */
52
- readonly importAliases = new Map<string, string>();
53
-
54
- /** Maps import alias → allowed kind names. Absent entry = unrestricted (e.g. Kernel). */
55
- private readonly importedKinds = new Map<string, Set<string>>();
56
-
57
- constructor(
58
- source: string,
59
- variables: Record<string, unknown> = {},
60
- secrets: Record<string, unknown> = {},
61
- resources: Record<string, unknown> = {},
62
- private targets: string[] = [],
63
- createInstance: InstanceFactory = async () => null,
64
- emit: EmitEvent,
65
- private readonly _hostEnv?: Record<string, string | undefined>,
66
- ) {
67
- super(source, {}, createInstance, new Set(), emit);
68
- this._variables = variables;
69
- this._secrets = secrets;
70
- this._resources = resources;
71
- this._rebuildContext();
72
- }
73
-
74
- get variables(): Record<string, unknown> {
75
- return this._variables;
76
- }
77
-
78
- get secrets(): Record<string, unknown> {
79
- return this._secrets;
80
- }
81
-
82
- get resources(): Record<string, unknown> {
83
- return this._resources;
84
- }
85
-
86
- setVariables(vars: Record<string, unknown>): void {
87
- this._variables = vars;
88
- this._rebuildContext();
89
- }
14
+ export interface ModuleContext extends EvaluationContext {
15
+ readonly variables: Record<string, unknown>;
16
+ readonly secrets: Record<string, unknown>;
17
+ readonly resources: Record<string, unknown>;
90
18
 
91
- setTargets(vars: string[]): void {
92
- this.targets = vars;
93
- }
19
+ /** Maps import alias -> real module name for kind resolution. */
20
+ readonly importAliases: Map<string, string>;
94
21
 
95
- setSecrets(secrets: Record<string, unknown>): void {
96
- this._secrets = secrets;
97
- this._rebuildContext();
98
- }
99
-
100
- setResource(name: string, props: Record<string, unknown>): void {
101
- this._resources = { ...this._resources, [name]: props };
102
- this._rebuildContext();
103
- }
104
-
105
- protected override onResourceSnapshotted(name: string, snap: Record<string, unknown>): void {
106
- this.setResource(name, snap);
107
- }
108
-
109
- /**
110
- * Register an imported module under the given alias, with the list of kind names
111
- * it exports. An empty kinds array means no restriction (used for built-ins like Kernel).
112
- */
113
- registerImport(alias: string, targetModule: string, kinds: string[]): void {
114
- this.importAliases.set(alias, targetModule);
115
- if (kinds.length > 0) {
116
- this.importedKinds.set(alias, new Set(kinds));
117
- }
118
- }
119
-
120
- getInstance(name: string): unknown {
121
- const entry = this.resourceInstances.get(name);
122
- if (!entry) {
123
- throw new Error(
124
- `Resource '${name}' not found in module context. Available resources: ${[...this.resourceInstances.keys()].join(", ")}`,
125
- );
126
- }
127
- return entry?.instance;
128
- }
22
+ setVariables(vars: Record<string, unknown>): void;
23
+ setTargets(vars: string[]): void;
24
+ setSecrets(secrets: Record<string, unknown>): void;
25
+ setResource(name: string, props: Record<string, unknown>): void;
129
26
 
27
+ registerImport(alias: string, targetModule: string, kinds: string[]): void;
28
+ getInstance(name: string): unknown;
130
29
  getInvocable<TInput = Record<string, any>, TOutput = any>(
131
30
  name: string,
132
- ): Invocable<TInput, TOutput> {
133
- const instance = this.getInstance(name);
134
-
135
- if (
136
- instance &&
137
- typeof instance === "object" &&
138
- "invoke" in instance &&
139
- typeof instance.invoke !== "function"
140
- ) {
141
- throw new Error(`Resource '${name}' does not have an invoke() method.`);
142
- }
143
- return instance as Invocable<TInput, TOutput>;
144
- }
145
-
146
- /**
147
- * Resolve a fully-qualified kind like "Http.Server" to its real kind "http-server.Server".
148
- * Splits on the first dot, looks up the prefix in importAliases, validates against
149
- * importedKinds (if set), and reconstructs the resolved kind.
150
- * Throws with a clear message if the alias is unknown or the kind is not exported.
151
- */
152
- resolveKind(kind: string): string {
153
- const dot = kind.indexOf(".");
154
- if (dot === -1) {
155
- throw new Error(`Kind '${kind}' must be fully qualified (e.g. 'Module.KindName')`);
156
- }
157
- const prefix = kind.slice(0, dot);
158
- const suffix = kind.slice(dot + 1);
159
- const realModule = this.importAliases.get(prefix);
160
- if (!realModule) {
161
- const known = [...this.importAliases.keys()].join(", ") || "(none)";
162
- throw new Error(
163
- `Kind '${kind}': no module imported with alias '${prefix}'. Known aliases: ${known}`,
164
- );
165
- }
166
- const allowed = this.importedKinds.get(prefix);
167
- if (allowed !== undefined && !allowed.has(suffix)) {
168
- throw new Error(
169
- `Kind '${suffix}' is not exported by module '${realModule}' (imported as '${prefix}'). ` +
170
- `Exported kinds: ${[...allowed].join(", ")}`,
171
- );
172
- }
173
- return `${realModule}.${suffix}`;
174
- }
175
-
176
- private _rebuildContext(): void {
177
- this._context = {
178
- variables: this._variables,
179
- secrets: this._secrets,
180
- resources: this._resources,
181
- ...(this._hostEnv ? { env: lenientEnv(this._hostEnv) } : {}),
182
- };
183
- this._secretValues = collectSecretValues(this._secrets);
184
- }
185
-
186
- override async invoke<TInputs>(kind: string, name: string, inputs: TInputs): Promise<any> {
187
- const result = await super.invoke(kind, name, inputs);
188
- const entry = this.resourceInstances.get(name);
189
- if (entry && typeof (entry.instance as any).snapshot === "function") {
190
- const snap = await Promise.resolve((entry.instance as any).snapshot());
191
- this.setResource(name, snap as Record<string, unknown>);
192
- }
193
- return result;
194
- }
195
-
196
- async run(name: string) {
197
- const resource = this.resourceInstances.get(name);
198
- if (!resource) {
199
- throw new Error(
200
- `Target resource ${name} not found in module context. Available resources: ${[...this.resourceInstances.keys()].join(", ")}`,
201
- );
202
- }
203
- if (typeof resource.instance.run === "function") {
204
- await resource.instance.run();
205
- } else {
206
- throw new Error(`Target resource ${name} does not have a run() method.`);
207
- }
208
- }
209
-
210
- async runTargets() {
211
- for (const target of this.targets) {
212
- await this.run(target);
213
- }
214
- }
31
+ ): Invocable<TInput, TOutput>;
32
+ resolveKind(kind: string): string;
33
+ runTargets(): Promise<void>;
215
34
  }
@@ -1,21 +0,0 @@
1
- import { EvaluationContext } from "./evaluation-context.js";
2
- import { ModuleContext } from "./module-context.js";
3
-
4
- /**
5
- * The ephemeral, per-trigger context layer. Merges a ModuleContext with
6
- * arbitrary execution-time properties (e.g. { request, inputs } for HTTP;
7
- * any shape is valid — determined by the trigger type).
8
- *
9
- * Execution props overlay the module namespaces on key conflict.
10
- */
11
- export class ExecutionContext extends EvaluationContext {
12
- constructor(moduleCtx: ModuleContext, execProps: Record<string, unknown>) {
13
- super(
14
- moduleCtx.source,
15
- Object.assign(Object.create(null), moduleCtx.context, execProps) as Record<string, unknown>,
16
- moduleCtx.createInstance,
17
- moduleCtx.secretValues,
18
- moduleCtx.emit,
19
- );
20
- }
21
- }