@toolproof-core/lib 1.0.34 → 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 (84) 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 +0 -1
  4. package/dist/integrations/firebase/createThreadedStrategy.js +3 -4
  5. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +4 -10
  6. package/dist/integrations/firebase/firebaseAdminHelpers.js +43 -139
  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 +5 -6
  18. package/dist/utils/creation/threadedStrategyCreation.js +3 -4
  19. package/dist/utils/extractData.d.ts +7 -8
  20. package/dist/utils/extractData.js +25 -21
  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 +1 -2
  26. package/dist/utils/roleSpec.js +0 -1
  27. package/dist/utils/strategyState.d.ts +11 -7
  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 +12 -12
  32. package/src/integrations/firebase/createStep.ts +9 -39
  33. package/src/integrations/firebase/createThreadedStrategy.ts +3 -3
  34. package/src/integrations/firebase/firebaseAdminHelpers.ts +61 -172
  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 +7 -7
  40. package/src/utils/extractData.ts +42 -34
  41. package/src/utils/parallelizeSteps.ts +92 -40
  42. package/src/utils/resolveStrategyStateChain.ts +58 -0
  43. package/src/utils/roleSpec.ts +1 -1
  44. package/src/utils/strategyState.ts +93 -22
  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/createStep.d.ts.map +0 -1
  52. package/dist/integrations/firebase/createStep.js.map +0 -1
  53. package/dist/integrations/firebase/createThreadedStrategy.d.ts.map +0 -1
  54. package/dist/integrations/firebase/createThreadedStrategy.js.map +0 -1
  55. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +0 -1
  56. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +0 -1
  57. package/dist/integrations/firebase/firebaseAdminInit.d.ts.map +0 -1
  58. package/dist/integrations/firebase/firebaseAdminInit.js.map +0 -1
  59. package/dist/types/types.d.ts.map +0 -1
  60. package/dist/types/types.js.map +0 -1
  61. package/dist/utils/bindInputRoleToResource.d.ts +0 -6
  62. package/dist/utils/bindInputRoleToResource.d.ts.map +0 -1
  63. package/dist/utils/bindInputRoleToResource.js +0 -25
  64. package/dist/utils/bindInputRoleToResource.js.map +0 -1
  65. package/dist/utils/creation/resourceCreation.d.ts.map +0 -1
  66. package/dist/utils/creation/resourceCreation.js.map +0 -1
  67. package/dist/utils/creation/stepCreation.d.ts.map +0 -1
  68. package/dist/utils/creation/stepCreation.js.map +0 -1
  69. package/dist/utils/creation/threadedStrategyCreation.d.ts.map +0 -1
  70. package/dist/utils/creation/threadedStrategyCreation.js.map +0 -1
  71. package/dist/utils/extractData.d.ts.map +0 -1
  72. package/dist/utils/extractData.js.map +0 -1
  73. package/dist/utils/parallelizeSteps.d.ts.map +0 -1
  74. package/dist/utils/parallelizeSteps.js.map +0 -1
  75. package/dist/utils/resolveResourceChain.d.ts +0 -19
  76. package/dist/utils/resolveResourceChain.d.ts.map +0 -1
  77. package/dist/utils/resolveResourceChain.js.map +0 -1
  78. package/dist/utils/roleSpec.d.ts.map +0 -1
  79. package/dist/utils/roleSpec.js.map +0 -1
  80. package/dist/utils/strategyState.d.ts.map +0 -1
  81. package/dist/utils/strategyState.js.map +0 -1
  82. package/src/artifacts/artifacts.ts +0 -49
  83. package/src/utils/bindInputRoleToResource.ts +0 -54
  84. package/src/utils/resolveResourceChain.ts +0 -52
@@ -55,4 +55,3 @@ export function extractToolStepsFromStrategy(strategy) {
55
55
  }
56
56
  return toolSteps;
57
57
  }
58
- //# sourceMappingURL=roleSpec.js.map
@@ -1,8 +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 type StrategyStateLike = Record<string, Record<string, unknown>>;
4
- export declare function getStrategyStateBucket(strategyState: StrategyStateLike, toolStepPath: CreationContext['toolStepPath']): StrategyStateInputEntryByRoleName | undefined;
5
- export declare function getStrategyStateEntry(strategyState: StrategyStateLike, context: CreationContext): StrategyStateInputEntry | undefined;
6
- export declare function setStrategyStateEntry<TStrategyState extends StrategyStateLike>(strategyState: TStrategyState, context: CreationContext, entry: StrategyStateInputEntry): TStrategyState;
7
- export declare function clearStrategyStateEntry<TStrategyState extends StrategyStateLike>(strategyState: TStrategyState, context: CreationContext): TStrategyState;
8
- //# 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.34",
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",
@@ -31,13 +31,13 @@
31
31
  "types": "./dist/integrations/firebase/createThreadedStrategy.d.ts",
