@chainlink/cre-sdk 0.0.1-alpha → 0.0.2-alpha

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 (114) hide show
  1. package/dist/generated/capabilities/internal/nodeaction/v1/node_action_pb.d.ts +69 -0
  2. package/dist/generated/capabilities/internal/nodeaction/v1/node_action_pb.js +29 -0
  3. package/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.d.ts +13 -18
  4. package/dist/generated-sdk/capabilities/blockchain/evm/v1alpha/client_sdk_gen.js +71 -260
  5. package/dist/generated-sdk/capabilities/internal/actionandtrigger/v1/basic_sdk_gen.d.ts +4 -9
  6. package/dist/generated-sdk/capabilities/internal/actionandtrigger/v1/basic_sdk_gen.js +14 -45
  7. package/dist/generated-sdk/capabilities/internal/basicaction/v1/basicaction_sdk_gen.d.ts +3 -7
  8. package/dist/generated-sdk/capabilities/internal/basicaction/v1/basicaction_sdk_gen.js +9 -36
  9. package/dist/generated-sdk/capabilities/internal/basictrigger/v1/basic_sdk_gen.d.ts +2 -8
  10. package/dist/generated-sdk/capabilities/internal/basictrigger/v1/basic_sdk_gen.js +8 -21
  11. package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.d.ts +5 -8
  12. package/dist/generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen.js +16 -60
  13. package/dist/generated-sdk/capabilities/internal/nodeaction/v1/basicaction_sdk_gen.d.ts +19 -0
  14. package/dist/generated-sdk/capabilities/internal/nodeaction/v1/basicaction_sdk_gen.js +36 -0
  15. package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.d.ts +3 -7
  16. package/dist/generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen.js +9 -36
  17. package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.d.ts +2 -8
  18. package/dist/generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen.js +8 -21
  19. package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.d.ts +2 -8
  20. package/dist/generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen.js +8 -21
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +1 -0
  23. package/dist/sdk/cre/index.d.ts +5 -19
  24. package/dist/sdk/cre/index.js +1 -17
  25. package/dist/sdk/errors.d.ts +12 -0
  26. package/dist/sdk/errors.js +21 -0
  27. package/dist/sdk/impl/runtime-impl.d.ts +38 -0
  28. package/dist/sdk/impl/runtime-impl.js +190 -0
  29. package/dist/sdk/impl/runtime-impl.test.js +334 -0
  30. package/dist/sdk/index.d.ts +3 -0
  31. package/dist/sdk/index.js +2 -0
  32. package/dist/sdk/runtime.d.ts +24 -0
  33. package/dist/sdk/utils/capabilities/capability-error.d.ts +0 -3
  34. package/dist/sdk/utils/capabilities/capability-error.js +0 -2
  35. package/dist/sdk/utils/config/configHandler.test.d.ts +1 -0
  36. package/dist/sdk/utils/config/configHandler.test.js +193 -0
  37. package/dist/sdk/utils/config/index.d.ts +4 -6
  38. package/dist/sdk/utils/config/index.js +10 -34
  39. package/dist/sdk/utils/index.d.ts +0 -3
  40. package/dist/sdk/utils/index.js +0 -3
  41. package/dist/sdk/utils/values/consensus_aggregators.d.ts +1 -1
  42. package/dist/sdk/{runtime → wasm}/host-bindings.d.ts +0 -1
  43. package/dist/sdk/{runtime → wasm}/host-bindings.js +0 -4
  44. package/dist/sdk/wasm/index.d.ts +1 -0
  45. package/dist/sdk/wasm/index.js +1 -0
  46. package/dist/sdk/wasm/runner.d.ts +14 -0
  47. package/dist/sdk/wasm/runner.js +114 -0
  48. package/dist/sdk/wasm/runner.test.d.ts +1 -0
  49. package/dist/sdk/wasm/runner.test.js +270 -0
  50. package/dist/sdk/wasm/runtime.d.ts +7 -0
  51. package/dist/sdk/wasm/runtime.js +55 -0
  52. package/dist/sdk/workflow.d.ts +13 -15
  53. package/dist/sdk/workflow.js +1 -21
  54. package/package.json +2 -2
  55. package/dist/sdk/engine/execute.d.ts +0 -4
  56. package/dist/sdk/engine/execute.js +0 -12
  57. package/dist/sdk/engine/execute.test.js +0 -129
  58. package/dist/sdk/engine/handleExecutionPhase.d.ts +0 -4
  59. package/dist/sdk/engine/handleExecutionPhase.js +0 -34
  60. package/dist/sdk/engine/handleSubscribePhase.d.ts +0 -3
  61. package/dist/sdk/engine/handleSubscribePhase.js +0 -23
  62. package/dist/sdk/logger.d.ts +0 -7
  63. package/dist/sdk/logger.js +0 -18
  64. package/dist/sdk/runtime/errors.d.ts +0 -23
  65. package/dist/sdk/runtime/errors.js +0 -30
  66. package/dist/sdk/runtime/index.d.ts +0 -3
  67. package/dist/sdk/runtime/index.js +0 -2
  68. package/dist/sdk/runtime/run-in-node-mode.d.ts +0 -12
  69. package/dist/sdk/runtime/run-in-node-mode.js +0 -47
  70. package/dist/sdk/runtime/run-in-node-mode.test.js +0 -116
  71. package/dist/sdk/runtime/runtime.d.ts +0 -36
  72. package/dist/sdk/runtime/runtime.js +0 -84
  73. package/dist/sdk/runtime/runtime.test.d.ts +0 -1
  74. package/dist/sdk/runtime/runtime.test.js +0 -58
  75. package/dist/sdk/testhelpers/dangerously-call-capability.d.ts +0 -10
  76. package/dist/sdk/testhelpers/dangerously-call-capability.js +0 -26
  77. package/dist/sdk/testhelpers/mock-host-bindings.d.ts +0 -15
  78. package/dist/sdk/testhelpers/mock-host-bindings.js +0 -25
  79. package/dist/sdk/testhelpers/mock-runtime.d.ts +0 -2
  80. package/dist/sdk/testhelpers/mock-runtime.js +0 -16
  81. package/dist/sdk/utils/await-async-request.d.ts +0 -9
  82. package/dist/sdk/utils/await-async-request.js +0 -27
  83. package/dist/sdk/utils/capabilities/call-capability.d.ts +0 -22
  84. package/dist/sdk/utils/capabilities/call-capability.js +0 -33
  85. package/dist/sdk/utils/capabilities/callback-id.d.ts +0 -3
  86. package/dist/sdk/utils/capabilities/callback-id.js +0 -22
  87. package/dist/sdk/utils/capabilities/http/fetch.d.ts +0 -44
  88. package/dist/sdk/utils/capabilities/http/fetch.js +0 -63
  89. package/dist/sdk/utils/do-request-async.d.ts +0 -12
  90. package/dist/sdk/utils/do-request-async.js +0 -17
  91. package/dist/sdk/utils/error-boundary.d.ts +0 -2
  92. package/dist/sdk/utils/error-boundary.js +0 -30
  93. package/dist/sdk/utils/get-request.d.ts +0 -1
  94. package/dist/sdk/utils/get-request.js +0 -15
  95. package/dist/sdk/utils/lazy-promise.d.ts +0 -59
  96. package/dist/sdk/utils/lazy-promise.js +0 -81
  97. package/dist/sdk/utils/random/get-rand.d.ts +0 -3
  98. package/dist/sdk/utils/random/get-rand.js +0 -6
  99. package/dist/sdk/utils/random/random.d.ts +0 -35
  100. package/dist/sdk/utils/random/random.js +0 -123
  101. package/dist/sdk/utils/secrets/await-async-secret.d.ts +0 -1
  102. package/dist/sdk/utils/secrets/await-async-secret.js +0 -29
  103. package/dist/sdk/utils/secrets/do-get-secret.d.ts +0 -1
  104. package/dist/sdk/utils/secrets/do-get-secret.js +0 -14
  105. package/dist/sdk/utils/secrets/get-secret.d.ts +0 -1
  106. package/dist/sdk/utils/secrets/get-secret.js +0 -10
  107. package/dist/sdk/utils/send-error.d.ts +0 -1
  108. package/dist/sdk/utils/send-error.js +0 -16
  109. package/dist/sdk/utils/send-response-value.d.ts +0 -2
  110. package/dist/sdk/utils/send-response-value.js +0 -15
  111. package/dist/sdk/utils/time/get-time.d.ts +0 -16
  112. package/dist/sdk/utils/time/get-time.js +0 -21
  113. /package/dist/sdk/{engine/execute.test.d.ts → impl/runtime-impl.test.d.ts} +0 -0
  114. /package/dist/sdk/{runtime/run-in-node-mode.test.d.ts → runtime.js} +0 -0
