@toolproof-core/lib 1.0.33 → 1.0.36

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 (90) hide show
  1. package/dist/integrations/firebase/createStep.d.ts +2 -3
  2. package/dist/integrations/firebase/createStep.js +7 -33
  3. package/dist/integrations/firebase/createThreadedStrategy.d.ts +2 -0
  4. package/dist/integrations/firebase/createThreadedStrategy.js +8 -0
  5. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +4 -10
  6. package/dist/integrations/firebase/firebaseAdminHelpers.js +43 -136
  7. package/dist/integrations/firebase/firebaseAdminInit.d.ts +0 -1
  8. package/dist/integrations/firebase/firebaseAdminInit.js +0 -1
  9. package/dist/lookups/lookups.d.ts +221 -0
  10. package/dist/lookups/lookups.js +23 -0
  11. package/dist/types/types.d.ts +8 -12
  12. package/dist/types/types.js +2 -3
  13. package/dist/utils/creation/resourceCreation.d.ts +18 -18
  14. package/dist/utils/creation/resourceCreation.js +49 -48
  15. package/dist/utils/creation/stepCreation.d.ts +7 -48
  16. package/dist/utils/creation/stepCreation.js +16 -42
  17. package/dist/utils/creation/threadedStrategyCreation.d.ts +18 -0
  18. package/dist/utils/creation/threadedStrategyCreation.js +16 -0
  19. package/dist/utils/extractData.d.ts +9 -10
  20. package/dist/utils/extractData.js +29 -25
  21. package/dist/utils/parallelizeSteps.d.ts +1 -2
  22. package/dist/utils/parallelizeSteps.js +52 -37
  23. package/dist/utils/resolveStrategyStateChain.d.ts +20 -0
  24. package/dist/utils/{resolveResourceChain.js → resolveStrategyStateChain.js} +6 -11
  25. package/dist/utils/roleSpec.d.ts +7 -3
  26. package/dist/utils/roleSpec.js +0 -1
  27. package/dist/utils/strategyState.d.ts +11 -6
  28. package/dist/utils/strategyState.js +41 -12
  29. package/dist/utils_2/threadedStrategyCreation.d.ts +5 -0
  30. package/dist/utils_2/threadedStrategyCreation.js +12 -0
  31. package/package.json +15 -15
  32. package/src/integrations/firebase/createStep.ts +9 -39
  33. package/src/integrations/firebase/createThreadedStrategy.ts +19 -0
  34. package/src/integrations/firebase/firebaseAdminHelpers.ts +61 -163
  35. package/src/lookups/lookups.ts +25 -0
  36. package/src/types/types.ts +9 -12
  37. package/src/utils/creation/resourceCreation.ts +140 -121
  38. package/src/utils/creation/stepCreation.ts +25 -95
  39. package/src/utils/creation/threadedStrategyCreation.ts +42 -0
  40. package/src/utils/extractData.ts +46 -38
  41. package/src/utils/parallelizeSteps.ts +92 -40
  42. package/src/utils/resolveStrategyStateChain.ts +58 -0
  43. package/src/utils/roleSpec.ts +7 -3
  44. package/src/utils/strategyState.ts +99 -27
  45. package/src/utils_2/threadedStrategyCreation.ts +19 -0
  46. package/tsconfig.tsbuildinfo +1 -1
  47. package/dist/artifacts/artifacts.d.ts +0 -175
  48. package/dist/artifacts/artifacts.d.ts.map +0 -1
  49. package/dist/artifacts/artifacts.js +0 -48
  50. package/dist/artifacts/artifacts.js.map +0 -1
  51. package/dist/integrations/firebase/createRunnableStrategy.d.ts +0 -3
  52. package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +0 -1
  53. package/dist/integrations/firebase/createRunnableStrategy.js +0 -9
  54. package/dist/integrations/firebase/createRunnableStrategy.js.map +0 -1
  55. package/dist/integrations/firebase/createStep.d.ts.map +0 -1
  56. package/dist/integrations/firebase/createStep.js.map +0 -1
  57. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +0 -1
  58. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +0 -1
  59. package/dist/integrations/firebase/firebaseAdminInit.d.ts.map +0 -1
  60. package/dist/integrations/firebase/firebaseAdminInit.js.map +0 -1
  61. package/dist/types/types.d.ts.map +0 -1
  62. package/dist/types/types.js.map +0 -1
  63. package/dist/utils/bindInputRoleToResource.d.ts +0 -6
  64. package/dist/utils/bindInputRoleToResource.d.ts.map +0 -1
  65. package/dist/utils/bindInputRoleToResource.js +0 -25
  66. package/dist/utils/bindInputRoleToResource.js.map +0 -1
  67. package/dist/utils/creation/resourceCreation.d.ts.map +0 -1
  68. package/dist/utils/creation/resourceCreation.js.map +0 -1
  69. package/dist/utils/creation/runnableStrategyCreation.d.ts +0 -4
  70. package/dist/utils/creation/runnableStrategyCreation.d.ts.map +0 -1
  71. package/dist/utils/creation/runnableStrategyCreation.js +0 -17
  72. package/dist/utils/creation/runnableStrategyCreation.js.map +0 -1
  73. package/dist/utils/creation/stepCreation.d.ts.map +0 -1
  74. package/dist/utils/creation/stepCreation.js.map +0 -1
  75. package/dist/utils/extractData.d.ts.map +0 -1
  76. package/dist/utils/extractData.js.map +0 -1
  77. package/dist/utils/parallelizeSteps.d.ts.map +0 -1
  78. package/dist/utils/parallelizeSteps.js.map +0 -1
  79. package/dist/utils/resolveResourceChain.d.ts +0 -19
  80. package/dist/utils/resolveResourceChain.d.ts.map +0 -1
  81. package/dist/utils/resolveResourceChain.js.map +0 -1
  82. package/dist/utils/roleSpec.d.ts.map +0 -1
  83. package/dist/utils/roleSpec.js.map +0 -1
  84. package/dist/utils/strategyState.d.ts.map +0 -1
  85. package/dist/utils/strategyState.js.map +0 -1
  86. package/src/artifacts/artifacts.ts +0 -49
  87. package/src/integrations/firebase/createRunnableStrategy.ts +0 -18
  88. package/src/utils/bindInputRoleToResource.ts +0 -53
  89. package/src/utils/creation/runnableStrategyCreation.ts +0 -28
  90. package/src/utils/resolveResourceChain.ts +0 -52
