@mokronos/wfkit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -0
  3. package/dist/authoring.d.ts +7 -0
  4. package/dist/authoring.d.ts.map +1 -0
  5. package/dist/authoring.js +8 -0
  6. package/dist/authoring.js.map +1 -0
  7. package/dist/cli/integrations.d.ts +3 -0
  8. package/dist/cli/integrations.d.ts.map +1 -0
  9. package/dist/cli/integrations.js +40 -0
  10. package/dist/cli/integrations.js.map +1 -0
  11. package/dist/cli/main.d.ts +3 -0
  12. package/dist/cli/main.d.ts.map +1 -0
  13. package/dist/cli/main.js +591 -0
  14. package/dist/cli/main.js.map +1 -0
  15. package/dist/core.d.ts +198 -0
  16. package/dist/core.d.ts.map +1 -0
  17. package/dist/core.js +1061 -0
  18. package/dist/core.js.map +1 -0
  19. package/dist/errors.d.ts +3 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/errors.js +13 -0
  22. package/dist/errors.js.map +1 -0
  23. package/dist/events.d.ts +154 -0
  24. package/dist/events.d.ts.map +1 -0
  25. package/dist/events.js +24 -0
  26. package/dist/events.js.map +1 -0
  27. package/dist/index.d.ts +15 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +11 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/replay.d.ts +3 -0
  32. package/dist/replay.d.ts.map +1 -0
  33. package/dist/replay.js +28 -0
  34. package/dist/replay.js.map +1 -0
  35. package/dist/runtime.d.ts +70 -0
  36. package/dist/runtime.d.ts.map +1 -0
  37. package/dist/runtime.js +183 -0
  38. package/dist/runtime.js.map +1 -0
  39. package/dist/schema.d.ts +18 -0
  40. package/dist/schema.d.ts.map +1 -0
  41. package/dist/schema.js +19 -0
  42. package/dist/schema.js.map +1 -0
  43. package/dist/schemas.d.ts +1198 -0
  44. package/dist/schemas.d.ts.map +1 -0
  45. package/dist/schemas.js +383 -0
  46. package/dist/schemas.js.map +1 -0
  47. package/dist/sdk/artifact.d.ts +41 -0
  48. package/dist/sdk/artifact.d.ts.map +1 -0
  49. package/dist/sdk/artifact.js +94 -0
  50. package/dist/sdk/artifact.js.map +1 -0
  51. package/dist/sdk/graph.d.ts +15 -0
  52. package/dist/sdk/graph.d.ts.map +1 -0
  53. package/dist/sdk/graph.js +388 -0
  54. package/dist/sdk/graph.js.map +1 -0
  55. package/dist/sdk/index.d.ts +14 -0
  56. package/dist/sdk/index.d.ts.map +1 -0
  57. package/dist/sdk/index.js +8 -0
  58. package/dist/sdk/index.js.map +1 -0
  59. package/dist/sdk/integrations.d.ts +18 -0
  60. package/dist/sdk/integrations.d.ts.map +1 -0
  61. package/dist/sdk/integrations.js +20 -0
  62. package/dist/sdk/integrations.js.map +1 -0
  63. package/dist/sdk/json.d.ts +4 -0
  64. package/dist/sdk/json.d.ts.map +1 -0
  65. package/dist/sdk/json.js +31 -0
  66. package/dist/sdk/json.js.map +1 -0
  67. package/dist/sdk/loader.d.ts +10 -0
  68. package/dist/sdk/loader.d.ts.map +1 -0
  69. package/dist/sdk/loader.js +75 -0
  70. package/dist/sdk/loader.js.map +1 -0
  71. package/dist/sdk/sdk.d.ts +102 -0
  72. package/dist/sdk/sdk.d.ts.map +1 -0
  73. package/dist/sdk/sdk.js +669 -0
  74. package/dist/sdk/sdk.js.map +1 -0
  75. package/dist/sdk/sqlite.d.ts +8 -0
  76. package/dist/sdk/sqlite.d.ts.map +1 -0
  77. package/dist/sdk/sqlite.js +288 -0
  78. package/dist/sdk/sqlite.js.map +1 -0
  79. package/dist/signal.d.ts +17 -0
  80. package/dist/signal.d.ts.map +1 -0
  81. package/dist/signal.js +78 -0
  82. package/dist/signal.js.map +1 -0
  83. package/dist/testing/index.d.ts +35 -0
  84. package/dist/testing/index.d.ts.map +1 -0
  85. package/dist/testing/index.js +244 -0
  86. package/dist/testing/index.js.map +1 -0
  87. package/package.json +67 -0
  88. package/src/authoring.ts +32 -0
  89. package/src/cli/integrations.ts +75 -0
  90. package/src/cli/main.ts +793 -0
  91. package/src/core.ts +1515 -0
  92. package/src/errors.ts +19 -0
  93. package/src/events.ts +37 -0
  94. package/src/index.ts +109 -0
  95. package/src/replay.ts +29 -0
  96. package/src/runtime.ts +314 -0
  97. package/src/schema.ts +34 -0
  98. package/src/schemas.ts +473 -0
  99. package/src/sdk/artifact.ts +176 -0
  100. package/src/sdk/graph.ts +486 -0
  101. package/src/sdk/index.ts +64 -0
  102. package/src/sdk/integrations.ts +49 -0
  103. package/src/sdk/json.ts +40 -0
  104. package/src/sdk/loader.ts +99 -0
  105. package/src/sdk/sdk.ts +911 -0
  106. package/src/sdk/sqlite.ts +405 -0
  107. package/src/signal.ts +116 -0
  108. package/src/testing/index.ts +341 -0