@@ -1,44 +1,34 @@
1
- import { create, fromBinary, fromJson, toBinary } from '@bufbuild/protobuf';
2
- import { AnySchema } from '@bufbuild/protobuf/wkt';
1
+ import { create, fromJson } from '@bufbuild/protobuf';
2
+ import { AnySchema, anyPack } from '@bufbuild/protobuf/wkt';
3
3
  import { ConfigSchema, PayloadSchema, } from '../../../../../generated/capabilities/networking/http/v1alpha/trigger_pb';
4
- import { Mode } from '../../../../../generated/sdk/v1alpha/sdk_pb';
5
- import { callCapability } from '../../../../../sdk/utils/capabilities/call-capability';
6
- import { CapabilityError } from '../../../../../sdk/utils/capabilities/capability-error';
4
+ import {} from '../../../../../sdk/runtime';
7
5
  import {} from '../../../../../sdk/utils/triggers/trigger-interface';
8
- import { getTypeUrl } from '../../../../../sdk/utils/typeurl';
9
6
  /**
10
7
  * HTTP Capability
11
8
  *
12
9
  * Capability ID: http-trigger@1.0.0-alpha
13
- * Default Mode: Mode.DON
14
10
  * Capability Name: http-trigger
15
11
  * Capability Version: 1.0.0-alpha
16
12
  */
