@toolproof-core/lib 1.0.5 → 1.0.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.
Files changed (48) hide show
  1. package/dist/artifacts/artifacts.d.ts +1 -2
  2. package/dist/artifacts/artifacts.d.ts.map +1 -1
  3. package/dist/shared.d.ts +5 -5
  4. package/dist/shared.d.ts.map +1 -1
  5. package/dist/shared.js +5 -5
  6. package/dist/shared.js.map +1 -1
  7. package/dist/utils/bindInputRoleToResource.d.ts +5 -0
  8. package/dist/utils/bindInputRoleToResource.d.ts.map +1 -0
  9. package/dist/utils/bindInputRoleToResource.js +75 -0
  10. package/dist/utils/bindInputRoleToResource.js.map +1 -0
  11. package/dist/utils/createResource.d.ts +9 -0
  12. package/dist/utils/createResource.d.ts.map +1 -0
  13. package/dist/utils/createResource.js +62 -0
  14. package/dist/utils/createResource.js.map +1 -0
  15. package/dist/utils/createRunnableStrategy.d.ts +3 -0
  16. package/dist/utils/createRunnableStrategy.d.ts.map +1 -0
  17. package/dist/utils/createRunnableStrategy.js +23 -0
  18. package/dist/utils/createRunnableStrategy.js.map +1 -0
  19. package/dist/utils/createStep.d.ts +11 -0
  20. package/dist/utils/createStep.d.ts.map +1 -0
  21. package/dist/utils/createStep.js +81 -0
  22. package/dist/utils/createStep.js.map +1 -0
  23. package/dist/utils/extractData.d.ts +11 -0
  24. package/dist/utils/extractData.d.ts.map +1 -0
  25. package/dist/utils/extractData.js +102 -0
  26. package/dist/utils/extractData.js.map +1 -0
  27. package/dist/utils/parallelizeSteps.d.ts +3 -0
  28. package/dist/utils/parallelizeSteps.d.ts.map +1 -0
  29. package/dist/utils/parallelizeSteps.js +137 -0
  30. package/dist/utils/parallelizeSteps.js.map +1 -0
  31. package/dist/utils/resolveResourceChain.d.ts +23 -0
  32. package/dist/utils/resolveResourceChain.d.ts.map +1 -0
  33. package/dist/utils/resolveResourceChain.js +39 -0
  34. package/dist/utils/resolveResourceChain.js.map +1 -0
  35. package/package.json +23 -11
  36. package/src/utils/{roleResourceBinding.ts → bindInputRoleToResource.ts} +8 -6
  37. package/src/utils/createResource.ts +105 -0
  38. package/src/utils/createRunnableStrategy.ts +31 -0
  39. package/src/utils/createStep.ts +111 -0
  40. package/src/utils/{extractFrom.ts → extractData.ts} +54 -2
  41. package/src/utils/{parallelization.ts → parallelizeSteps.ts} +5 -4
  42. package/src/utils/{resourceChain.ts → resolveResourceChain.ts} +11 -10
  43. package/tsconfig.tsbuildinfo +1 -1
  44. package/src/shared.ts +0 -126
  45. package/src/utils/extractFromResourcesByResourceTypeHandle.ts +0 -55
  46. package/src/utils/generateFrom.ts +0 -27
  47. package/src/utils/runnableStrategyCreation.ts +0 -47
  48. package/src/utils/stepCreation.ts +0 -112
@@ -8,6 +8,7 @@ import type {
8
8
  StrategyStateJson,
9
9
  WhileStepJson,
10
10
  } from '@toolproof-core/schema';
11
+ import { CONSTANTS } from '../artifacts/artifacts.js';
11
12
 
12
13
 