@@ -0,0 +1,99 @@
1
+ import { DefinedWorkflowTypeId, type DefinedWorkflow } from "../core.ts"
2
+ import type { WorkflowArtifact } from "./artifact.ts"
3
+
4
+ export interface LoadedWorkflow {
5
+ readonly artifact: WorkflowArtifact
6
+ readonly exportName: string
7
+ readonly workflow: DefinedWorkflow
8
+ }
9
+
10
+ export const isDefinedWorkflow = (value: unknown): value is DefinedWorkflow => {
11
+ if ((typeof value !== "object" && typeof value !== "function") || value === null) {
12
+ return false
13
+ }
14
+ return DefinedWorkflowTypeId in value
15
+ }
16
+
17
+ interface WorkflowModule {
18
+ readonly default?: unknown
19
+ readonly [exportName: string]: unknown
20
+ }
21
+
22
+ const importArtifactModule = async (
23
+ artifact: WorkflowArtifact
24
+ ): Promise<WorkflowModule> => {
25
+ const compiled = await compileWorkflowSource(artifact)
26
+ const url = `data:text/javascript;base64,${Buffer.from(compiled).toString("base64")}`
27
+ return await import(url)
28
+ }
29
+
30
+ const compileWorkflowSource = async (artifact: WorkflowArtifact): Promise<string> => {
31
+ const source = rewriteWfImports(artifact.source)
32
+
33
+ if (typeof Bun !== "undefined" && Bun.Transpiler !== undefined) {
34
+ const transpiler = new Bun.Transpiler({
35
+ loader: "ts",
36
+ target: "bun"
37
+ })
38
+ return `${transpiler.transformSync(source)}\n//# sourceURL=wf:${artifact.id}@${artifact.version}\n`
39
+ }
40
+
41
+ const ts = await import("typescript")
42
+ return `${ts.transpileModule(source, {
43
+ compilerOptions: {
44
+ module: ts.ModuleKind.ESNext,
45
+ target: ts.ScriptTarget.ESNext,
46
+ verbatimModuleSyntax: true
47
+ }
48
+ }).outputText}\n//# sourceURL=wf:${artifact.id}@${artifact.version}\n`
49
+ }
50
+
51
+ const rewriteWfImports = (source: string): string => {
52
+ const wfModuleUrl = new URL("../authoring.ts", import.meta.url).href
53
+ return source
54
+ .replaceAll(`from "@mokronos/wfkit"`, `from "${wfModuleUrl}"`)
55
+ .replaceAll(`from '@mokronos/wfkit'`, `from '${wfModuleUrl}'`)
56
+ .replaceAll(`import("@mokronos/wfkit")`, `import("${wfModuleUrl}")`)
57
+ .replaceAll(`import('@mokronos/wfkit')`, `import('${wfModuleUrl}')`)
58
+ }
59
+
60
+ export const loadWorkflowArtifact = async (
61
+ artifact: WorkflowArtifact
62
+ ): Promise<LoadedWorkflow> => {
63
+ const module = await importArtifactModule(artifact)
64
+
65
+ if (artifact.exportName !== undefined) {
66
+ const exported = module[artifact.exportName]
67
+ if (isDefinedWorkflow(exported)) {
68
+ return { artifact, exportName: artifact.exportName, workflow: exported }
69
+ }
70
+ throw new Error(
71
+ `Workflow ${artifact.id} expected export ${artifact.exportName}, but it was not a wf workflow`
72
+ )
73
+ }
74
+
75
+ if (isDefinedWorkflow(module.default)) {
76
+ return { artifact, exportName: "default", workflow: module.default }
77
+ }
78
+
79
+ const candidates: Array<readonly [string, DefinedWorkflow]> = []
80
+ for (const [name, value] of Object.entries(module)) {
81
+ if (isDefinedWorkflow(value)) {
82
+ candidates.push([name, value])
83
+ }
84
+ }
85
+
86
+ if (candidates.length === 1) {
87
+ const [exportName, workflow] = candidates[0]!
88
+ return { artifact, exportName, workflow }
89
+ }
90
+
91
+ if (candidates.length > 1) {
92
+ const names = candidates.map(([name]) => name).join(", ")
93
+ throw new Error(
94
+ `Workflow ${artifact.id} exports multiple workflows (${names}); set exportName in the manifest`
95
+ )
96
+ }
97
+
98
+ throw new Error(`Workflow ${artifact.id} did not export a wf workflow`)
99
+ }