@hotmeshio/hotmesh 0.4.0 → 0.4.1

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 (283) hide show
  1. package/build/modules/enums.d.ts +110 -0
  2. package/build/modules/enums.js +134 -0
  3. package/build/modules/errors.d.ts +124 -0
  4. package/build/modules/errors.js +191 -0
  5. package/build/modules/key.d.ts +66 -0
  6. package/build/modules/key.js +190 -0
  7. package/build/modules/storage.d.ts +3 -0
  8. package/build/modules/storage.js +5 -0
  9. package/build/modules/utils.d.ts +119 -0
  10. package/build/modules/utils.js +374 -0
  11. package/build/package.json +1 -1
  12. package/build/services/activities/activity.d.ts +104 -0
  13. package/build/services/activities/activity.js +549 -0
  14. package/build/services/activities/await.d.ts +12 -0
  15. package/build/services/activities/await.js +114 -0
  16. package/build/services/activities/cycle.d.ts +19 -0
  17. package/build/services/activities/cycle.js +112 -0
  18. package/build/services/activities/hook.d.ts +27 -0
  19. package/build/services/activities/hook.js +168 -0
  20. package/build/services/activities/index.d.ts +19 -0
  21. package/build/services/activities/index.js +20 -0
  22. package/build/services/activities/interrupt.d.ts +16 -0
  23. package/build/services/activities/interrupt.js +158 -0
  24. package/build/services/activities/signal.d.ts +20 -0
  25. package/build/services/activities/signal.js +134 -0
  26. package/build/services/activities/trigger.d.ts +37 -0
  27. package/build/services/activities/trigger.js +246 -0
  28. package/build/services/activities/worker.d.ts +12 -0
  29. package/build/services/activities/worker.js +106 -0
  30. package/build/services/collator/index.d.ts +111 -0
  31. package/build/services/collator/index.js +293 -0
  32. package/build/services/compiler/deployer.d.ts +40 -0
  33. package/build/services/compiler/deployer.js +488 -0
  34. package/build/services/compiler/index.d.ts +32 -0
  35. package/build/services/compiler/index.js +112 -0
  36. package/build/services/compiler/validator.d.ts +34 -0
  37. package/build/services/compiler/validator.js +147 -0
  38. package/build/services/connector/factory.d.ts +22 -0
  39. package/build/services/connector/factory.js +99 -0
  40. package/build/services/connector/index.d.ts +30 -0
  41. package/build/services/connector/index.js +54 -0
  42. package/build/services/connector/providers/ioredis.d.ts +9 -0
  43. package/build/services/connector/providers/ioredis.js +26 -0
  44. package/build/services/connector/providers/nats.d.ts +9 -0
  45. package/build/services/connector/providers/nats.js +34 -0
  46. package/build/services/connector/providers/postgres.d.ts +20 -0
  47. package/build/services/connector/providers/postgres.js +102 -0
  48. package/build/services/connector/providers/redis.d.ts +9 -0
  49. package/build/services/connector/providers/redis.js +38 -0
  50. package/build/services/engine/index.d.ts +264 -0
  51. package/build/services/engine/index.js +761 -0
  52. package/build/services/exporter/index.d.ts +44 -0
  53. package/build/services/exporter/index.js +126 -0
  54. package/build/services/hotmesh/index.d.ts +483 -0
  55. package/build/services/hotmesh/index.js +622 -0
  56. package/build/services/logger/index.d.ts +16 -0
  57. package/build/services/logger/index.js +54 -0
  58. package/build/services/mapper/index.d.ts +28 -0
  59. package/build/services/mapper/index.js +81 -0
  60. package/build/services/memflow/client.d.ts +108 -0
  61. package/build/services/memflow/client.js +372 -0
  62. package/build/services/memflow/connection.d.ts +23 -0
  63. package/build/services/memflow/connection.js +33 -0
  64. package/build/services/memflow/context.d.ts +143 -0
  65. package/build/services/memflow/context.js +299 -0
  66. package/build/services/memflow/exporter.d.ts +51 -0
  67. package/build/services/memflow/exporter.js +215 -0
  68. package/build/services/memflow/handle.d.ts +90 -0
  69. package/build/services/memflow/handle.js +176 -0
  70. package/build/services/memflow/index.d.ts +116 -0
  71. package/build/services/memflow/index.js +122 -0
  72. package/build/services/memflow/schemas/factory.d.ts +29 -0
  73. package/build/services/memflow/schemas/factory.js +2492 -0
  74. package/build/services/memflow/search.d.ts +142 -0
  75. package/build/services/memflow/search.js +320 -0
  76. package/build/services/memflow/worker.d.ts +124 -0
  77. package/build/services/memflow/worker.js +514 -0
  78. package/build/services/memflow/workflow/all.d.ts +7 -0
  79. package/build/services/memflow/workflow/all.js +15 -0
  80. package/build/services/memflow/workflow/common.d.ts +20 -0
  81. package/build/services/memflow/workflow/common.js +47 -0
  82. package/build/services/memflow/workflow/context.d.ts +6 -0
  83. package/build/services/memflow/workflow/context.js +45 -0
  84. package/build/services/memflow/workflow/contextMethods.d.ts +14 -0
  85. package/build/services/memflow/workflow/contextMethods.js +33 -0
  86. package/build/services/memflow/workflow/didRun.d.ts +7 -0
  87. package/build/services/memflow/workflow/didRun.js +22 -0
  88. package/build/services/memflow/workflow/emit.d.ts +11 -0
  89. package/build/services/memflow/workflow/emit.js +29 -0
  90. package/build/services/memflow/workflow/enrich.d.ts +9 -0
  91. package/build/services/memflow/workflow/enrich.js +17 -0
  92. package/build/services/memflow/workflow/execChild.d.ts +18 -0
  93. package/build/services/memflow/workflow/execChild.js +102 -0
  94. package/build/services/memflow/workflow/execHook.d.ts +65 -0
  95. package/build/services/memflow/workflow/execHook.js +73 -0
  96. package/build/services/memflow/workflow/hook.d.ts +9 -0
  97. package/build/services/memflow/workflow/hook.js +56 -0
  98. package/build/services/memflow/workflow/index.d.ts +74 -0
  99. package/build/services/memflow/workflow/index.js +87 -0
  100. package/build/services/memflow/workflow/interrupt.d.ts +9 -0
  101. package/build/services/memflow/workflow/interrupt.js +24 -0
  102. package/build/services/memflow/workflow/isSideEffectAllowed.d.ts +10 -0
  103. package/build/services/memflow/workflow/isSideEffectAllowed.js +33 -0
  104. package/build/services/memflow/workflow/proxyActivities.d.ts +20 -0
  105. package/build/services/memflow/workflow/proxyActivities.js +97 -0
  106. package/build/services/memflow/workflow/random.d.ts +6 -0
  107. package/build/services/memflow/workflow/random.js +16 -0
  108. package/build/services/memflow/workflow/searchMethods.d.ts +6 -0
  109. package/build/services/memflow/workflow/searchMethods.js +25 -0
  110. package/build/services/memflow/workflow/signal.d.ts +7 -0
  111. package/build/services/memflow/workflow/signal.js +28 -0
  112. package/build/services/memflow/workflow/sleepFor.d.ts +8 -0
  113. package/build/services/memflow/workflow/sleepFor.js +35 -0
  114. package/build/services/memflow/workflow/trace.d.ts +14 -0
  115. package/build/services/memflow/workflow/trace.js +33 -0
  116. package/build/services/memflow/workflow/waitFor.d.ts +8 -0
  117. package/build/services/memflow/workflow/waitFor.js +35 -0
  118. package/build/services/meshcall/index.d.ts +194 -0
  119. package/build/services/meshcall/index.js +452 -0
  120. package/build/services/meshcall/schemas/factory.d.ts +9 -0
  121. package/build/services/meshcall/schemas/factory.js +189 -0
  122. package/build/services/meshdata/index.d.ts +795 -0
  123. package/build/services/meshdata/index.js +1235 -0
  124. package/build/services/meshos/index.d.ts +293 -0
  125. package/build/services/meshos/index.js +547 -0
  126. package/build/services/pipe/functions/array.d.ts +17 -0
  127. package/build/services/pipe/functions/array.js +74 -0
  128. package/build/services/pipe/functions/bitwise.d.ts +9 -0
  129. package/build/services/pipe/functions/bitwise.js +24 -0
  130. package/build/services/pipe/functions/conditional.d.ts +13 -0
  131. package/build/services/pipe/functions/conditional.js +36 -0
  132. package/build/services/pipe/functions/cron.d.ts +12 -0
  133. package/build/services/pipe/functions/cron.js +40 -0
  134. package/build/services/pipe/functions/date.d.ts +58 -0
  135. package/build/services/pipe/functions/date.js +171 -0
  136. package/build/services/pipe/functions/index.d.ts +29 -0
  137. package/build/services/pipe/functions/index.js +30 -0
  138. package/build/services/pipe/functions/json.d.ts +5 -0
  139. package/build/services/pipe/functions/json.js +12 -0
  140. package/build/services/pipe/functions/logical.d.ts +5 -0
  141. package/build/services/pipe/functions/logical.js +12 -0
  142. package/build/services/pipe/functions/math.d.ts +42 -0
  143. package/build/services/pipe/functions/math.js +184 -0
  144. package/build/services/pipe/functions/number.d.ts +21 -0
  145. package/build/services/pipe/functions/number.js +60 -0
  146. package/build/services/pipe/functions/object.d.ts +25 -0
  147. package/build/services/pipe/functions/object.js +81 -0
  148. package/build/services/pipe/functions/string.d.ts +23 -0
  149. package/build/services/pipe/functions/string.js +69 -0
  150. package/build/services/pipe/functions/symbol.d.ts +12 -0
  151. package/build/services/pipe/functions/symbol.js +33 -0
  152. package/build/services/pipe/functions/unary.d.ts +7 -0
  153. package/build/services/pipe/functions/unary.js +18 -0
  154. package/build/services/pipe/index.d.ts +48 -0
  155. package/build/services/pipe/index.js +242 -0
  156. package/build/services/quorum/index.d.ts +90 -0
  157. package/build/services/quorum/index.js +263 -0
  158. package/build/services/reporter/index.d.ts +50 -0
  159. package/build/services/reporter/index.js +348 -0
  160. package/build/services/router/config/index.d.ts +11 -0
  161. package/build/services/router/config/index.js +36 -0
  162. package/build/services/router/consumption/index.d.ts +34 -0
  163. package/build/services/router/consumption/index.js +395 -0
  164. package/build/services/router/error-handling/index.d.ts +8 -0
  165. package/build/services/router/error-handling/index.js +98 -0
  166. package/build/services/router/index.d.ts +57 -0
  167. package/build/services/router/index.js +121 -0
  168. package/build/services/router/lifecycle/index.d.ts +27 -0
  169. package/build/services/router/lifecycle/index.js +80 -0
  170. package/build/services/router/telemetry/index.d.ts +11 -0
  171. package/build/services/router/telemetry/index.js +32 -0
  172. package/build/services/router/throttling/index.d.ts +23 -0
  173. package/build/services/router/throttling/index.js +76 -0
  174. package/build/services/search/factory.d.ts +7 -0
  175. package/build/services/search/factory.js +24 -0
  176. package/build/services/search/index.d.ts +23 -0
  177. package/build/services/search/index.js +10 -0
  178. package/build/services/search/providers/postgres/postgres.d.ts +25 -0
  179. package/build/services/search/providers/postgres/postgres.js +149 -0
  180. package/build/services/search/providers/redis/ioredis.d.ts +19 -0
  181. package/build/services/search/providers/redis/ioredis.js +121 -0
  182. package/build/services/search/providers/redis/redis.d.ts +19 -0
  183. package/build/services/search/providers/redis/redis.js +134 -0
  184. package/build/services/serializer/index.d.ts +42 -0
  185. package/build/services/serializer/index.js +282 -0
  186. package/build/services/store/cache.d.ts +67 -0
  187. package/build/services/store/cache.js +128 -0
  188. package/build/services/store/factory.d.ts +8 -0
  189. package/build/services/store/factory.js +24 -0
  190. package/build/services/store/index.d.ts +89 -0
  191. package/build/services/store/index.js +9 -0
  192. package/build/services/store/providers/postgres/kvsql.d.ts +168 -0
  193. package/build/services/store/providers/postgres/kvsql.js +198 -0
  194. package/build/services/store/providers/postgres/kvtables.d.ts +20 -0
  195. package/build/services/store/providers/postgres/kvtables.js +441 -0
  196. package/build/services/store/providers/postgres/kvtransaction.d.ts +36 -0
  197. package/build/services/store/providers/postgres/kvtransaction.js +248 -0
  198. package/build/services/store/providers/postgres/kvtypes/hash.d.ts +60 -0
  199. package/build/services/store/providers/postgres/kvtypes/hash.js +1287 -0
  200. package/build/services/store/providers/postgres/kvtypes/list.d.ts +33 -0
  201. package/build/services/store/providers/postgres/kvtypes/list.js +194 -0
  202. package/build/services/store/providers/postgres/kvtypes/string.d.ts +20 -0
  203. package/build/services/store/providers/postgres/kvtypes/string.js +115 -0
  204. package/build/services/store/providers/postgres/kvtypes/zset.d.ts +41 -0
  205. package/build/services/store/providers/postgres/kvtypes/zset.js +214 -0
  206. package/build/services/store/providers/postgres/postgres.d.ts +145 -0
  207. package/build/services/store/providers/postgres/postgres.js +1036 -0
  208. package/build/services/store/providers/redis/_base.d.ts +137 -0
  209. package/build/services/store/providers/redis/_base.js +980 -0
  210. package/build/services/store/providers/redis/ioredis.d.ts +20 -0
  211. package/build/services/store/providers/redis/ioredis.js +180 -0
  212. package/build/services/store/providers/redis/redis.d.ts +18 -0
  213. package/build/services/store/providers/redis/redis.js +199 -0
  214. package/build/services/store/providers/store-initializable.d.ts +5 -0
  215. package/build/services/store/providers/store-initializable.js +2 -0
  216. package/build/services/stream/factory.d.ts +8 -0
  217. package/build/services/stream/factory.js +37 -0
  218. package/build/services/stream/index.d.ts +69 -0
  219. package/build/services/stream/index.js +11 -0
  220. package/build/services/stream/providers/nats/nats.d.ts +60 -0
  221. package/build/services/stream/providers/nats/nats.js +225 -0
  222. package/build/services/stream/providers/postgres/kvtables.d.ts +3 -0
  223. package/build/services/stream/providers/postgres/kvtables.js +146 -0
  224. package/build/services/stream/providers/postgres/postgres.d.ts +107 -0
  225. package/build/services/stream/providers/postgres/postgres.js +519 -0
  226. package/build/services/stream/providers/redis/ioredis.d.ts +61 -0
  227. package/build/services/stream/providers/redis/ioredis.js +272 -0
  228. package/build/services/stream/providers/redis/redis.d.ts +61 -0
  229. package/build/services/stream/providers/redis/redis.js +305 -0
  230. package/build/services/stream/providers/stream-initializable.d.ts +4 -0
  231. package/build/services/stream/providers/stream-initializable.js +2 -0
  232. package/build/services/sub/factory.d.ts +7 -0
  233. package/build/services/sub/factory.js +29 -0
  234. package/build/services/sub/index.d.ts +22 -0
  235. package/build/services/sub/index.js +10 -0
  236. package/build/services/sub/providers/nats/nats.d.ts +19 -0
  237. package/build/services/sub/providers/nats/nats.js +105 -0
  238. package/build/services/sub/providers/postgres/postgres.d.ts +19 -0
  239. package/build/services/sub/providers/postgres/postgres.js +92 -0
  240. package/build/services/sub/providers/redis/ioredis.d.ts +17 -0
  241. package/build/services/sub/providers/redis/ioredis.js +81 -0
  242. package/build/services/sub/providers/redis/redis.d.ts +17 -0
  243. package/build/services/sub/providers/redis/redis.js +72 -0
  244. package/build/services/task/index.d.ts +36 -0
  245. package/build/services/task/index.js +206 -0
  246. package/build/services/telemetry/index.d.ts +52 -0
  247. package/build/services/telemetry/index.js +306 -0
  248. package/build/services/worker/index.d.ts +77 -0
  249. package/build/services/worker/index.js +197 -0
  250. package/package.json +1 -1
  251. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  252. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  253. package/typedoc.json +0 -47
  254. package/types/activity.ts +0 -268
  255. package/types/app.ts +0 -20
  256. package/types/async.ts +0 -6
  257. package/types/cache.ts +0 -1
  258. package/types/collator.ts +0 -9
  259. package/types/error.ts +0 -56
  260. package/types/exporter.ts +0 -102
  261. package/types/hook.ts +0 -44
  262. package/types/hotmesh.ts +0 -314
  263. package/types/index.ts +0 -306
  264. package/types/job.ts +0 -233
  265. package/types/logger.ts +0 -8
  266. package/types/manifest.ts +0 -70
  267. package/types/map.ts +0 -5
  268. package/types/memflow.ts +0 -645
  269. package/types/meshcall.ts +0 -235
  270. package/types/meshdata.ts +0 -278
  271. package/types/ms.d.ts +0 -7
  272. package/types/nats.ts +0 -270
  273. package/types/pipe.ts +0 -90
  274. package/types/postgres.ts +0 -114
  275. package/types/provider.ts +0 -161
  276. package/types/quorum.ts +0 -167
  277. package/types/redis.ts +0 -404
  278. package/types/serializer.ts +0 -40
  279. package/types/stats.ts +0 -117
  280. package/types/stream.ts +0 -231
  281. package/types/task.ts +0 -7
  282. package/types/telemetry.ts +0 -16
  283. package/types/transition.ts +0 -20
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Permanent-Memory Workflows & AI Agents",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -0,0 +1,104 @@
1
+ import { EngineService } from '../engine';
2
+ import { ILogger } from '../logger';
3
+ import { StoreService } from '../store';
4
+ import { TelemetryService } from '../telemetry';
5
+ import { ActivityData, ActivityLeg, ActivityMetadata, ActivityType } from '../../types/activity';
6
+ import { ProviderClient, ProviderTransaction, TransactionResultList } from '../../types/provider';
7
+ import { JobState, JobStatus } from '../../types/job';
8
+ import { StringAnyType } from '../../types/serializer';
9
+ import { StreamCode, StreamData, StreamStatus } from '../../types/stream';
10
+ /**
11
+ * The base class for all activities
12
+ */
13
+ declare class Activity {
14
+ config: ActivityType;
15
+ data: ActivityData;
16
+ hook: ActivityData;
17
+ metadata: ActivityMetadata;
18
+ store: StoreService<ProviderClient, ProviderTransaction>;
19
+ context: JobState;
20
+ engine: EngineService;
21
+ logger: ILogger;
22
+ status: StreamStatus;
23
+ code: StreamCode;
24
+ leg: ActivityLeg;
25
+ adjacencyList: StreamData[];
26
+ adjacentIndex: number;
27
+ constructor(config: ActivityType, data: ActivityData, metadata: ActivityMetadata, hook: ActivityData | null, engine: EngineService, context?: JobState);
28
+ setLeg(leg: ActivityLeg): void;
29
+ /**
30
+ * A job is assumed to be complete when its status (a semaphore)
31
+ * reaches `0`. A different threshold can be set in the
32
+ * activity YAML, in support of Dynamic Activation Control.
33
+ */
34
+ mapStatusThreshold(): number;
35
+ /**
36
+ * Upon entering leg 1 of a duplexed activity
37
+ */
38
+ verifyEntry(): Promise<void>;
39
+ /**
40
+ * Upon entering leg 2 of a duplexed activity
41
+ */
42
+ verifyReentry(): Promise<number>;
43
+ processEvent(status?: StreamStatus, code?: StreamCode, type?: 'hook' | 'output'): Promise<void>;
44
+ processPending(type: 'hook' | 'output'): Promise<TransactionResultList>;
45
+ processSuccess(type: 'hook' | 'output'): Promise<TransactionResultList>;
46
+ processError(): Promise<TransactionResultList>;
47
+ transitionAdjacent(multiResponse: TransactionResultList, telemetry: TelemetryService): Promise<void>;
48
+ resolveStatus(multiResponse: TransactionResultList): number;
49
+ mapJobData(): void;
50
+ mapInputData(): void;
51
+ mapOutputData(): void;
52
+ registerTimeout(): Promise<void>;
53
+ /**
54
+ * Any StreamMessage with a status of ERROR is bound to the activity
55
+ */
56
+ bindActivityError(data: Record<string, unknown>): void;
57
+ /**
58
+ * unhandled activity errors (activities that return an ERROR StreamMessage
59
+ * status and have no adjacent children to transition to) are bound to the job
60
+ */
61
+ bindJobError(data: Record<string, unknown>): void;
62
+ getTriggerConfig(): Promise<ActivityType>;
63
+ getJobStatus(): null | number;
64
+ setStatus(amount: number, transaction?: ProviderTransaction): Promise<void | any>;
65
+ authorizeEntry(state: StringAnyType): string[];
66
+ bindDimensionalAddress(state: StringAnyType): void;
67
+ setState(transaction?: ProviderTransaction): Promise<string>;
68
+ bindJobMetadata(): void;
69
+ bindActivityMetadata(): void;
70
+ bindJobState(state: StringAnyType): Promise<void>;
71
+ bindActivityState(state: StringAnyType): void;
72
+ bindJobMetadataPaths(): string[];
73
+ bindActivityMetadataPaths(): string[];
74
+ getState(): Promise<void>;
75
+ /**
76
+ * if the job is created/deleted/created with the same key,
77
+ * the 'gid' ensures no stale messages (such as sleep delays)
78
+ * enter the workstream. Any message with a mismatched gid
79
+ * belongs to a prior job and can safely be ignored/dropped.
80
+ */
81
+ assertGenerationalId(jobGID: string, msgGID?: string): void;
82
+ initDimensionalAddress(dad: string): void;
83
+ initSelf(context: StringAnyType): JobState;
84
+ initPolicies(context: JobState): void;
85
+ bindActivityData(type: 'output' | 'hook'): void;
86
+ resolveDad(): string;
87
+ resolveAdjacentDad(): string;
88
+ filterAdjacent(): Promise<StreamData[]>;
89
+ isJobComplete(jobStatus: JobStatus): boolean;
90
+ shouldEmit(): boolean;
91
+ /**
92
+ * emits the job completed event while leaving the job active, allowing
93
+ * a `main` thread to exit while other threads continue to run.
94
+ * @private
95
+ */
96
+ shouldPersistJob(): boolean;
97
+ transition(adjacencyList: StreamData[], jobStatus: JobStatus): Promise<string[]>;
98
+ /**
99
+ * A job with a vale < -100_000_000 is considered interrupted,
100
+ * as the interruption event decrements the job status by 1billion.
101
+ */
102
+ jobWasInterrupted(jobStatus: JobStatus): boolean;
103
+ }
104
+ export { Activity, ActivityType };
@@ -0,0 +1,549 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Activity = void 0;
4
+ const enums_1 = require("../../modules/enums");
5
+ const errors_1 = require("../../modules/errors");
6
+ const utils_1 = require("../../modules/utils");
7
+ const collator_1 = require("../collator");
8
+ const mapper_1 = require("../mapper");
9
+ const pipe_1 = require("../pipe");
10
+ const serializer_1 = require("../serializer");
11
+ const telemetry_1 = require("../telemetry");
12
+ const stream_1 = require("../../types/stream");
13
+ /**
14
+ * The base class for all activities
15
+ */
16
+ class Activity {
17
+ constructor(config, data, metadata, hook, engine, context) {
18
+ this.status = stream_1.StreamStatus.SUCCESS;
19
+ this.code = 200;
20
+ this.adjacentIndex = 0;
21
+ this.config = config;
22
+ this.data = data;
23
+ this.metadata = metadata;
24
+ this.hook = hook;
25
+ this.engine = engine;
26
+ this.context = context || { data: {}, metadata: {} };
27
+ this.logger = engine.logger;
28
+ this.store = engine.store;
29
+ }
30
+ setLeg(leg) {
31
+ this.leg = leg;
32
+ }
33
+ /**
34
+ * A job is assumed to be complete when its status (a semaphore)
35
+ * reaches `0`. A different threshold can be set in the
36
+ * activity YAML, in support of Dynamic Activation Control.
37
+ */
38
+ mapStatusThreshold() {
39
+ if (this.config.statusThreshold !== undefined) {
40
+ const threshold = pipe_1.Pipe.resolve(this.config.statusThreshold, this.context);
41
+ if (threshold !== undefined && !isNaN(Number(threshold))) {
42
+ return threshold;
43
+ }
44
+ }
45
+ return 0;
46
+ }
47
+ /**
48
+ * Upon entering leg 1 of a duplexed activity
49
+ */
50
+ async verifyEntry() {
51
+ this.setLeg(1);
52
+ await this.getState();
53
+ const threshold = this.mapStatusThreshold();
54
+ try {
55
+ collator_1.CollatorService.assertJobActive(this.context.metadata.js, this.context.metadata.jid, this.metadata.aid, threshold);
56
+ }
57
+ catch (error) {
58
+ await collator_1.CollatorService.notarizeEntry(this);
59
+ if (threshold > 0) {
60
+ if (this.context.metadata.js === threshold) {
61
+ //conclude job EXACTLY ONCE
62
+ const status = await this.setStatus(-threshold);
63
+ if (Number(status) === 0) {
64
+ await this.engine.runJobCompletionTasks(this.context);
65
+ }
66
+ }
67
+ }
68
+ else {
69
+ throw error;
70
+ }
71
+ return;
72
+ }
73
+ await collator_1.CollatorService.notarizeEntry(this);
74
+ }
75
+ /**
76
+ * Upon entering leg 2 of a duplexed activity
77
+ */
78
+ async verifyReentry() {
79
+ const guid = this.context.metadata.guid;
80
+ this.setLeg(2);
81
+ await this.getState();
82
+ collator_1.CollatorService.assertJobActive(this.context.metadata.js, this.context.metadata.jid, this.metadata.aid);
83
+ return await collator_1.CollatorService.notarizeReentry(this, guid);
84
+ }
85
+ //******** DUPLEX RE-ENTRY POINT ********//
86
+ async processEvent(status = stream_1.StreamStatus.SUCCESS, code = 200, type = 'output') {
87
+ this.setLeg(2);
88
+ const jid = this.context.metadata.jid;
89
+ if (!jid) {
90
+ this.logger.error('activity-process-event-error', {
91
+ message: 'job id is undefined',
92
+ });
93
+ return;
94
+ }
95
+ const aid = this.metadata.aid;
96
+ this.status = status;
97
+ this.code = code;
98
+ this.logger.debug('activity-process-event', {
99
+ topic: this.config.subtype,
100
+ jid,
101
+ aid,
102
+ status,
103
+ code,
104
+ });
105
+ let telemetry;
106
+ try {
107
+ const collationKey = await this.verifyReentry();
108
+ this.adjacentIndex = collator_1.CollatorService.getDimensionalIndex(collationKey);
109
+ telemetry = new telemetry_1.TelemetryService(this.engine.appId, this.config, this.metadata, this.context);
110
+ telemetry.startActivitySpan(this.leg);
111
+ let multiResponse;
112
+ if (status === stream_1.StreamStatus.PENDING) {
113
+ multiResponse = await this.processPending(type);
114
+ }
115
+ else if (status === stream_1.StreamStatus.SUCCESS) {
116
+ multiResponse = await this.processSuccess(type);
117
+ }
118
+ else {
119
+ multiResponse = await this.processError();
120
+ }
121
+ this.transitionAdjacent(multiResponse, telemetry);
122
+ }
123
+ catch (error) {
124
+ if (error instanceof errors_1.CollationError) {
125
+ this.logger.info(`process-event-${error.fault}-error`, { error });
126
+ return;
127
+ }
128
+ else if (error instanceof errors_1.InactiveJobError) {
129
+ this.logger.info('process-event-inactive-job-error', { error });
130
+ return;
131
+ }
132
+ else if (error instanceof errors_1.GenerationalError) {
133
+ this.logger.info('process-event-generational-job-error', { error });
134
+ return;
135
+ }
136
+ else if (error instanceof errors_1.GetStateError) {
137
+ this.logger.info('process-event-get-job-error', { error });
138
+ return;
139
+ }
140
+ this.logger.error('activity-process-event-error', {
141
+ error,
142
+ message: error.message,
143
+ stack: error.stack,
144
+ name: error.name,
145
+ });
146
+ telemetry?.setActivityError(error.message);
147
+ throw error;
148
+ }
149
+ finally {
150
+ telemetry?.endActivitySpan();
151
+ this.logger.debug('activity-process-event-end', { jid, aid });
152
+ }
153
+ }
154
+ async processPending(type) {
155
+ this.bindActivityData(type);
156
+ this.adjacencyList = await this.filterAdjacent();
157
+ this.mapJobData();
158
+ const transaction = this.store.transact();
159
+ await this.setState(transaction);
160
+ await collator_1.CollatorService.notarizeContinuation(this, transaction);
161
+ await this.setStatus(this.adjacencyList.length, transaction);
162
+ return (await transaction.exec());
163
+ }
164
+ async processSuccess(type) {
165
+ this.bindActivityData(type);
166
+ this.adjacencyList = await this.filterAdjacent();
167
+ this.mapJobData();
168
+ const transaction = this.store.transact();
169
+ await this.setState(transaction);
170
+ await collator_1.CollatorService.notarizeCompletion(this, transaction);
171
+ await this.setStatus(this.adjacencyList.length - 1, transaction);
172
+ return (await transaction.exec());
173
+ }
174
+ async processError() {
175
+ this.bindActivityError(this.data);
176
+ this.adjacencyList = await this.filterAdjacent();
177
+ if (!this.adjacencyList.length) {
178
+ this.bindJobError(this.data);
179
+ }
180
+ this.mapJobData();
181
+ const transaction = this.store.transact();
182
+ await this.setState(transaction);
183
+ await collator_1.CollatorService.notarizeCompletion(this, transaction);
184
+ await this.setStatus(this.adjacencyList.length - 1, transaction);
185
+ return (await transaction.exec());
186
+ }
187
+ async transitionAdjacent(multiResponse, telemetry) {
188
+ telemetry.mapActivityAttributes();
189
+ const jobStatus = this.resolveStatus(multiResponse);
190
+ const attrs = { 'app.job.jss': jobStatus };
191
+ //adjacencyList membership has already been set at this point (according to activity status)
192
+ const messageIds = await this.transition(this.adjacencyList, jobStatus);
193
+ if (messageIds?.length) {
194
+ attrs['app.activity.mids'] = messageIds.join(',');
195
+ }
196
+ telemetry.setActivityAttributes(attrs);
197
+ }
198
+ resolveStatus(multiResponse) {
199
+ const activityStatus = multiResponse[multiResponse.length - 1];
200
+ if (Array.isArray(activityStatus)) {
201
+ return Number(activityStatus[1]);
202
+ }
203
+ else {
204
+ return Number(activityStatus);
205
+ }
206
+ }
207
+ mapJobData() {
208
+ if (this.config.job?.maps) {
209
+ const mapper = new mapper_1.MapperService((0, utils_1.deepCopy)(this.config.job.maps), this.context);
210
+ const output = mapper.mapRules();
211
+ if (output) {
212
+ for (const key in output) {
213
+ const f1 = key.indexOf('[');
214
+ //keys with array notation suffix `somekey[]` represent
215
+ //dynamically-keyed mappings whose `value` must be moved to the output.
216
+ //The `value` must be an object with keys appropriate to the
217
+ //notation type: `somekey[0] (array)`, `somekey[-] (mark)`, OR `somekey[_] (search)`
218
+ if (f1 > -1) {
219
+ const amount = key.substring(f1 + 1).split(']')[0];
220
+ if (!isNaN(Number(amount))) {
221
+ const left = key.substring(0, f1);
222
+ output[left] = output[key];
223
+ delete output[key];
224
+ }
225
+ else if (amount === '-' || amount === '_') {
226
+ const obj = output[key];
227
+ Object.keys(obj).forEach((newKey) => {
228
+ output[newKey] = obj[newKey];
229
+ });
230
+ }
231
+ }
232
+ }
233
+ }
234
+ this.context.data = output;
235
+ }
236
+ }
237
+ mapInputData() {
238
+ if (this.config.input?.maps) {
239
+ const mapper = new mapper_1.MapperService((0, utils_1.deepCopy)(this.config.input.maps), this.context);
240
+ this.context.data = mapper.mapRules();
241
+ }
242
+ }
243
+ mapOutputData() {
244
+ //activity YAML may include output map data that produces/extends activity output data.
245
+ if (this.config.output?.maps) {
246
+ const mapper = new mapper_1.MapperService((0, utils_1.deepCopy)(this.config.output.maps), this.context);
247
+ const actOutData = mapper.mapRules();
248
+ const activityId = this.metadata.aid;
249
+ const data = { ...this.context[activityId].output, ...actOutData };
250
+ this.context[activityId].output.data = data;
251
+ }
252
+ }
253
+ async registerTimeout() {
254
+ //set timeout in support of hook and/or duplex
255
+ }
256
+ /**
257
+ * Any StreamMessage with a status of ERROR is bound to the activity
258
+ */
259
+ bindActivityError(data) {
260
+ const md = this.context[this.metadata.aid].output.metadata;
261
+ md.err = JSON.stringify(this.data);
262
+ //(temporary...useful for mapping error parts in the app.yaml)
263
+ md.$error = { ...data, is_stream_error: true };
264
+ }
265
+ /**
266
+ * unhandled activity errors (activities that return an ERROR StreamMessage
267
+ * status and have no adjacent children to transition to) are bound to the job
268
+ */
269
+ bindJobError(data) {
270
+ this.context.metadata.err = JSON.stringify({
271
+ ...data,
272
+ is_stream_error: true,
273
+ });
274
+ }
275
+ async getTriggerConfig() {
276
+ return await this.store.getSchema(this.config.trigger, await this.engine.getVID());
277
+ }
278
+ getJobStatus() {
279
+ return null;
280
+ }
281
+ async setStatus(amount, transaction) {
282
+ const { id: appId } = await this.engine.getVID();
283
+ return await this.store.setStatus(amount, this.context.metadata.jid, appId, transaction);
284
+ }
285
+ authorizeEntry(state) {
286
+ //pre-authorize activity state to allow entry for adjacent activities
287
+ return (this.adjacencyList?.map((streamData) => {
288
+ const { metadata: { aid }, } = streamData;
289
+ state[`${aid}/output/metadata/as`] = collator_1.CollatorService.getSeed();
290
+ return aid;
291
+ }) ?? []);
292
+ }
293
+ bindDimensionalAddress(state) {
294
+ const dad = this.resolveDad();
295
+ state[`${this.metadata.aid}/output/metadata/dad`] = dad;
296
+ }
297
+ async setState(transaction) {
298
+ const jobId = this.context.metadata.jid;
299
+ this.bindJobMetadata();
300
+ this.bindActivityMetadata();
301
+ const state = {};
302
+ await this.bindJobState(state);
303
+ const presets = this.authorizeEntry(state);
304
+ this.bindDimensionalAddress(state);
305
+ this.bindActivityState(state);
306
+ //symbolNames holds symkeys
307
+ const symbolNames = [
308
+ `$${this.config.subscribes}`,
309
+ this.metadata.aid,
310
+ ...presets,
311
+ ];
312
+ const dIds = collator_1.CollatorService.getDimensionsById([...this.config.ancestors, this.metadata.aid], this.resolveDad());
313
+ return await this.store.setState(state, this.getJobStatus(), jobId, symbolNames, dIds, transaction);
314
+ }
315
+ bindJobMetadata() {
316
+ //both legs of the most recently run activity (1 and 2) modify ju (job_updated)
317
+ this.context.metadata.ju = (0, utils_1.formatISODate)(new Date());
318
+ }
319
+ bindActivityMetadata() {
320
+ const self = this.context['$self'];
321
+ if (!self.output.metadata) {
322
+ self.output.metadata = {};
323
+ }
324
+ if (this.status === stream_1.StreamStatus.ERROR) {
325
+ self.output.metadata.err = JSON.stringify(this.data);
326
+ }
327
+ const ts = (0, utils_1.formatISODate)(new Date());
328
+ self.output.metadata.ac = ts;
329
+ self.output.metadata.au = ts;
330
+ self.output.metadata.atp = this.config.type;
331
+ if (this.config.subtype) {
332
+ self.output.metadata.stp = this.config.subtype;
333
+ }
334
+ self.output.metadata.aid = this.metadata.aid;
335
+ }
336
+ async bindJobState(state) {
337
+ const triggerConfig = await this.getTriggerConfig();
338
+ const PRODUCES = [
339
+ ...(triggerConfig.PRODUCES || []),
340
+ ...this.bindJobMetadataPaths(),
341
+ ];
342
+ for (const path of PRODUCES) {
343
+ const value = (0, utils_1.getValueByPath)(this.context, path);
344
+ if (value !== undefined) {
345
+ state[path] = value;
346
+ }
347
+ }
348
+ for (const key in this.context?.data ?? {}) {
349
+ if (key.startsWith('-') || key.startsWith('_')) {
350
+ state[key] = this.context.data[key];
351
+ }
352
+ }
353
+ telemetry_1.TelemetryService.bindJobTelemetryToState(state, this.config, this.context);
354
+ }
355
+ bindActivityState(state) {
356
+ const produces = [
357
+ ...this.config.produces,
358
+ ...this.bindActivityMetadataPaths(),
359
+ ];
360
+ for (const path of produces) {
361
+ const prefixedPath = `${this.metadata.aid}/${path}`;
362
+ const value = (0, utils_1.getValueByPath)(this.context, prefixedPath);
363
+ if (value !== undefined) {
364
+ state[prefixedPath] = value;
365
+ }
366
+ }
367
+ telemetry_1.TelemetryService.bindActivityTelemetryToState(state, this.config, this.metadata, this.context, this.leg);
368
+ }
369
+ bindJobMetadataPaths() {
370
+ return serializer_1.MDATA_SYMBOLS.JOB_UPDATE.KEYS.map((key) => `metadata/${key}`);
371
+ }
372
+ bindActivityMetadataPaths() {
373
+ const keys_to_save = this.leg === 1 ? 'ACTIVITY' : 'ACTIVITY_UPDATE';
374
+ return serializer_1.MDATA_SYMBOLS[keys_to_save].KEYS.map((key) => `output/metadata/${key}`);
375
+ }
376
+ async getState() {
377
+ const gid = this.context.metadata.gid;
378
+ const jobSymbolHashName = `$${this.config.subscribes}`;
379
+ const consumes = {
380
+ [jobSymbolHashName]: serializer_1.MDATA_SYMBOLS.JOB.KEYS.map((key) => `metadata/${key}`),
381
+ };
382
+ for (let [activityId, paths] of Object.entries(this.config.consumes)) {
383
+ if (activityId === '$job') {
384
+ for (const path of paths) {
385
+ consumes[jobSymbolHashName].push(path);
386
+ }
387
+ }
388
+ else {
389
+ if (activityId === '$self') {
390
+ activityId = this.metadata.aid;
391
+ }
392
+ if (!consumes[activityId]) {
393
+ consumes[activityId] = [];
394
+ }
395
+ for (const path of paths) {
396
+ consumes[activityId].push(`${activityId}/${path}`);
397
+ }
398
+ }
399
+ }
400
+ telemetry_1.TelemetryService.addTargetTelemetryPaths(consumes, this.config, this.metadata, this.leg);
401
+ const { dad, jid } = this.context.metadata;
402
+ const dIds = collator_1.CollatorService.getDimensionsById([...this.config.ancestors, this.metadata.aid], dad || '');
403
+ //`state` is a unidimensional hash; context is a tree
404
+ const [state, _status] = await this.store.getState(jid, consumes, dIds);
405
+ this.context = (0, utils_1.restoreHierarchy)(state);
406
+ this.assertGenerationalId(this.context?.metadata?.gid, gid);
407
+ this.initDimensionalAddress(dad);
408
+ this.initSelf(this.context);
409
+ this.initPolicies(this.context);
410
+ }
411
+ /**
412
+ * if the job is created/deleted/created with the same key,
413
+ * the 'gid' ensures no stale messages (such as sleep delays)
414
+ * enter the workstream. Any message with a mismatched gid
415
+ * belongs to a prior job and can safely be ignored/dropped.
416
+ */
417
+ assertGenerationalId(jobGID, msgGID) {
418
+ if (msgGID !== jobGID) {
419
+ throw new errors_1.GenerationalError(jobGID, msgGID, this.context?.metadata?.jid ?? '', this.context?.metadata?.aid ?? '', this.context?.metadata?.dad ?? '');
420
+ }
421
+ }
422
+ initDimensionalAddress(dad) {
423
+ this.metadata.dad = dad;
424
+ }
425
+ initSelf(context) {
426
+ const activityId = this.metadata.aid;
427
+ if (!context[activityId]) {
428
+ context[activityId] = {};
429
+ }
430
+ const self = context[activityId];
431
+ if (!self.output) {
432
+ self.output = {};
433
+ }
434
+ if (!self.input) {
435
+ self.input = {};
436
+ }
437
+ if (!self.hook) {
438
+ self.hook = {};
439
+ }
440
+ if (!self.output.metadata) {
441
+ self.output.metadata = {};
442
+ }
443
+ //prebind the updated timestamp (mappings need the time)
444
+ self.output.metadata.au = (0, utils_1.formatISODate)(new Date());
445
+ context['$self'] = self;
446
+ context['$job'] = context; //NEVER call STRINGIFY! (now circular)
447
+ return context;
448
+ }
449
+ initPolicies(context) {
450
+ const expire = pipe_1.Pipe.resolve(this.config.expire ?? enums_1.HMSH_EXPIRE_DURATION, context);
451
+ context.metadata.expire = expire;
452
+ if (this.config.persistent != undefined) {
453
+ const persistent = pipe_1.Pipe.resolve(this.config.persistent ?? false, context);
454
+ context.metadata.persistent = persistent;
455
+ }
456
+ }
457
+ bindActivityData(type) {
458
+ this.context[this.metadata.aid][type].data = this.data;
459
+ }
460
+ resolveDad() {
461
+ let dad = this.metadata.dad;
462
+ if (this.adjacentIndex > 0) {
463
+ //if adjacent index > 0 the activity is cycling; replace last index with cycle index
464
+ dad = `${dad.substring(0, dad.lastIndexOf(','))},${this.adjacentIndex}`;
465
+ }
466
+ return dad;
467
+ }
468
+ resolveAdjacentDad() {
469
+ //concat self and child dimension (all children (leg 1) begin life at 0)
470
+ return `${this.resolveDad()}${collator_1.CollatorService.getDimensionalSeed(0)}`;
471
+ }
472
+ async filterAdjacent() {
473
+ const adjacencyList = [];
474
+ const transitions = await this.store.getTransitions(await this.engine.getVID());
475
+ const transition = transitions[`.${this.metadata.aid}`];
476
+ //resolve the dimensional address for adjacent children
477
+ const adjacentDad = this.resolveAdjacentDad();
478
+ if (transition) {
479
+ for (const toActivityId in transition) {
480
+ const transitionRule = transition[toActivityId];
481
+ if (mapper_1.MapperService.evaluate(transitionRule, this.context, this.code)) {
482
+ adjacencyList.push({
483
+ metadata: {
484
+ guid: (0, utils_1.guid)(),
485
+ jid: this.context.metadata.jid,
486
+ gid: this.context.metadata.gid,
487
+ dad: adjacentDad,
488
+ aid: toActivityId,
489
+ spn: this.context['$self'].output.metadata?.l2s,
490
+ trc: this.context.metadata.trc,
491
+ },
492
+ type: stream_1.StreamDataType.TRANSITION,
493
+ data: {},
494
+ });
495
+ }
496
+ }
497
+ }
498
+ return adjacencyList;
499
+ }
500
+ isJobComplete(jobStatus) {
501
+ return jobStatus <= 0;
502
+ }
503
+ shouldEmit() {
504
+ if (this.config.emit) {
505
+ return pipe_1.Pipe.resolve(this.config.emit, this.context) === true;
506
+ }
507
+ return false;
508
+ }
509
+ /**
510
+ * emits the job completed event while leaving the job active, allowing
511
+ * a `main` thread to exit while other threads continue to run.
512
+ * @private
513
+ */
514
+ shouldPersistJob() {
515
+ if (this.config.persist !== undefined) {
516
+ return pipe_1.Pipe.resolve(this.config.persist, this.context) === true;
517
+ }
518
+ return false;
519
+ }
520
+ async transition(adjacencyList, jobStatus) {
521
+ if (this.jobWasInterrupted(jobStatus)) {
522
+ return;
523
+ }
524
+ let mIds = [];
525
+ if (this.shouldEmit() ||
526
+ this.isJobComplete(jobStatus) ||
527
+ this.shouldPersistJob()) {
528
+ await this.engine.runJobCompletionTasks(this.context, {
529
+ emit: !this.isJobComplete(jobStatus) && !this.shouldPersistJob(),
530
+ });
531
+ }
532
+ if (adjacencyList.length && !this.isJobComplete(jobStatus)) {
533
+ const transaction = this.store.transact();
534
+ for (const execSignal of adjacencyList) {
535
+ await this.engine.router?.publishMessage(null, execSignal, transaction);
536
+ }
537
+ mIds = (await transaction.exec());
538
+ }
539
+ return mIds;
540
+ }
541
+ /**
542
+ * A job with a vale < -100_000_000 is considered interrupted,
543
+ * as the interruption event decrements the job status by 1billion.
544
+ */
545
+ jobWasInterrupted(jobStatus) {
546
+ return jobStatus < -100000000;
547
+ }
548
+ }
549
+ exports.Activity = Activity;
@@ -0,0 +1,12 @@
1
+ import { EngineService } from '../engine';
2
+ import { ActivityData, ActivityMetadata, AwaitActivity, ActivityType } from '../../types/activity';
3
+ import { ProviderTransaction } from '../../types/provider';
4
+ import { JobState } from '../../types/job';
5
+ import { Activity } from './activity';
6
+ declare class Await extends Activity {
7
+ config: AwaitActivity;
8
+ constructor(config: ActivityType, data: ActivityData, metadata: ActivityMetadata, hook: ActivityData | null, engine: EngineService, context?: JobState);
9
+ process(): Promise<string>;
10
+ execActivity(transaction: ProviderTransaction): Promise<string>;
11
+ }
12
+ export { Await };