32
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
  }
@@ -1,4 +1,4 @@
1
- import { CONSTANTS } from '../../artifacts/artifacts.js';
1
+ import { CONSTANTS } from '../../lookups/lookups.js';
2
2
  import {
3
3
  buildThreadedStrategy,
4
4
  getThreadedStrategyStepGroups,
@@ -9,11 +9,11 @@ import { getNewId } from './firebaseAdminHelpers.js';
9
9
 
10
10
  export function createThreadedStrategy<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): ThreadedStrategyLike<TStrategyState> {
11
11
  const threadStepGroups = getThreadedStrategyStepGroups(strategy);
12
- const strategyId = getNewId(CONSTANTS.Names.StrategyId);
12
+ const strategyHandle = getNewId(CONSTANTS.Names.StrategyHandle);
13
13
 
14
14
  return buildThreadedStrategy(
15
15
  threadStepGroups,
16
- strategyId,
16
+ strategyHandle,
17
17
  strategy,
18
18
  );
19
19
  }
@@ -1,203 +1,92 @@
1
1
  import type {
2
- CreationContext,
3
2
  Resource,
4
- ResourceProvenance,
5
- ResourceTypeId,
6
- StepKind,
7
- } from '@toolproof-core/schema';
3
+ ResourcePointer,
4
+ ResourceTypeHandle,
5
+ TimestampedResource,
6
+ } from '@toolproof-core/genesis';
8
7
  import type {
9
- IdName,
10
- IdStringByIdName,
8
+ IdentifierName,
9
+ IdentifierStringByName,
11
10
  ResourcesByType,
12
11
  } from '../../types/types.js';
13
- import { CONSTANTS, MAPPINGS } from '../../artifacts/artifacts.js';
12
+ import { CONSTANTS, MAPPINGS } from '../../lookups/lookups.js';
14
13
  import { dbAdmin, storageAdmin } from './firebaseAdminInit.js';
15
14
 
16
- type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
17
-
18
- function capitalizeFirst(value: string): string {
19
- if (!value) {
20
- return value;
21
- }
22
-
23
- return value.charAt(0).toUpperCase() + value.slice(1);
24
- }
25
-
26
- export function getNewId<K extends IdName>(idName: K): IdStringByIdName<K> {
27
- const prefix = MAPPINGS.IdNameToIdPrefix[idName];
15
+ export function getNewId<K extends IdentifierName>(identifierName: K): IdentifierStringByName<K> {
16
+ const prefix = MAPPINGS.IdentifierNameToPrefix[identifierName];
28
17
  const docRef = dbAdmin
29
18
  .collection(CONSTANTS.Persistence.Collections.resources)
30
- .doc(idName)
19
+ .doc(identifierName)
31
20
  .collection(CONSTANTS.Persistence.Collections.members)
32
21
  .doc();
33
22
 
34
- return (prefix + docRef.id) as IdStringByIdName<K>;
35
- }
36
-
37
- export function getNewStepId<K extends StepKind>(stepKind: K): StepIdStringByStepKind<K> {
38
- const idName = `${capitalizeFirst(stepKind)}StepId` as IdName;
39
-
40
- if (!(idName in MAPPINGS.IdNameToIdPrefix)) {
41
- throw new Error(`No IdNameToIdPrefix entry for derived step id name: ${idName}`);
42
- }
43
-
44
- return getNewId(idName) as StepIdStringByStepKind<K>;
23
+ return (prefix + docRef.id) as IdentifierStringByName<K>;
45
24
  }
46
25
 
47
26
  export async function listResources(
48
- resourceTypeIds: ResourceTypeId[],
27
+ resourceTypeHandles: ResourceTypeHandle[],
49
28
  ): Promise<ResourcesByType> {
50
29
  const bucketResources = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_resources);
51
30
  const bucketStrategies = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_strategies);
52
31
 
53
- async function fetchFilesUnder(
54
- resourceTypeId: ResourceTypeId,
55
- ): Promise<Array<{ data: unknown; meta: any; name: string }>> {
56
- const bucket = resourceTypeId === CONSTANTS.Cosmos.TYPE_ThreadedStrategy
57
- ? bucketStrategies
58
- : bucketResources;
59
- const prefix = `${resourceTypeId}/`;
60
- const [found] = await bucket.getFiles({ prefix });
61
- const files = found.filter((file) => {
62
- const name = file.name || '';
63
- 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;
64
58
  });
59
+ }
65
60
 