17
13
  export class HTTPCapability {
18
- mode;
19
14
  /** The capability ID for this service */
20
15
  static CAPABILITY_ID = 'http-trigger@1.0.0-alpha';
21
- /** The default execution mode for this capability */
22
- static DEFAULT_MODE = Mode.DON;
23
16
  static CAPABILITY_NAME = 'http-trigger';
24
17
  static CAPABILITY_VERSION = '1.0.0-alpha';
25
- constructor(mode = HTTPCapability.DEFAULT_MODE) {
26
- this.mode = mode;
27
- }
18
+ constructor() { }
28
19
  trigger(config) {
29
- return new HTTPTrigger(this.mode, config, HTTPCapability.CAPABILITY_ID, 'Trigger');
20
+ const capabilityId = HTTPCapability.CAPABILITY_ID;
21
+ return new HTTPTrigger(config, capabilityId, 'Trigger');
30
22
  }
31
23
  }
32
24
  /**
33
25
  * Trigger implementation for Trigger
34
26
  */
35
27
  class HTTPTrigger {
36
- mode;
37
28
  _capabilityId;
38
29
  _method;
39
30
  config;
40
- constructor(mode, config, _capabilityId, _method) {
41
- this.mode = mode;
31
+ constructor(config, _capabilityId, _method) {
42
32
  this._capabilityId = _capabilityId;
43
33
  this._method = _method;
44
34
  // biome-ignore lint/suspicious/noExplicitAny: Needed for runtime type checking of protocol buffer messages
@@ -56,10 +46,7 @@ class HTTPTrigger {
56
46
  return PayloadSchema;
57
47
  }
58
48
  configAsAny() {
59
- return create(AnySchema, {
60
- typeUrl: getTypeUrl(ConfigSchema),
61
- value: toBinary(ConfigSchema, this.config),
62
- });
49
+ return anyPack(ConfigSchema, this.config);
63
50
  }
64
51
  /**
65
52
  * Transform the raw trigger output - override this method if needed
@@ -1,35 +1,29 @@
1
1
  import { type Any } from '@bufbuild/protobuf/wkt';
2
2
  import { type Config, type ConfigJson, type Payload } from '../../../../../generated/capabilities/scheduler/cron/v1/trigger_pb';
3
- import { Mode } from '../../../../../generated/sdk/v1alpha/sdk_pb';
4
3
  import { type Trigger } from '../../../../../sdk/utils/triggers/trigger-interface';
5
4
  /**
6
5
  * Cron Capability
7
6
  *
8
7
  * Capability ID: cron-trigger@1.0.0
9
- * Default Mode: Mode.DON
10
8
  * Capability Name: cron-trigger
11
9
  * Capability Version: 1.0.0
12
10
  */
13
11
  export declare class CronCapability {
14
- private readonly mode;
15
12
  /** The capability ID for this service */
16
13
  static readonly CAPABILITY_ID = "cron-trigger@1.0.0";
17
- /** The default execution mode for this capability */
18
- static readonly DEFAULT_MODE = Mode.DON;
19
14
  static readonly CAPABILITY_NAME = "cron-trigger";
20
15
  static readonly CAPABILITY_VERSION = "1.0.0";
21
- constructor(mode?: Mode);
16
+ constructor();
22
17
  trigger(config: ConfigJson): CronTrigger;
23
18
  }
24
19
  /**
25
20
  * Trigger implementation for Trigger
26
21
  */
27
22
  declare class CronTrigger implements Trigger<Payload, Payload> {
28
- readonly mode: Mode;
29
23
  private readonly _capabilityId;
30
24
  private readonly _method;
31
25
  readonly config: Config;
32
- constructor(mode: Mode, config: Config | ConfigJson, _capabilityId: string, _method: string);
26
+ constructor(config: Config | ConfigJson, _capabilityId: string, _method: string);
33
27
  capabilityId(): string;
34
28
  method(): string;
35
29
  outputSchema(): import("@bufbuild/protobuf/codegenv2").GenMessage<Payload, {
@@ -1,44 +1,34 @@
1
- import { create, fromBinary, fromJson, toBinary } from '@bufbuild/protobuf';
2
- import { AnySchema } from '@bufbuild/protobuf/wkt';
1
+ import { create, fromJson } from '@bufbuild/protobuf';
2
+ import { AnySchema, anyPack } from '@bufbuild/protobuf/wkt';
3
3
  import { ConfigSchema, LegacyPayloadSchema, PayloadSchema, } from '../../../../../generated/capabilities/scheduler/cron/v1/trigger_pb';
4
- import { Mode } from '../../../../../generated/sdk/v1alpha/sdk_pb';
5
- import { callCapability } from '../../../../../sdk/utils/capabilities/call-capability';
6
- import { CapabilityError } from '../../../../../sdk/utils/capabilities/capability-error';
4
+ import {} from '../../../../../sdk/runtime';
7
5
  import {} from '../../../../../sdk/utils/triggers/trigger-interface';
8
- import { getTypeUrl } from '../../../../../sdk/utils/typeurl';
9
6
  /**
10
7
  * Cron Capability
11
8
  *
12
9
  * Capability ID: cron-trigger@1.0.0
13
- * Default Mode: Mode.DON
14
10
  * Capability Name: cron-trigger
15
11
  * Capability Version: 1.0.0
16
12
  */
17
13
  export class CronCapability {
18
- mode;
19
14
  /** The capability ID for this service */
20
15
  static CAPABILITY_ID = 'cron-trigger@1.0.0';
21
- /** The default execution mode for this capability */
22
- static DEFAULT_MODE = Mode.DON;
23
16
  static CAPABILITY_NAME = 'cron-trigger';
24
17
  static CAPABILITY_VERSION = '1.0.0';
25
- constructor(mode = CronCapability.DEFAULT_MODE) {
26
- this.mode = mode;
27
- }
18
+ constructor() { }
28
19
  trigger(config) {
29
- return new CronTrigger(this.mode, config, CronCapability.CAPABILITY_ID, 'Trigger');
20
+ const capabilityId = CronCapability.CAPABILITY_ID;
21
+ return new CronTrigger(config, capabilityId, 'Trigger');
30
22
  }
31
23
  }
32
24
  /**
33
25
  * Trigger implementation for Trigger
34
26
  */
35
27
  class CronTrigger {
36
- mode;
37
28
  _capabilityId;
38
29
  _method;
39
30
  config;
40
- constructor(mode, config, _capabilityId, _method) {
41
- this.mode = mode;
31
+ constructor(config, _capabilityId, _method) {
42
32
  this._capabilityId = _capabilityId;
43
33
  this._method = _method;
44
34
  // biome-ignore lint/suspicious/noExplicitAny: Needed for runtime type checking of protocol buffer messages
@@ -56,10 +46,7 @@ class CronTrigger {
56
46
  return PayloadSchema;
57
47
  }
58
48
  configAsAny() {
59
- return create(AnySchema, {
60
- typeUrl: getTypeUrl(ConfigSchema),
61
- value: toBinary(ConfigSchema, this.config),
62
- });
49
+ return anyPack(ConfigSchema, this.config);
63
50
  }
64
51
  /**
65
52
  * Transform the raw trigger output - override this method if needed
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './sdk';
2
2
  export * from './sdk/runtime';
3
3
  export * from './sdk/utils';
4
+ export * from './sdk/wasm';
package/dist/index.js CHANGED
@@ -2,3 +2,4 @@
2
2
  export * from './sdk';
3
3
  export * from './sdk/runtime';
4
4
  export * from './sdk/utils';
5
+ export * from './sdk/wasm';
@@ -5,9 +5,10 @@ import { ClientCapability as EVMClient } from '../../generated-sdk/capabilities/
5
5
  import { ClientCapability as HTTPClient } from '../../generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen';
6
6
  import { HTTPCapability } from '../../generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen';
7
7
  import { CronCapability } from '../../generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen';
8
- import { runInNodeMode } from '../runtime/run-in-node-mode';
9
- import { Runner } from '../workflow';
10
- export type { NodeRuntime, Runtime } from '../runtime/runtime';
8
+ export type { Log as EVMLog } from '../../generated/capabilities/blockchain/evm/v1alpha/client_pb';
9
+ export type { Payload as HTTPPayload } from '../../generated/capabilities/networking/http/v1alpha/trigger_pb';
10
+ export type { Payload as CronPayload } from '../../generated/capabilities/scheduler/cron/v1/trigger_pb';
11
+ export type { NodeRuntime, Runtime } from '../runtime';
11
12
  export declare const cre: {
12
13
  capabilities: {
13
14
  CronCapability: typeof CronCapability;
@@ -15,20 +16,5 @@ export declare const cre: {
15
16
  HTTPClient: typeof HTTPClient;
16
17
  EVMClient: typeof EVMClient;
17
18
  };
18
- config: <TConfig>({ configParser, configSchema, }?: import("../utils/config").ConfigHandlerParams) => Promise<TConfig>;
19
- handler: <TRawTriggerOutput extends import("@bufbuild/protobuf").Message<string>, TTriggerOutput = TRawTriggerOutput, TConfig = unknown>(trigger: import("../utils/triggers/trigger-interface").Trigger<TRawTriggerOutput, TTriggerOutput>, fn: import("../workflow").HandlerFn<TConfig, TTriggerOutput>) => import("../workflow").HandlerEntry<TConfig, TRawTriggerOutput, TTriggerOutput>;
20
- newRunner: typeof Runner.newRunner;
21
- runInNodeMode: typeof runInNodeMode;
22
- utils: {
23
- fetch: (input: import("../utils/capabilities/http/fetch").CreFetchRequest) => Promise<{
24
- statusCode: number;
25
- headers: {
26
- [key: string]: string;
27
- };
28
- body: string;
29
- }>;
30
- };
31
- sendResponseValue: (value: import("../utils").Value) => void;
32
- sendError: (error: string | Error) => void;
33
- withErrorBoundary: (fn: () => Promise<void>) => Promise<void>;
19
+ handler: <TRawTriggerOutput extends import("@bufbuild/protobuf").Message<string>, TTriggerOutput, TConfig, TResult>(trigger: import("../utils/triggers/trigger-interface").Trigger<TRawTriggerOutput, TTriggerOutput>, fn: import("../workflow").HandlerFn<TConfig, TTriggerOutput, TResult>) => import("../workflow").HandlerEntry<TConfig, TRawTriggerOutput, TTriggerOutput, TResult>;
34
20
  };
@@ -5,16 +5,9 @@ import { ClientCapability as EVMClient } from '../../generated-sdk/capabilities/
5
5
  import { ClientCapability as HTTPClient } from '../../generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen';
6
6
  import { HTTPCapability } from '../../generated-sdk/capabilities/networking/http/v1alpha/http_sdk_gen';
7
7
  import { CronCapability } from '../../generated-sdk/capabilities/scheduler/cron/v1/cron_sdk_gen';
8
- import { runInNodeMode } from '../runtime/run-in-node-mode';
9
- import { creFetch } from '../utils/capabilities/http/fetch';
10
- import { configHandler } from '../utils/config';
11
- import { withErrorBoundary } from '../utils/error-boundary';
12
8
  import { prepareRuntime } from '../utils/prepare-runtime';
13
- import { sendError } from '../utils/send-error';
14
- import { sendResponseValue } from '../utils/send-response-value';
15
- import { handler, Runner } from '../workflow';
9
+ import { handler } from '../workflow';
16
10
  prepareRuntime();
17
- versionV2();
18
11
  export const cre = {
19
12
  capabilities: {
20
13
  CronCapability,
@@ -22,14 +15,5 @@ export const cre = {
22
15
  HTTPClient,
23
16
  EVMClient,
24
17
  },
25
- config: configHandler,
26
18
  handler,
27
- newRunner: Runner.newRunner,
28
- runInNodeMode,
29
- utils: {
30
- fetch: creFetch,
31
- },
32
- sendResponseValue,
33
- sendError,
34
- withErrorBoundary,
35
19
  };
@@ -0,0 +1,12 @@
1
+ import { type SecretRequest } from '../generated/sdk/v1alpha/sdk_pb';
2
+ export declare class DonModeError extends Error {
3
+ constructor();
4
+ }
5
+ export declare class NodeModeError extends Error {
6
+ constructor();
7
+ }
8
+ export declare class SecretsError extends Error {
9
+ sceretRequest: SecretRequest;
10
+ error: String;
11
+ constructor(sceretRequest: SecretRequest, error: String);
12
+ }
@@ -0,0 +1,21 @@
1
+ import { Mode } from '../generated/sdk/v1alpha/sdk_pb';
2
+ export class DonModeError extends Error {
3
+ constructor() {
4
+ super('cannot use Runtime inside RunInNodeMode');
5
+ }
6
+ }
7
+ export class NodeModeError extends Error {
8
+ constructor() {
9
+ super('cannot use NodeRuntime outside RunInNodeMode');
10
+ this.name = 'NodeModeError';
11
+ }
12
+ }
13
+ export class SecretsError extends Error {
14
+ sceretRequest;
15
+ error;
16
+ constructor(sceretRequest, error) {
17
+ super(`error fetching ${sceretRequest}: ${error}`);
18
+ this.sceretRequest = sceretRequest;
19
+ this.error = error;
20
+ }
21
+ }
@@ -0,0 +1,38 @@
1
+ import { type Message } from '@bufbuild/protobuf';
2
+ import { type AwaitCapabilitiesRequest, type AwaitCapabilitiesResponse, type AwaitSecretsRequest, type AwaitSecretsResponse, type CapabilityRequest, type GetSecretsRequest, Mode, type Secret, type SecretRequest, type SecretRequestJson } from '../../generated/sdk/v1alpha/sdk_pb';
3
+ import type { BaseRuntime, CallCapabilityParams, NodeRuntime, Runtime } from '../runtime';
4
+ import { type ConsensusAggregation, type PrimitiveTypes, type UnwrapOptions } from '../utils';
5
+ export declare class BaseRuntimeImpl<C> implements BaseRuntime<C> {
6
+ config: C;
7
+ nextCallId: number;
8
+ protected helpers: RuntimeHelpers;
9
+ protected maxResponseSize: bigint;
10
+ private mode;
11
+ modeError?: Error;
12
+ constructor(config: C, nextCallId: number, helpers: RuntimeHelpers, maxResponseSize: bigint, mode: Mode);
13
+ callCapability<I extends Message, O extends Message>({ capabilityId, method, payload, inputSchema, outputSchema, }: CallCapabilityParams<I, O>): {
14
+ result: () => Promise<O>;
15
+ };
16
+ getNextCallId(): number;
17
+ now(): Date;
18
+ }
19
+ export declare class NodeRuntimeImpl<C> extends BaseRuntimeImpl<C> implements NodeRuntime<C> {
20
+ _isNodeRuntime: true;
21
+ constructor(config: C, nextCallId: number, helpers: RuntimeHelpers, maxResponseSize: bigint);
22
+ }
23
+ export declare class RuntimeImpl<C> extends BaseRuntimeImpl<C> implements Runtime<C> {
24
+ private nextNodeCallId;
25
+ constructor(config: C, nextCallId: number, helpers: RuntimeHelpers, maxResponseSize: bigint);
26
+ runInNodeMode<TArgs extends any[], TOutput>(fn: (nodeRuntime: NodeRuntime<C>, ...args: TArgs) => Promise<TOutput> | TOutput, consesusAggretation: ConsensusAggregation<TOutput, true>, unwrapOptions?: TOutput extends PrimitiveTypes ? never : UnwrapOptions<TOutput>): (...args: TArgs) => Promise<TOutput>;
27
+ getSecret(request: SecretRequest | SecretRequestJson): {
28
+ result: () => Promise<Secret>;
29
+ };
30
+ }
31
+ export interface RuntimeHelpers {
32
+ call(request: CapabilityRequest): boolean;
33
+ await(request: AwaitCapabilitiesRequest, maxResponseSize: bigint): AwaitCapabilitiesResponse;
34
+ getSecrets(request: GetSecretsRequest, maxResponseSize: bigint): boolean;
35
+ awaitSecrets(request: AwaitSecretsRequest, maxResponseSize: bigint): AwaitSecretsResponse;
36
+ switchModes(mode: Mode): void;
37
+ now(): number;
38
+ }
@@ -0,0 +1,190 @@
1
+ import { create } from '@bufbuild/protobuf';
2
+ import { anyPack, anyUnpack } from '@bufbuild/protobuf/wkt';
3
+ import { AwaitCapabilitiesRequestSchema, AwaitSecretsRequestSchema, CapabilityRequestSchema, GetSecretsRequestSchema, Mode, SecretRequestSchema, SimpleConsensusInputsSchema, } from '../../generated/sdk/v1alpha/sdk_pb';
4
+ import { ConsensusCapability } from '../../generated-sdk/capabilities/internal/consensus/v1alpha/consensus_sdk_gen';
5
+ import { Value, } from '../utils';
6
+ import { CapabilityError } from '../utils/capabilities/capability-error';
7
+ import { DonModeError, NodeModeError, SecretsError } from '../errors';
8
+ export class BaseRuntimeImpl {
9
+ config;
10
+ nextCallId;
11
+ helpers;
12
+ maxResponseSize;
13
+ mode;
14
+ // modeError must only be set from within NodeRuntimeImpl
15
+ modeError;
16
+ constructor(config, nextCallId, helpers, maxResponseSize, mode) {
17
+ this.config = config;
18
+ this.nextCallId = nextCallId;
19
+ this.helpers = helpers;
20
+ this.maxResponseSize = maxResponseSize;
21
+ this.mode = mode;
22
+ }
23
+ callCapability({ capabilityId, method, payload, inputSchema, outputSchema, }) {
24
+ if (this.modeError) {
25
+ return { result: () => Promise.reject(this.modeError) };
26
+ }
27
+ // nextCallId tracks the unique id for a request to the WASM host.
28
+ // to avoid collisions of the ID in different modes, it is
29
+ // incremented in DON mode and decremented in Node mode.
30
+ // eg. - first call don mode: nextCallId = 1
31
+ // - second call: nextCallId = 2
32
+ // - first call node mode: nextCallId = -1
33
+ // - second call node mode: nextCallId = -2
34
+ // - etc...
35
+ const anyPayload = anyPack(inputSchema, payload);
36
+ const callbackId = this.nextCallId;
37
+ if (this.mode == Mode.DON)
38
+ this.nextCallId++;
39
+ else
40
+ this.nextCallId--;
41
+ const req = create(CapabilityRequestSchema, {
42
+ id: capabilityId,
43
+ method,
44
+ payload: anyPayload,
45
+ callbackId,
46
+ });
47
+ if (!this.helpers.call(req)) {
48
+ return {
49
+ result: () => {
50
+ return Promise.reject(new CapabilityError(`Capability not found ${capabilityId}`, {
51
+ callbackId,
52
+ method,
53
+ capabilityId,
54
+ }));
55
+ },
56
+ };
57
+ }
58
+ return {
59
+ result: async () => {
60
+ const awaitRequest = create(AwaitCapabilitiesRequestSchema, { ids: [callbackId] });
61
+ const awaitResponse = this.helpers.await(awaitRequest, this.maxResponseSize);
62
+ const capabilityResponse = awaitResponse.responses[callbackId];
63
+ if (!capabilityResponse) {
64
+ throw new CapabilityError(`No response found for callback ID ${callbackId}`, {
65
+ capabilityId,
66
+ method,
67
+ callbackId,
68
+ });
69
+ }
70
+ const response = capabilityResponse.response;
71
+ switch (response.case) {
72
+ case 'payload':
73
+ return anyUnpack(response.value, outputSchema);
74
+ case 'error':
75
+ throw new CapabilityError(`Error ${response.value}`, {
76
+ capabilityId,
77
+ method,
78
+ callbackId,
79
+ });
80
+ default:
81
+ throw new CapabilityError(`Error cannot unwrap ${response.case}`, {
82
+ capabilityId,
83
+ method,
84
+ callbackId,
85
+ });
86
+ }
87
+ },
88
+ };
89
+ }
90
+ getNextCallId() {
91
+ return this.nextCallId;
92
+ }
93
+ now() {
94
+ // ns to ms
95
+ return new Date(this.helpers.now() / 1000000);
96
+ }
97
+ }
98
+ export class NodeRuntimeImpl extends BaseRuntimeImpl {
99
+ _isNodeRuntime = true;
100
+ constructor(config, nextCallId, helpers, maxResponseSize) {
101
+ helpers.switchModes(Mode.NODE);
102
+ super(config, nextCallId, helpers, maxResponseSize, Mode.NODE);
103
+ }
104
+ }
105
+ export class RuntimeImpl extends BaseRuntimeImpl {
106
+ nextNodeCallId = -1;
107
+ constructor(config, nextCallId, helpers, maxResponseSize) {
108
+ helpers.switchModes(Mode.DON);
109
+ super(config, nextCallId, helpers, maxResponseSize, Mode.DON);
110
+ }
111
+ runInNodeMode(fn, consesusAggretation, unwrapOptions) {
112
+ return async (...args) => {
113
+ this.modeError = new DonModeError();
114
+ const nodeRuntime = new NodeRuntimeImpl(this.config, this.nextNodeCallId, this.helpers, this.maxResponseSize);
115
+ const consensusInput = create(SimpleConsensusInputsSchema, {
116
+ descriptors: consesusAggretation.descriptor,
117
+ });
118
+ if (consesusAggretation.defaultValue) {
119
+ // This cast is safe, since ConsensusAggregation can only have true its second argument if T extends CreSerializable<TOutput>
120
+ consensusInput.default = Value.from(consesusAggretation.defaultValue).proto();
121
+ }
122
+ try {
123
+ const observation = await fn(nodeRuntime, ...args);
124
+ // This cast is safe, since ConsensusAggregation can only have true its second argument if T extends CreSerializable<TOutput>
125
+ consensusInput.observation = {
126
+ case: 'value',
127
+ value: Value.from(observation).proto(),
128
+ };
129
+ }
130
+ catch (e) {
131
+ consensusInput.observation = { case: 'error', value: e.message || String(e) };
132
+ }
133
+ finally {
134
+ // Always restore DON mode before invoking consensus
135
+ this.modeError = undefined;
136
+ this.nextNodeCallId = nodeRuntime.nextCallId;
137
+ nodeRuntime.modeError = new NodeModeError();
138
+ this.helpers.switchModes(Mode.DON);
139
+ }
140
+ const consensus = new ConsensusCapability();
141
+ const result = await consensus.simple(this, consensusInput).result();
142
+ const wrappedValue = Value.wrap(result);
143
+ return unwrapOptions
144
+ ? wrappedValue.unwrapToType(unwrapOptions)
145
+ : wrappedValue.unwrap();
146
+ };
147
+ }
148
+ getSecret(request) {
149
+ if (this.modeError) {
150
+ return { result: () => Promise.reject(this.modeError) };
151
+ }
152
+ const secretRequest = request.$typeName
153
+ ? create(SecretRequestSchema, request)
154
+ : request;
155
+ const id = this.nextCallId;
156
+ this.nextCallId++;
157
+ const secretsReq = create(GetSecretsRequestSchema, {
158
+ callbackId: id,
159
+ requests: [request],
160
+ });
161
+ if (!this.helpers.getSecrets(secretsReq, this.maxResponseSize)) {
162
+ return {
163
+ result: () => Promise.reject(new SecretsError(secretRequest, 'host is not making the secrets request')),
164
+ };
165
+ }
166
+ return {
167
+ result: async () => {
168
+ const awaitRequest = create(AwaitSecretsRequestSchema, { ids: [id] });
169
+ const awaitResponse = this.helpers.awaitSecrets(awaitRequest, this.maxResponseSize);
170
+ const secretsResponse = awaitResponse.responses[id];
171
+ if (!secretsResponse) {
172
+ throw new SecretsError(secretRequest, 'no response');
173
+ }
174
+ const responses = secretsResponse.responses;
175
+ if (responses.length != 1) {
176
+ throw new SecretsError(secretRequest, 'invalid value returned from host');
177
+ }
178
+ const response = responses[0].response;
179
+ switch (response.case) {
180
+ case 'secret':
181
+ return response.value;
182
+ case 'error':
183
+ throw new SecretsError(secretRequest, response.value.error);
184
+ default:
185
+ throw new SecretsError(secretRequest, 'cannot unmashal returned value from host');
186
+ }
187
+ },
188
+ };
189
+ }
190
+ }