13
14
  export function getIndependentThreads(steps: StepJson[], strategyState: StrategyStateJson): StepJson[][] {
@@ -41,26 +42,26 @@ export function getIndependentThreads(steps: StepJson[], strategyState: Strategy
41
42
  };
42
43
 
43
44
  steps.forEach((step, ownerIndex) => {
44
- if (step.stepKind === 'job') {
45
+ if (step.stepKind === CONSTANTS.Enums.StepKind.job) {
45
46
  addJobStep(step as JobStepJson, ownerIndex);
46
47
  return;
47
48
  }
48
49
 
49
- if (step.stepKind === 'for') {
50
+ if (step.stepKind === CONSTANTS.Enums.StepKind.for) {
50
51
  const loop = step as ForStepJson;
51
52
  addJobStep(loop.case?.what, ownerIndex);
52
53
  addJobStep(loop.case?.when, ownerIndex);
53
54
  return;
54
55
  }
55
56
 
56
- if (step.stepKind === 'while') {
57
+ if (step.stepKind === CONSTANTS.Enums.StepKind.while) {
57
58
  const loop = step as WhileStepJson;
58
59
  addJobStep(loop.case?.what, ownerIndex);
59
60
  addJobStep(loop.case?.when, ownerIndex);
60
61
  return;
61
62
  }
62
63
 
63
- if (step.stepKind === 'branch') {
64
+ if (step.stepKind === CONSTANTS.Enums.StepKind.branch) {
64
65
  const branch = step as BranchStepJson;
65
66
  for (const caseItem of branch.cases ?? []) {
66
67
  addJobStep(caseItem?.what, ownerIndex);
@@ -6,11 +6,12 @@ import type {
6
6
  ResourceOutputPotentialJson,
7
7
  StrategyStateJson,
8
8
  } from '@toolproof-core/schema';
9
+ import { CONSTANTS } from '../artifacts/artifacts.js';
9
10
 
10
11
  export type ResolveResult =
11
- | { status: 'materialized'; entry: ResourceJson; path: CreationContextJson[] }
12
- | { status: 'missing'; entry: ResourceMissingJson; path: CreationContextJson[] }
13
- | { status: 'outputPotential'; entry: ResourceOutputPotentialJson; path: CreationContextJson[] }
12
+ | { status: typeof CONSTANTS.Enums.ResourceShellKind.materialized; entry: ResourceJson; path: CreationContextJson[] }
13
+ | { status: typeof CONSTANTS.Enums.ResourceShellKind.missing; entry: ResourceMissingJson; path: CreationContextJson[] }
14
+ | { status: typeof CONSTANTS.Enums.ResourceShellKind.outputPotential; entry: ResourceOutputPotentialJson; path: CreationContextJson[] }
14
15
  | { status: 'unresolved'; reason: 'not-found' | 'cycle' | 'depth-exceeded'; path: CreationContextJson[] };
15
16
 
16
17
  export function resolveResourceChain(
@@ -42,18 +43,18 @@ export function resolveResourceChain(
42
43
  );
43
44
  if (!entry) return { status: 'unresolved', reason: 'not-found', path };
44
45
 
45
- if (entry.resourceShellKind === 'materialized') {
46
- return { status: 'materialized', entry: entry as ResourceJson, path };
46
+ if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.materialized) {
47
+ return { status: CONSTANTS.Enums.ResourceShellKind.materialized, entry: entry as ResourceJson, path };
47
48
  }
48
- if (entry.resourceShellKind === 'missing') {
49
- return { status: 'missing', entry: entry as ResourceMissingJson, path };
49
+ if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.missing) {
50
+ return { status: CONSTANTS.Enums.ResourceShellKind.missing, entry: entry as ResourceMissingJson, path };
50
51
  }
51
- if (entry.resourceShellKind === 'outputPotential') {
52
- return { status: 'outputPotential', entry: entry as ResourceOutputPotentialJson, path };
52
+ if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.outputPotential) {
53
+ return { status: CONSTANTS.Enums.ResourceShellKind.outputPotential, entry: entry as ResourceOutputPotentialJson, path };
53
54
  }
54
55
 
55
56
  // inputPotential: follow ref backwards
56
- if (entry.resourceShellKind === 'inputPotential') {
57
+ if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.inputPotential) {
57
58
  current = (entry as ResourceInputPotentialJson).creationContext;
58
59
  continue;
59
60
  }
@@ -1 +1 @@
1
- {"root":["./src/shared.ts","./src/_lib/setup/firebaseadmininit.ts","./src/artifacts/artifacts.ts","./src/firebase/firebaseadminhelpers.ts","./src/types/types.ts","./src/utils/extractfrom.ts","./src/utils/extractfromresourcesbyresourcetypehandle.ts","./src/utils/generatefrom.ts","./src/utils/parallelization.ts","./src/utils/resourcechain.ts","./src/utils/roleresourcebinding.ts","./src/utils/runnablestrategycreation.ts","./src/utils/stepcreation.ts"],"version":"5.9.3"}
1
+ {"root":["./src/_lib/setup/firebaseadmininit.ts","./src/artifacts/artifacts.ts","./src/firebase/firebaseadminhelpers.ts","./src/types/types.ts","./src/utils/bindinputroletoresource.ts","./src/utils/createresource.ts","./src/utils/createrunnablestrategy.ts","./src/utils/createstep.ts","./src/utils/extractdata.ts","./src/utils/parallelizesteps.ts","./src/utils/resolveresourcechain.ts"],"version":"5.9.3"}
package/src/shared.ts DELETED
@@ -1,126 +0,0 @@
1
- import type {
2
- CreationContextJson,
3
- JsonDataJson,
4
- ResourceIdentityJson,
5
- ResourceInputPotentialJson,
6
- ResourceJson,
7
- ResourceMissingJson,
8
- ResourceOutputPotentialJson,
9
- ResourceTypeIdentityJson,
10
- } from '@toolproof-core/schema';
11
-
12
- import { resolveResourceChain } from './utils/resourceChain.js';
13
- import {
14
- bindInputRefInStrategyState,
15
- bindInputResInStrategyState,
16
- clearInputBindingInStrategyState,
17
- } from './utils/roleResourceBinding.js';
18
- import { createRunnableStrategy } from './utils/runnableStrategyCreation.js';
19
- import {
20
- cloneForStep,
21
- cloneWhileStep,
22
- createBranchStepFromJobPairs,
23
- createJobStepFromJob,
24
- createLoopStepFromJobs,
25
- } from './utils/stepCreation.js';
26
-
27
-
28
- export const RESOURCE_CHAIN = {
29
- resolveResourceChain,
30
- } as const;
31
-
32
-
33
- export const ROLE_RESOURCE_BINDINGS = {
34
- bindInputResInStrategyState,
35
- bindInputRefInStrategyState,
36
- clearInputBindingInStrategyState,
37
- } as const;
38
-
39
-
40
- export const RUNNABLE_STRATEGY_CREATION = {
41
- createRunnableStrategy,
42
- } as const;
43
-
44
-
45
- export const STEP_CREATION = {
46
- createJobStepFromJob,
47
- createLoopStepFromJobs,
48
- createBranchStepFromJobPairs,
49
- cloneForStep,
50
- cloneWhileStep,
51
- } as const;
52
-
53
-
54
- function asMaterializedResource(
55
- base: {
56
- identity: ResourceIdentityJson;
57
- resourceTypeHandle: ResourceTypeIdentityJson;
58
- creationContext: CreationContextJson;
59
- },
60
- nucleus: JsonDataJson,
61
- opts?: { path?: string; timestamp?: string }
62
- ): ResourceJson {
63
- return {
64
- identity: base.identity,
65
- resourceTypeHandle: base.resourceTypeHandle,
66
- creationContext: base.creationContext,
67
- resourceShellKind: 'materialized',
68
- version: 1,
69
- timestamp: opts?.timestamp ?? new Date().toISOString(),
70
- path: opts?.path ?? '',
71
- nucleus,
72
- } as unknown as ResourceJson;
73
- }
74
-
75
-
76
- export const RESOURCE_CREATION = {
77
- createMissingResource(identity: ResourceIdentityJson, resourceTypeHandle: ResourceTypeIdentityJson): ResourceMissingJson {
78
- return {
79
- identity,
80
- resourceTypeHandle,
81
- resourceShellKind: 'missing',
82
- };
83
- },
84
-
85
- createOutputPotentialResource(
86
- identity: ResourceIdentityJson,
87
- resourceTypeHandle: ResourceTypeIdentityJson,
88
- creationContext: CreationContextJson
89
- ): ResourceOutputPotentialJson {
90
- return {
91
- identity,
92
- resourceTypeHandle,
93
- creationContext,
94
- resourceShellKind: 'outputPotential',
95
- };
96
- },
97
-
98
- createInputPotentialResource(
99
- identity: ResourceIdentityJson,
100
- resourceTypeHandle: ResourceTypeIdentityJson,
101
- creationContext: CreationContextJson
102
- ): ResourceInputPotentialJson {
103
- return {
104
- identity,
105
- resourceTypeHandle,
106
- creationContext,
107
- resourceShellKind: 'inputPotential',
108
- };
109
- },
110
-
111
- createMaterializedResourceFromPotential(
112
- potential: ResourceOutputPotentialJson | ResourceInputPotentialJson,
113
- nucleus: JsonDataJson,
114
- opts?: { path?: string; timestamp?: string }
115
- ): ResourceJson {
116
- return asMaterializedResource(
117
- {
118
- identity: potential.identity,
119
- resourceTypeHandle: potential.resourceTypeHandle,
120
- creationContext: potential.creationContext,
121
- },
122
- nucleus,
123
- opts
124
- );
125
- },
126
- } as const;
@@ -1,55 +0,0 @@
1
- import type {
2
- ResourceIdentityJson,
3
- ResourceTypeIdentityJson,
4
- ResourceJson,
5
- } from '@toolproof-core/schema';
6
-
7
-
8
- export function extractResourceMapForResourceType<TResource extends ResourceJson = ResourceJson>(
9
- resourcesByResourceTypeHandle: Partial<Record<ResourceTypeIdentityJson, TResource[]>>,
10
- resourceTypeHandle: ResourceTypeIdentityJson
11
- ): Map<ResourceIdentityJson, TResource> {
12
- const resources = resourcesByResourceTypeHandle[resourceTypeHandle] ?? [];
13
- const map = new Map<ResourceIdentityJson, TResource>();
14
-
15
- for (const resource of resources) {
16
- map.set(resource.identity, resource);
17
- }
18
-
19
- return map;
20
- }
21
-
22
- // ATTENTION: should only need standalone type as type parameter, then we could extract the nuclueus type and identity type
23
- export function extractNucleusMapForResourceType<
24
- TKey extends string = string,
25
- TNucleus extends { identity: string | number | boolean } = { identity: string | number | boolean },
26
- TResource extends ResourceJson = ResourceJson
27
- >(
28
- resourcesByResourceTypeHandle: Partial<Record<ResourceTypeIdentityJson, TResource[]>>,
29
- resourceTypeHandle: ResourceTypeIdentityJson,
30
- ): Map<TKey, TNucleus> {
31
- const resourceMap = extractResourceMapForResourceType<TResource>(resourcesByResourceTypeHandle, resourceTypeHandle);
32
- const out = new Map<TKey, TNucleus>();
33
-
34
- for (const resource of resourceMap.values()) {
35
- const data = (resource as any).nucleus as unknown;
36
- if (data === null || data === undefined) {
37
- continue;
38
- }
39
- if (typeof data !== 'object') {
40
- continue;
41
- }
42
-
43
- const maybeIdentity = (data as any).identity as unknown;
44
- if (maybeIdentity === null || maybeIdentity === undefined) {
45
- continue;
46
- }
47
-
48
- const value = data as TNucleus;
49
- const key = String(maybeIdentity) as TKey;
50
-
51
- out.set(key, value);
52
- }
53
-
54
- return out;
55
- }
@@ -1,27 +0,0 @@
1
- import type { ResourceIdentityJson, ResourceTypeIdentityJson, ResourceJson, ResourceOutputPotentialJson } from '@toolproof-core/schema';
2
-
3
-
4
- export function generatePath(resourceTypeHandle: ResourceTypeIdentityJson, identity: ResourceIdentityJson): string {
5
- return `${resourceTypeHandle}/${identity}.json`;
6
- }
7
-
8
- export function generateMaterializedFromOutputPotential(
9
- outputPotential: ResourceOutputPotentialJson,
10
- nucleus: unknown,
11
- timestamp?: string
12
- ): ResourceJson {
13
- const { identity, resourceTypeHandle, creationContext } = outputPotential;
14
-
15
- const path = generatePath(resourceTypeHandle, identity);
16
-
17
- return {
18
- identity,
19
- resourceTypeHandle,
20
- creationContext,
21
- resourceShellKind: 'materialized',
22
- version: 1,
23
- timestamp: timestamp ?? new Date().toISOString(), // Preserve original timestamp when copying
24
- path,
25
- nucleus,
26
- };
27
- }
@@ -1,47 +0,0 @@
1
- import type {
2
- RawStrategyJson,
3
- RunnableStrategyIdentityJson,
4
- RunnableStrategyJson,
5
- RunnableStrategyStatusJson,
6
- StepJson,
7
- StrategyThreadIdentityJson,
8
- } from '@toolproof-core/schema';
9
- import type { IdentityName } from '../types/types.js';
10
- import { getIndependentThreads } from './parallelization.js';
11
-
12
-
13
- export type GetNewIdentity = (identityName: IdentityName) => string | Promise<string>;
14
-
15
-
16
- export async function createRunnableStrategy(
17
- rawStrategy: RawStrategyJson,
18
- opts: {
19
- getNewIdentity: GetNewIdentity;
20
- status?: RunnableStrategyStatusJson;
21
- }
22
- ): Promise<RunnableStrategyJson> {
23
- if (!opts?.getNewIdentity) {
24
- throw new Error('createRunnableStrategy: opts.getNewIdentity is required');
25
- }
26
-
27
- const runnableStrategyIdentity = (await opts.getNewIdentity('RunnableStrategyIdentity')) as RunnableStrategyIdentityJson;
28
- const steps = (rawStrategy.steps ?? []) as StepJson[];
29
- const strategyState = (rawStrategy.strategyState ?? {}) as RunnableStrategyJson['strategyState'];
30
-
31
- const threadStepGroups = getIndependentThreads(steps, strategyState);
32
- const strategyThreadDict: Record<StrategyThreadIdentityJson, StepJson[]> = {} as any;
33
-
34
- for (const group of threadStepGroups) {
35
- const threadIdentity = (await opts.getNewIdentity('StrategyThreadIdentity')) as StrategyThreadIdentityJson;
36
- strategyThreadDict[threadIdentity] = group;
37
- }
38
-
39
- return {
40
- identity: runnableStrategyIdentity,
41
- runnableStrategyContext: {
42
- status: opts.status ?? 'running',
43
- },
44
- strategyThreadDict,
45
- strategyState,
46
- } as RunnableStrategyJson;
47
- }
@@ -1,112 +0,0 @@
1
- import type {
2
- BranchStepJson,
3
- ConditionalJson,
4
- ForStepJson,
5
- JobJson,
6
- JobStepJson,
7
- ResourceRoleIdentityJson,
8
- WhileStepJson,
9
- } from '@toolproof-core/schema';
10
- import { getNewStepIdentity } from '../firebase/firebaseAdminHelpers.js';
11
-
12
-
13
- function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
14
- if (arr.length === 0) throw new Error(msg);
15
- }
16
-
17
-
18
- export async function createJobStepFromJob(job: JobJson): Promise<JobStepJson> {
19
- const inputBindings = Object.keys(job.roles.inputDict ?? {}) as ResourceRoleIdentityJson[];
20
- const outputBindings = Object.keys(job.roles.outputDict ?? {}) as ResourceRoleIdentityJson[];
21
-
22
- return {
23
- identity: getNewStepIdentity('job'),
24
- stepKind: 'job',
25
- jobHandle: job.identity,
26
- roleBindings: {
27
- inputBindings,
28
- outputBindings,
29
- },
30
- };
31
- }
32
-
33
-
34
- export async function createLoopStepFromJobs(
35
- whatJob: JobJson,
36
- whenJob: JobJson,
37
- stepKind: 'for' | 'while'
38
- ): Promise<ForStepJson | WhileStepJson> {
39
- const what = await createJobStepFromJob(whatJob);
40
- const when = await createJobStepFromJob(whenJob);
41
-
42
- if (stepKind === 'for') {
43
- return {
44
- identity: getNewStepIdentity('for'),
45
- stepKind: 'for',
46
- case: { what, when },
47
- };
48
- }
49
-
50
- return {
51
- identity: getNewStepIdentity('while'),
52
- stepKind: 'while',
53
- case: { what, when },
54
- };
55
- }
56
-
57
-
58
- export async function createBranchStepFromJobPairs(
59
- cases: Array<{ whatJob: JobJson; whenJob: JobJson }>
60
- ): Promise<BranchStepJson> {
61
- const resolved = await Promise.all(
62
- cases.map(async ({ whatJob, whenJob }) => {
63
- const what = await createJobStepFromJob(whatJob);
64
- const when = await createJobStepFromJob(whenJob);
65
- return { what, when } satisfies ConditionalJson;
66
- })
67
- );
68
-
69
- assertNonEmpty(resolved, 'createBranchStepFromJobPairs requires at least one case');
70
-
71
- return {
72
- identity: getNewStepIdentity('branch'),
73
- stepKind: 'branch',
74
- cases: resolved,
75
- };
76
- }
77
-
78
-
79
- export function cloneForStep(forStep: ForStepJson): ForStepJson {
80
- return {
81
- identity: getNewStepIdentity('for'),
82
- stepKind: 'for',
83
- case: {
84
- what: {
85
- ...forStep.case.what,
86
- identity: getNewStepIdentity('job'),
87
- },
88
- when: {
89
- ...forStep.case.when,
90
- identity: getNewStepIdentity('job'),
91
- },
92
- },
93
- };
94
- }
95
-
96
-
97
- export function cloneWhileStep(whileStep: WhileStepJson): WhileStepJson {
98
- return {
99
- identity: getNewStepIdentity('while'),
100
- stepKind: 'while',
101
- case: {
102
- what: {
103
- ...whileStep.case.what,
104
- identity: getNewStepIdentity('job'),
105
- },
106
- when: {
107
- ...whileStep.case.when,
108
- identity: getNewStepIdentity('job'),
109
- },
110
- },
111
- };
112
- }