66
- if (!files.length) {
67
- 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}`);
68
72
  }
69
73
 
70
- const items = await Promise.all(files.map(async (file) => {
71
- try {
72
- const [buf] = await file.download();
73
- const meta = file.metadata || (await file.getMetadata())[0];
74
- const data = JSON.parse(buf.toString('utf8')) as unknown;
75
- return { data, meta, name: file.name };
76
- } catch {
77
- return null as unknown as { data: unknown; meta: any; name: string };
78
- }
79
- }));
80
-
81
- 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;
82
76
  }
83
77
 
84
78
  const entries = await Promise.all(
85
- resourceTypeIds.map(async (resourceTypeId) => {
86
- const rows = await fetchFilesUnder(resourceTypeId);
87
- const items: Resource[] = rows.map(({ data, meta, name }) => {
88
- const flat = meta?.metadata ?? {};
89
- const root: any = {};
90
-
91
- for (const [key, valueRaw] of Object.entries(flat)) {
92
- if (typeof valueRaw !== 'string') {
93
- continue;
94
- }
95
-
96
- const valueString = valueRaw.trim();
97
- let value: any = valueString;
98
-
99
- if ((valueString.startsWith('{') && valueString.endsWith('}')) || (valueString.startsWith('[') && valueString.endsWith(']'))) {
100
- try {
101
- value = JSON.parse(valueString);
102
- } catch {
103
- value = valueString;
104
- }
105
- }
106
-
107
- const segments = key.split('.');
108
- let cursor = root;
109
-
110
- for (const [index, segment] of segments.entries()) {
111
- const arrIdxMatch = segment.match(/^(.*)\[(\d+)\]$/);
112
-
113
- if (arrIdxMatch) {
114
- const base = arrIdxMatch[1];
115
- const idxStr = arrIdxMatch[2];
116
-
117
- if (base === undefined || idxStr === undefined) {
118
- continue;
119
- }
120
-
121
- const idx = parseInt(idxStr, 10);
122
-
123
- if (!cursor[base] || !Array.isArray(cursor[base])) {
124
- cursor[base] = [];
125
- }
126
-
127
- while (cursor[base].length <= idx) {
128
- cursor[base].push(undefined);
129
- }
130
-
131
- if (index === segments.length - 1) {
132
- cursor[base][idx] = value;
133
- } else {
134
- if (!cursor[base][idx]) {
135
- cursor[base][idx] = {};
136
- }
137
- cursor = cursor[base][idx];
138
- }
139
- } else {
140
- if (index === segments.length - 1) {
141
- cursor[segment] = value;
142
- } else {
143
- if (!cursor[segment] || typeof cursor[segment] !== 'object') {
144
- cursor[segment] = {};
145
- }
146
- cursor = cursor[segment];
147
- }
148
- }
149
- }
150
- }
151
-
152
- const id = root.id;
153
- const provenance = root.provenance as ResourceProvenance | undefined;
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
- ['version', Number.isFinite(version) ? String(version) : ''],
164
- ['timestamp', timestamp],
165
- ['path', resourcePath],
166
- ].filter(([_, value]) => typeof value !== 'string' || (value as string).length === 0) as Array<[string, string | undefined]>;
167
-
168
- if (missing.length) {
169
- const keys = missing.map(([key]) => key).join(', ');
170
- throw new Error(`Missing required metadata keys [${keys}] for resource file: ${name}`);
171
- }
172
-
173
- if (!provenance || typeof provenance !== 'object' || typeof provenance.resourceProvenanceKind !== 'string') {
174
- throw new Error(`Missing required metadata key [provenance] for resource file: ${name}`);
175
- }
176
-
177
- if (provenance.resourceProvenanceKind === 'strategy') {
178
- const creationContext = (provenance as { creationContext?: CreationContext }).creationContext;
179
- if (
180
- !creationContext ||
181
- typeof creationContext !== 'object' ||
182
- typeof creationContext.roleName !== 'string' ||
183
- typeof creationContext.toolStepPath !== 'string'
184
- ) {
185
- throw new Error(`Missing required strategy provenance creationContext for resource file: ${name}`);
186
- }
187
- }
188
-
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);
189
83
  return {
190
- id,
191
- resourceTypeId,
192
- provenance,
193
- version,
194
- timestamp: timestamp as string,
195
- path: resourcePath as string,
196
- nucleus: data as any,
197
- } as unknown as Resource;
198
- });
84
+ ...resourcePointer,
85
+ projection,
86
+ };
87
+ }));
199
88
 
200
- return [resourceTypeId, items] as const;
89
+ return [resourceTypeHandle, items] as const;
201
90
  }),
202
91
  );
203
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
  }