@@ -1,4 +1,9 @@
1
- import type { RoleName, RoleValue, Strategy, Tool, ToolStep } from '@toolproof-core/schema';
1
+ import type { RoleName, RoleValue, Step, StepArrayArray, Tool, ToolStep } from '@toolproof-core/genesis';
2
+ export type StrategyStepGraph = {
3
+ steps: Step[];
4
+ } | {
5
+ stepsByThreadIndex: StepArrayArray;
6
+ };
2
7
  export interface ToolRoleDescriptor extends RoleValue {
3
8
  id: RoleName;
4
9
  name: string;
@@ -8,5 +13,4 @@ export declare function getOutputRoleEntries(tool: Tool): Array<[RoleName, RoleV
8
13
  export declare function getInputRoleNames(tool: Tool): RoleName[];
9
14
  export declare function getOutputRoleNames(tool: Tool): RoleName[];
10
15
  export declare function toToolRoleDescriptor([roleName, roleValue]: [RoleName, RoleValue]): ToolRoleDescriptor;
11
- export declare function extractToolStepsFromStrategy(strategy: Strategy): ToolStep[];
12
- //# sourceMappingURL=roleSpec.d.ts.map
16
+ export declare function extractToolStepsFromStrategy(strategy: StrategyStepGraph): ToolStep[];
@@ -55,4 +55,3 @@ export function extractToolStepsFromStrategy(strategy) {
55
55
  }
56
56
  return toolSteps;
57
57
  }
58
- //# sourceMappingURL=roleSpec.js.map
@@ -1,7 +1,12 @@
1
- import type { CreationContext, Strategy, StrategyStateInputEntry, StrategyStateInputEntryByRoleName } from '@toolproof-core/schema';
1
+ import type { ToolStepRoleAddress, ToolStepPath, Resource, Strategy, StrategyStateInputEntry, StrategyStateInputEntryByRoleName, ThreadedStrategyState, ThreadedToolStepPath, ThreadedToolStepRoleAddress, UnthreadedStrategyState, UnthreadedToolStepPath, UnthreadedToolStepRoleAddress } from '@toolproof-core/genesis';
2
2
  export type StrategyState = Strategy['strategyState'];
3
- export declare function getStrategyStateBucket(strategyState: StrategyState, toolStepPath: CreationContext['toolStepPath']): StrategyStateInputEntryByRoleName | undefined;
4
- export declare function getStrategyStateEntry(strategyState: StrategyState, context: CreationContext): StrategyStateInputEntry | undefined;
5
- export declare function setStrategyStateEntry(strategyState: StrategyState, context: CreationContext, entry: StrategyStateInputEntry): StrategyState;
6
- export declare function clearStrategyStateEntry(strategyState: StrategyState, context: CreationContext): StrategyState;
7
- //# sourceMappingURL=strategyState.d.ts.map
3
+ type ToolStepPathForStrategyState<TStrategyState extends StrategyState> = TStrategyState extends UnthreadedStrategyState ? UnthreadedToolStepPath : TStrategyState extends ThreadedStrategyState ? ThreadedToolStepPath : ToolStepPath;
4
+ type ToolStepRoleAddressForStrategyState<TStrategyState extends StrategyState> = TStrategyState extends UnthreadedStrategyState ? UnthreadedToolStepRoleAddress : TStrategyState extends ThreadedStrategyState ? ThreadedToolStepRoleAddress : ToolStepRoleAddress;
5
+ export declare function getStrategyStateBucket<TStrategyState extends StrategyState>(strategyState: TStrategyState, toolStepPath: ToolStepPathForStrategyState<TStrategyState>): StrategyStateInputEntryByRoleName | undefined;
6
+ export declare function getStrategyStateEntry<TStrategyState extends StrategyState>(strategyState: TStrategyState, toolStepRoleAddress: ToolStepRoleAddressForStrategyState<TStrategyState>): StrategyStateInputEntry | undefined;
7
+ export declare function setStrategyStateEntry<TStrategyState extends StrategyState>(strategyState: TStrategyState, toolStepRoleAddress: ToolStepRoleAddressForStrategyState<TStrategyState>, entry: StrategyStateInputEntry): TStrategyState;
8
+ export declare function clearStrategyStateEntry<TStrategyState extends StrategyState>(strategyState: TStrategyState, toolStepRoleAddress: ToolStepRoleAddressForStrategyState<TStrategyState>): TStrategyState;
9
+ export declare function bindInputResource<TStrategyState extends StrategyState>(strategyState: TStrategyState, target: ToolStepRoleAddressForStrategyState<TStrategyState>, resource: Resource): TStrategyState;
10
+ export declare function bindInternalInputPotential<TStrategyState extends StrategyState>(strategyState: TStrategyState, target: ToolStepRoleAddressForStrategyState<TStrategyState>, source: ToolStepRoleAddressForStrategyState<TStrategyState>): TStrategyState;
11
+ export declare function bindExternalInputPotential<TStrategyState extends StrategyState>(strategyState: TStrategyState, target: ToolStepRoleAddressForStrategyState<TStrategyState>): TStrategyState;
12
+ export {};
@@ -1,29 +1,58 @@
1
1
  export function getStrategyStateBucket(strategyState, toolStepPath) {
2
2
  return strategyState[toolStepPath];
3
3
  }
4
- export function getStrategyStateEntry(strategyState, context) {
5
- return getStrategyStateBucket(strategyState, context.toolStepPath)?.[context.roleName];
4
+ export function getStrategyStateEntry(strategyState, toolStepRoleAddress) {
5
+ const toolStepPath = toolStepRoleAddress.toolStepPath;
6
+ return getStrategyStateBucket(strategyState, toolStepPath)?.[toolStepRoleAddress.roleName];
6
7
  }
7
- export function setStrategyStateEntry(strategyState, context, entry) {
8
- const bucket = getStrategyStateBucket(strategyState, context.toolStepPath) ?? {};
8
+ export function setStrategyStateEntry(strategyState, toolStepRoleAddress, entry) {
9
+ const toolStepPath = toolStepRoleAddress.toolStepPath;
10
+ const bucket = getStrategyStateBucket(strategyState, toolStepPath) ?? {};
9
11
  return {
10
12
  ...strategyState,
11
- [context.toolStepPath]: {
13
+ [toolStepPath]: {
12
14
  ...bucket,
13
- [context.roleName]: entry,
15
+ [toolStepRoleAddress.roleName]: entry,
14
16
  },
15
17
  };
16
18
  }
17
- export function clearStrategyStateEntry(strategyState, context) {
18
- const bucket = getStrategyStateBucket(strategyState, context.toolStepPath);
19
- if (!bucket || !(context.roleName in bucket)) {
19
+ export function clearStrategyStateEntry(strategyState, toolStepRoleAddress) {
20
+ const toolStepPath = toolStepRoleAddress.toolStepPath;
21
+ const bucket = getStrategyStateBucket(strategyState, toolStepPath);
22
+ if (!bucket || !(toolStepRoleAddress.roleName in bucket)) {
20
23
  return strategyState;
21
24
  }
22
25
  const nextBucket = { ...bucket };
23
- delete nextBucket[context.roleName];
26
+ delete nextBucket[toolStepRoleAddress.roleName];
24
27
  return {
25
28
  ...strategyState,
26
- [context.toolStepPath]: nextBucket,
29
+ [toolStepPath]: nextBucket,
27
30
  };
28
31
  }
29
- //# sourceMappingURL=strategyState.js.map
32
+ export function bindInputResource(strategyState, target, resource) {
33
+ const inputResource = {
34
+ ...resource,
35
+ strategyStateInputKind: 'inputResource',
36
+ };
37
+ return setStrategyStateEntry(strategyState, target, inputResource);
38
+ }
39
+ export function bindInternalInputPotential(strategyState, target, source) {
40
+ const sourceEntry = getStrategyStateEntry(strategyState, source);
41
+ if (!sourceEntry) {
42
+ throw new Error(`resourceEntry not found for source (${source.toolStepPath}, ${source.roleName})`);
43
+ }
44
+ const internalInputPotential = {
45
+ strategyStateInputKind: 'internalInputPotential',
46
+ toolStepRoleAddress: {
47
+ toolStepPath: source.toolStepPath,
48
+ roleName: source.roleName,
49
+ },
50
+ };
51
+ return setStrategyStateEntry(strategyState, target, internalInputPotential);
52
+ }
53
+ export function bindExternalInputPotential(strategyState, target) {
54
+ const externalInputPotential = {
55
+ strategyStateInputKind: 'externalInputPotential',
56
+ };
57
+ return setStrategyStateEntry(strategyState, target, externalInputPotential);
58
+ }
@@ -0,0 +1,5 @@
1
+ import type { UnthreadedStrategy, ThreadedStrategy, StepArray, StepArrayArray, UnthreadedStrategyState, ThreadedStrategyState } from "@toolproof-core/genesis/types";
2
+ export declare const __UnthreadedStrategyToThreadedStrategy: (unthreadedStrategy: UnthreadedStrategy) => ThreadedStrategy;
3
+ export declare const __UnthreadedStrategyStateToThreadedStrategyState: (unthreadedStrategyState: UnthreadedStrategyState) => ThreadedStrategyState;
4
+ export declare const __StepArrayToStepArrayArray: (stepArray: StepArray) => StepArrayArray;
5
+ export declare const __Foo: (unthreadedStrategy: UnthreadedStrategy, draft: any) => UnthreadedStrategy;
@@ -0,0 +1,12 @@
1
+ export const __UnthreadedStrategyToThreadedStrategy = (unthreadedStrategy) => {
2
+ throw new Error('Not implemented yet');
3
+ };
4
+ export const __UnthreadedStrategyStateToThreadedStrategyState = (unthreadedStrategyState) => {
5
+ throw new Error('Not implemented yet');
6
+ };
7
+ export const __StepArrayToStepArrayArray = (stepArray) => {
8
+ throw new Error('Not implemented yet');
9
+ };
10
+ export const __Foo = (unthreadedStrategy, draft) => {
11
+ throw new Error('Not implemented yet');
12
+ };
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@toolproof-core/lib",
3
- "version": "1.0.33",
3
+ "version": "1.0.36",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
7
7
  "author": "",
8
8
  "license": "ISC",
9
9
  "exports": {
10
- "./artifacts": {
11
- "types": "./dist/artifacts/artifacts.d.ts",
12
- "import": "./dist/artifacts/artifacts.js"
10
+ "./lookups": {
11
+ "types": "./dist/lookups/lookups.d.ts",
12
+ "import": "./dist/lookups/lookups.js"
13
13
  },
14
14
  "./types": {
15
15
  "types": "./dist/types/types.d.ts",
@@ -27,17 +27,17 @@
27
27
  "types": "./dist/integrations/firebase/createStep.d.ts",
28
28
  "import": "./dist/integrations/firebase/createStep.js"
29
29
  },
30
- "./create-runnable-strategy": {
31
- "types": "./dist/integrations/firebase/createRunnableStrategy.d.ts",
32
- "import": "./dist/integrations/firebase/createRunnableStrategy.js"
30
+ "./create-threaded-strategy": {
31
+ "types": "./dist/integrations/firebase/createThreadedStrategy.d.ts",
32
+ "import": "./dist/integrations/firebase/createThreadedStrategy.js"
33
33
  },
34
- "./resolve-resource-chain": {
35
- "types": "./dist/utils/resolveResourceChain.d.ts",
36
- "import": "./dist/utils/resolveResourceChain.js"
34
+ "./resolve-strategy-state-chain": {
35
+ "types": "./dist/utils/resolveStrategyStateChain.d.ts",
36
+ "import": "./dist/utils/resolveStrategyStateChain.js"
37
37
  },
38
- "./bind-input-role-to-resource": {
39
- "types": "./dist/utils/bindInputRoleToResource.d.ts",
40
- "import": "./dist/utils/bindInputRoleToResource.js"
38
+ "./strategy-state": {
39
+ "types": "./dist/utils/strategyState.d.ts",
40
+ "import": "./dist/utils/strategyState.js"
41
41
  },
42
42
  "./firebase-admin-init": {
43
43
  "types": "./dist/integrations/firebase/firebaseAdminInit.d.ts",
@@ -52,8 +52,8 @@
52
52
  "@types/node": "^22.0.0"
53
53
  },
54
54
  "dependencies": {
55
- "@toolproof-core/schema": "^1.0.46",
56
- "firebase-admin": "^13.7.0"
55
+ "firebase-admin": "^13.7.0",
56
+ "@toolproof-core/genesis": "1.0.56"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -4,19 +4,18 @@ import type {
4
4
  Tool,
5
5
  ToolStep,
6
6
  WhileStep,
7
- } from '@toolproof-core/schema';
8
- import { CONSTANTS } from '../../artifacts/artifacts.js';
7
+ } from '@toolproof-core/genesis';
8
+ import { CONSTANTS } from '../../lookups/lookups.js';
9
9
  import {
10
10
  buildBranchStepFromToolPairs,
11
11
  buildToolStepFromTool,
12
12
  buildLoopStepFromToolPair,
13
- cloneForStepWithIdentities,
14
- cloneWhileStepWithIdentities,
13
+ cloneForStep as cloneForStepPure,
14
+ cloneWhileStep as cloneWhileStepPure,
15
15
  } from '../../utils/creation/stepCreation.js';
16
- import { getNewStepId } from './firebaseAdminHelpers.js';
17
16
 
18
17
  export function createToolStepFromTool(tool: Tool): ToolStep {
19
- return buildToolStepFromTool(tool, getNewStepId(CONSTANTS.Enums.StepKind.tool));
18
+ return buildToolStepFromTool(tool);
20
19
  }
21
20
 
22
21
  export function createLoopStepFromToolPair(
@@ -24,48 +23,19 @@ export function createLoopStepFromToolPair(
24
23
  whenTool: Tool,
25
24
  stepKind: typeof CONSTANTS.Enums.StepKind.for | typeof CONSTANTS.Enums.StepKind.while,
26
25
  ): ForStep | WhileStep {
27
- if (stepKind === CONSTANTS.Enums.StepKind.for) {
28
- return buildLoopStepFromToolPair(whatTool, whenTool, {
29
- stepKind,
30
- stepId: getNewStepId(CONSTANTS.Enums.StepKind.for),
31
- whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
32
- whenId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
33
- });
34
- }
35
-
36
- return buildLoopStepFromToolPair(whatTool, whenTool, {
37
- stepKind,
38
- stepId: getNewStepId(CONSTANTS.Enums.StepKind.while),
39
- whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
40
- whenId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
41
- });
26
+ return buildLoopStepFromToolPair(whatTool, whenTool, stepKind);
42
27
  }
43
28
 
44
29
  export function createBranchStepFromToolPairs(
45
30
  cases: Array<{ whatTool: Tool; whenTool: Tool }>,
46
31
  ): BranchStep {
47
- return buildBranchStepFromToolPairs(
48
- cases,
49
- getNewStepId(CONSTANTS.Enums.StepKind.branch),
50
- cases.map(() => ({
51
- whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
52
- whenId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
53
- })),
54
- );
32
+ return buildBranchStepFromToolPairs(cases);
55
33
  }
56
34
 
57
35
  export function cloneForStep(forStep: ForStep): ForStep {
58
- return cloneForStepWithIdentities(forStep, {
59
- stepId: getNewStepId(CONSTANTS.Enums.StepKind.for),
60
- whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
61
- whenId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
62
- });
36
+ return cloneForStepPure(forStep);
63
37
  }
64
38
 
65
39
  export function cloneWhileStep(whileStep: WhileStep): WhileStep {
66
- return cloneWhileStepWithIdentities(whileStep, {
67
- stepId: getNewStepId(CONSTANTS.Enums.StepKind.while),
68
- whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
69
- whenId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
70
- });
40
+ return cloneWhileStepPure(whileStep);
71
41
  }
@@ -0,0 +1,19 @@
1
+ import { CONSTANTS } from '../../lookups/lookups.js';
2
+ import {
3
+ buildThreadedStrategy,
4
+ getThreadedStrategyStepGroups,
5
+ type ThreadableStrategy,
6
+ type ThreadedStrategyLike,
7
+ } from '../../utils/creation/threadedStrategyCreation.js';
8
+ import { getNewId } from './firebaseAdminHelpers.js';
9
+
10
+ export function createThreadedStrategy<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): ThreadedStrategyLike<TStrategyState> {
11
+ const threadStepGroups = getThreadedStrategyStepGroups(strategy);
12
+ const strategyHandle = getNewId(CONSTANTS.Names.StrategyHandle);
13
+
14
+ return buildThreadedStrategy(
15
+ threadStepGroups,
16
+ strategyHandle,
17
+ strategy,
18
+ );
19
+ }
@@ -1,194 +1,92 @@
1
1
  import type {
2
2
  Resource,
3
- ResourceTypeId,
4
- StepKind,
5
- } from '@toolproof-core/schema';
3
+ ResourcePointer,
4
+ ResourceTypeHandle,
5
+ TimestampedResource,
6
+ } from '@toolproof-core/genesis';
6
7
  import type {
7
- IdName,
8
- IdStringByIdName,
8
+ IdentifierName,
9
+ IdentifierStringByName,
9
10
  ResourcesByType,
10
11
  } from '../../types/types.js';
11
- import { CONSTANTS, MAPPINGS } from '../../artifacts/artifacts.js';
12
+ import { CONSTANTS, MAPPINGS } from '../../lookups/lookups.js';
12
13
  import { dbAdmin, storageAdmin } from './firebaseAdminInit.js';
13
14
 
14
- type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
15
-
16
- function capitalizeFirst(value: string): string {
17
- if (!value) {
18
- return value;
19
- }
20
-
21
- return value.charAt(0).toUpperCase() + value.slice(1);
22
- }
23
-
24
- export function getNewId<K extends IdName>(idName: K): IdStringByIdName<K> {
25
- const prefix = MAPPINGS.IdNameToIdPrefix[idName];
15
+ export function getNewId<K extends IdentifierName>(identifierName: K): IdentifierStringByName<K> {
16
+ const prefix = MAPPINGS.IdentifierNameToPrefix[identifierName];
26
17
  const docRef = dbAdmin
27
18
  .collection(CONSTANTS.Persistence.Collections.resources)
28
- .doc(idName)
19
+ .doc(identifierName)
29
20
  .collection(CONSTANTS.Persistence.Collections.members)
30
21
  .doc();
31
22
 
32
- return (prefix + docRef.id) as IdStringByIdName<K>;
33
- }
34
-
35
- export function getNewStepId<K extends StepKind>(stepKind: K): StepIdStringByStepKind<K> {
36
- const idName = `${capitalizeFirst(stepKind)}StepId` as IdName;
37
-
38
- if (!(idName in MAPPINGS.IdNameToIdPrefix)) {
39
- throw new Error(`No IdNameToIdPrefix entry for derived step id name: ${idName}`);
40
- }
41
-
42
- return getNewId(idName) as StepIdStringByStepKind<K>;
23
+ return (prefix + docRef.id) as IdentifierStringByName<K>;
43
24
  }
44
25
 
45
26
  export async function listResources(
46
- resourceTypeIds: ResourceTypeId[],
27
+ resourceTypeHandles: ResourceTypeHandle[],
47
28
  ): Promise<ResourcesByType> {
48
29
  const bucketResources = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_resources);
49
30
  const bucketStrategies = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_strategies);
50
31
 
51
- async function fetchFilesUnder(
52
- resourceTypeId: ResourceTypeId,
53
- ): Promise<Array<{ data: unknown; meta: any; name: string }>> {
54
- const bucket = resourceTypeId === CONSTANTS.Cosmos.TYPE_RunnableStrategy
55
- ? bucketStrategies
56
- : bucketResources;
57
- const prefix = `${resourceTypeId}/`;
58
- const [found] = await bucket.getFiles({ prefix });
59
- const files = found.filter((file) => {
60
- const name = file.name || '';
61
- return !!name && !name.endsWith('/');
32
+ async function fetchTimestampedMetadataUnder(
33
+ resourceTypeHandle: ResourceTypeHandle,
34
+ ): Promise<ResourcePointer[]> {
35
+ const snapshot = await dbAdmin
36
+ .collection(CONSTANTS.Persistence.Collections.resources)
37
+ .doc(resourceTypeHandle)
38
+ .collection(CONSTANTS.Persistence.Collections.members)
39
+ .get();
40
+
41
+ return snapshot.docs.map((doc) => {
42
+ const data = doc.data() as Partial<TimestampedResource>;
43
+ if (
44
+ typeof data.id !== 'string' ||
45
+ typeof data.resourceTypeHandle !== 'string' ||
46
+ !data.provenance ||
47
+ typeof data.provenance !== 'object' ||
48
+ typeof data.provenance.provenanceKind !== 'string'
49
+ ) {
50
+ throw new Error(`Invalid TimestampedResource document for ${resourceTypeHandle}/${doc.id}`);
51
+ }
52
+
53
+ return {
54
+ id: data.id,
55
+ resourceTypeHandle: data.resourceTypeHandle,
56
+ provenance: data.provenance,
57
+ } as ResourcePointer;
62
58
  });
59
+ }
63
60
 
64
- if (!files.length) {
65
- return [];
61
+ async function fetchProjection(
62
+ resourceTypeHandle: ResourceTypeHandle,
63
+ resourceId: ResourcePointer['id'],
64
+ ): Promise<unknown> {
65
+ const bucket = resourceTypeHandle === CONSTANTS.Handles.ResourceTypes.TYPE_Strategy
66
+ ? bucketStrategies
67
+ : bucketResources;
68
+ const file = bucket.file(`${resourceTypeHandle}/${resourceId}`);
69
+ const [exists] = await file.exists();
70
+ if (!exists) {
71
+ throw new Error(`Missing projection blob for ${resourceTypeHandle}/${resourceId}`);
66
72
  }
67
73
 
68
- const items = await Promise.all(files.map(async (file) => {
69
- try {
70
- const [buf] = await file.download();
71
- const meta = file.metadata || (await file.getMetadata())[0];
72
- const data = JSON.parse(buf.toString('utf8')) as unknown;
73
- return { data, meta, name: file.name };
74
- } catch {
75
- return null as unknown as { data: unknown; meta: any; name: string };
76
- }
77
- }));
78
-
79
- return items.filter(Boolean) as Array<{ data: unknown; meta: any; name: string }>;
74
+ const [buf] = await file.download();
75
+ return JSON.parse(buf.toString('utf8')) as unknown;
80
76
  }
81
77
 
82
78
  const entries = await Promise.all(
83
- resourceTypeIds.map(async (resourceTypeId) => {
84
- const rows = await fetchFilesUnder(resourceTypeId);
85
- const items: Resource[] = rows.map(({ data, meta, name }) => {
86
- const flat = meta?.metadata ?? {};
87
- const root: any = {};
88
-
89
- for (const [key, valueRaw] of Object.entries(flat)) {
90
- if (typeof valueRaw !== 'string') {
91
- continue;
92
- }
93
-
94
- const valueString = valueRaw.trim();
95
- let value: any = valueString;
96
-
97
- if ((valueString.startsWith('{') && valueString.endsWith('}')) || (valueString.startsWith('[') && valueString.endsWith(']'))) {
98
- try {
99
- value = JSON.parse(valueString);
100
- } catch {
101
- value = valueString;
102
- }
103
- }
104
-
105
- const segments = key.split('.');
106
- let cursor = root;
107
-
108
- for (const [index, segment] of segments.entries()) {
109
- const arrIdxMatch = segment.match(/^(.*)\[(\d+)\]$/);
110
-
111
- if (arrIdxMatch) {
112
- const base = arrIdxMatch[1];
113
- const idxStr = arrIdxMatch[2];
114
-
115
- if (base === undefined || idxStr === undefined) {
116
- continue;
117
- }
118
-
119
- const idx = parseInt(idxStr, 10);
120
-
121
- if (!cursor[base] || !Array.isArray(cursor[base])) {
122
- cursor[base] = [];
123
- }
124
-
125
- while (cursor[base].length <= idx) {
126
- cursor[base].push(undefined);
127
- }
128
-
129
- if (index === segments.length - 1) {
130
- cursor[base][idx] = value;
131
- } else {
132
- if (!cursor[base][idx]) {
133
- cursor[base][idx] = {};
134
- }
135
- cursor = cursor[base][idx];
136
- }
137
- } else {
138
- if (index === segments.length - 1) {
139
- cursor[segment] = value;
140
- } else {
141
- if (!cursor[segment] || typeof cursor[segment] !== 'object') {
142
- cursor[segment] = {};
143
- }
144
- cursor = cursor[segment];
145
- }
146
- }
147
- }
148
- }
149
-
150
- const id = root.id;
151
- const resourceRoleId = root.creationContext.resourceRoleId;
152
- const toolStepId = root.creationContext.toolStepId;
153
- const resourceShellKind = root.resourceShellKind;
154
- const versionRaw = root.version;
155
- const version = typeof versionRaw === 'number'
156
- ? versionRaw
157
- : parseInt(String(versionRaw ?? ''), 10);
158
- const resourcePath = root.path;
159
- const timestamp = root.timestamp;
160
-
161
- const missing = [
162
- ['id', id],
163
- ['resourceRoleId', resourceRoleId],
164
- ['toolStepId', toolStepId],
165
- ['resourceShellKind', resourceShellKind],
166
- ['version', Number.isFinite(version) ? String(version) : ''],
167
- ['timestamp', timestamp],
168
- ['path', resourcePath],
169
- ].filter(([_, value]) => typeof value !== 'string' || (value as string).length === 0) as Array<[string, string | undefined]>;
170
-
171
- if (missing.length) {
172
- const keys = missing.map(([key]) => key).join(', ');
173
- throw new Error(`Missing required metadata keys [${keys}] for resource file: ${name}`);
174
- }
175
-
79
+ resourceTypeHandles.map(async (resourceTypeHandle) => {
80
+ const pointers = await fetchTimestampedMetadataUnder(resourceTypeHandle);
81
+ const items: Resource[] = await Promise.all(pointers.map(async (resourcePointer) => {
82
+ const projection = await fetchProjection(resourceTypeHandle, resourcePointer.id);
176
83
  return {
177
- id,
178
- resourceTypeId,
179
- creationContext: {
180
- resourceRoleId,
181
- toolStepId,
182
- },
183
- resourceShellKind,
184
- version,
185
- timestamp: timestamp as string,
186
- path: resourcePath as string,
187
- nucleus: data as any,
188
- } as unknown as Resource;
189
- });
84
+ ...resourcePointer,
85
+ projection,
86
+ };
87
+ }));
190
88
 
191
- return [resourceTypeId, items] as const;
89
+ return [resourceTypeHandle, items] as const;
192
90
  }),
193
91
  );
194
92
 
@@ -0,0 +1,25 @@
1
+ import { CONSTANTS as _CONSTANTS, MAPPINGS as _MAPPINGS } from '@toolproof-core/genesis';
2
+
3
+ export const CONSTANTS = {
4
+ ..._CONSTANTS,
5
+ Persistence: {
6
+ Buckets: {
7
+ tp_resources: 'tp-resources',
8
+ tp_strategies: 'tp-strategies',
9
+ },
10
+ Collections: {
11
+ resources: 'resources',
12
+ members: 'members',
13
+ },
14
+ },
15
+ Engine: {
16
+ Graphs: {
17
+ GraphRunStrategy: 'GraphRunStrategy',
18
+ GraphBuildStrategy: 'GraphBuildStrategy',
19
+ },
20
+ },
21
+ } as const;
22
+
23
+ export const MAPPINGS = {
24
+ ..._MAPPINGS,
25
+ } as const;
@@ -1,10 +1,9 @@
1
1
  import type {
2
- StepKind,
3
2
  Resource,
4
- ResourceTypeId,
5
- } from '@toolproof-core/schema';
6
- import { CONSTANTS } from '../artifacts/artifacts.js';
7
- import { MAPPINGS } from '../artifacts/artifacts.js';
3
+ ResourceTypeHandle,
4
+ } from '@toolproof-core/genesis/types';
5
+ import { CONSTANTS } from '../lookups/lookups.js';
6
+ import { MAPPINGS } from '../lookups/lookups.js';
8
7
 
9
8
  export type Bucket = typeof CONSTANTS.Persistence.Buckets.tp_resources;
10
9
 
@@ -12,19 +11,17 @@ export type Collection = keyof typeof CONSTANTS.Persistence.Collections;
12
11
 
13
12
  export type SchemaLike = Record<string, unknown>;
14
13
 
15
- export type ResourcesByType = Record<ResourceTypeId, Resource[]>;
14
+ export type ResourcesByType = Record<ResourceTypeHandle, Resource[]>;
16
15
 
17
- export type IdName = keyof typeof MAPPINGS.IdNameToIdPrefix;
16
+ export type IdentifierName = keyof typeof MAPPINGS.IdentifierNameToPrefix;
18
17
 
19
- export type IdStringByIdName<K extends IdName> = `${(typeof MAPPINGS.IdNameToIdPrefix)[K]}${string}`;
18
+ export type IdentifierStringByName<K extends IdentifierName> = `${(typeof MAPPINGS.IdentifierNameToPrefix)[K]}${string}`;
20
19
 
21
- export type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
22
-
23
- export interface NucleusBaseSmall<T extends string = string> {
20
+ export interface SmallBaseProjection<T extends string = string> {
24
21
  id: T;
25
22
  }
26
23
 
27
- export interface NucleusBaseLarge<T extends string = string> extends NucleusBaseSmall<T> {
24
+ export interface LargeBaseProjection<T extends string = string> extends SmallBaseProjection<T> {
28
25
  name: string;
29
26
  description: string;
30
27
  }