@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,121 +1,140 @@
1
- import type {
2
- CreationContext,
3
- ExternalInput,
4
- ReferenceInput,
5
- ResourceId,
6
- Resource,
7
- ResourceTypeId,
8
- StrategyRunId,
9
- } from '@toolproof-core/schema';
10
-
11
- export interface OutputPotential {
12
- id: ResourceId;
13
- resourceTypeId: ResourceTypeId;
14
- creationContext: CreationContext;
15
- strategyRunId?: StrategyRunId;
16
- }
17
-
18
- export type InputPotential = ReferenceInput & {
19
- id: ResourceId;
20
- resourceTypeId: ResourceTypeId;
21
- creationContext: CreationContext;
22
- strategyRunId?: StrategyRunId;
23
- };
24
-
25
- export function generatePath(resourceTypeId: ResourceTypeId, id: ResourceId): string {
26
- return `${resourceTypeId}/${id}.json`;
27
- }
28
-
29
- export function createMaterializedFromOutputPotential(
30
- outputPotential: OutputPotential,
31
- nucleus: unknown,
32
- timestamp?: string,
33
- ): Resource {
34
- const { id, resourceTypeId, creationContext } = outputPotential;
35
- const path = generatePath(resourceTypeId, id);
36
-
37
- return {
38
- id,
39
- resourceTypeId,
40
- provenance: {
41
- resourceProvenanceKind: 'strategy',
42
- strategyRunId: outputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
43
- creationContext,
44
- },
45
- version: 1,
46
- timestamp: timestamp ?? new Date().toISOString(),
47
- path,
48
- nucleus,
49
- };
50
- }
51
-
52
- export function createMaterializedFromInputPotential(
53
- inputPotential: InputPotential,
54
- nucleus: unknown,
55
- timestamp?: string,
56
- ): Resource {
57
- const { id, resourceTypeId, creationContext } = inputPotential;
58
- const path = generatePath(resourceTypeId, id);
59
-
60
- return {
61
- id,
62
- resourceTypeId,
63
- provenance: {
64
- resourceProvenanceKind: 'strategy',
65
- strategyRunId: inputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
66
- creationContext,
67
- },
68
- version: 1,
69
- timestamp: timestamp ?? new Date().toISOString(),
70
- path,
71
- nucleus,
72
- };
73
- }
74
-
75
- export function createMaterializedFromPotential(
76
- potential: InputPotential | OutputPotential,
77
- nucleus: unknown,
78
- timestamp?: string,
79
- ): Resource {
80
- if ('strategyStateInputKind' in potential && potential.strategyStateInputKind === 'referenceInput') {
81
- return createMaterializedFromInputPotential(potential, nucleus, timestamp);
82
- }
83
-
84
- return createMaterializedFromOutputPotential(potential, nucleus, timestamp);
85
- }
86
-
87
- export function createExternalInputPotential(
88
- _id: ResourceId,
89
- _resourceTypeId: ResourceTypeId,
90
- ): ExternalInput {
91
- return {
92
- strategyStateInputKind: 'externalInput',
93
- };
94
- }
95
-
96
- export function createInputPotential(
97
- id: ResourceId,
98
- resourceTypeId: ResourceTypeId,
99
- creationContext: CreationContext,
100
- ): InputPotential {
101
- return {
102
- id,
103
- resourceTypeId,
104
- strategyStateInputKind: 'referenceInput',
105
- toolStepPath: creationContext.toolStepPath,
106
- roleName: creationContext.roleName,
107
- creationContext,
108
- };
109
- }
110
-
111
- export function createOutputPotential(
112
- id: ResourceId,
113
- resourceTypeId: ResourceTypeId,
114
- creationContext: CreationContext,
115
- ): OutputPotential {
116
- return {
117
- id,
118
- resourceTypeId,
119
- creationContext,
120
- };
121
- }
1
+ import type {
2
+ ToolStepRoleAddress,
3
+ ExternalInputPotential,
4
+ InternalInputPotential,
5
+ ResourceId,
6
+ Resource,
7
+ ResourcePointer,
8
+ ResourceTypeHandle,
9
+ StrategyProvenance,
10
+ Provenance,
11
+ StrategyTraceHandle,
12
+ } from '@toolproof-core/genesis';
13
+
14
+ type RuntimeProvenance = Extract<Provenance, { provenanceKind: 'runtime' }>;
15
+
16
+ export interface OutputPotential {
17
+ id: ResourceId;
18
+ resourceTypeHandle: ResourceTypeHandle;
19
+ toolStepRoleAddress?: ToolStepRoleAddress;
20
+ strategyTraceHandle?: StrategyTraceHandle;
21
+ provenanceKind?: Provenance['provenanceKind'];
22
+ }
23
+
24
+ export function createExternalInputPotential(): ExternalInputPotential {
25
+ return {
26
+ strategyStateInputKind: 'externalInputPotential',
27
+ };
28
+ }
29
+
30
+ export function createInternalInputPotential(
31
+ toolStepRoleAddress: ToolStepRoleAddress,
32
+ ): InternalInputPotential {
33
+ return {
34
+ strategyStateInputKind: 'internalInputPotential',
35
+ toolStepRoleAddress,
36
+ };
37
+ }
38
+
39
+ export function createStrategyProvenance(
40
+ strategyTraceHandle: StrategyTraceHandle,
41
+ toolStepRoleAddress: ToolStepRoleAddress,
42
+ ): StrategyProvenance {
43
+ return {
44
+ provenanceKind: 'strategy',
45
+ strategyTraceHandle,
46
+ toolStepRoleAddress,
47
+ };
48
+ }
49
+
50
+ export function createRuntimeProvenance(): RuntimeProvenance {
51
+ return {
52
+ provenanceKind: 'runtime',
53
+ };
54
+ }
55
+
56
+ export function createResourcePointer(
57
+ id: ResourceId,
58
+ resourceTypeHandle: ResourceTypeHandle,
59
+ provenance: Provenance,
60
+ ): ResourcePointer {
61
+ return {
62
+ id,
63
+ resourceTypeHandle,
64
+ provenance,
65
+ };
66
+ }
67
+
68
+ export function createResource(
69
+ resourcePointer: ResourcePointer,
70
+ projection: unknown,
71
+ ): Resource {
72
+ return {
73
+ ...resourcePointer,
74
+ projection,
75
+ };
76
+ }
77
+
78
+ export function createMaterializedFromOutputPotential(
79
+ outputPotential: OutputPotential,
80
+ projection: unknown,
81
+ ): Resource {
82
+ const provenance = outputPotential.provenanceKind === 'runtime'
83
+ ? createRuntimeProvenance()
84
+ : createStrategyProvenance(
85
+ requireStrategyTraceHandle(outputPotential.strategyTraceHandle),
86
+ requireToolStepRoleAddress(outputPotential.toolStepRoleAddress),
87
+ );
88
+
89
+ const resourcePointer = createResourcePointer(
90
+ outputPotential.id,
91
+ outputPotential.resourceTypeHandle,
92
+ provenance,
93
+ );
94
+
95
+ return createResource(resourcePointer, projection);
96
+ }
97
+
98
+ export function createMaterializedFromPotential(
99
+ outputPotential: OutputPotential,
100
+ projection: unknown,
101
+ ): Resource {
102
+ return createMaterializedFromOutputPotential(outputPotential, projection);
103
+ }
104
+
105
+ export function createOutputPotential(
106
+ id: ResourceId,
107
+ resourceTypeHandle: ResourceTypeHandle,
108
+ toolStepRoleAddress?: ToolStepRoleAddress,
109
+ strategyTraceHandle?: StrategyTraceHandle,
110
+ provenanceKind?: Provenance['provenanceKind'],
111
+ ): OutputPotential {
112
+ return {
113
+ id,
114
+ resourceTypeHandle,
115
+ ...(toolStepRoleAddress ? { toolStepRoleAddress } : {}),
116
+ ...(strategyTraceHandle ? { strategyTraceHandle } : {}),
117
+ ...(provenanceKind ? { provenanceKind } : {}),
118
+ };
119
+ }
120
+
121
+ function requireStrategyTraceHandle(
122
+ strategyTraceHandle: StrategyTraceHandle | undefined,
123
+ ): StrategyTraceHandle {
124
+ if (!strategyTraceHandle) {
125
+ throw new Error('strategyTraceHandle is required to materialize a strategy output potential');
126
+ }
127
+
128
+ return strategyTraceHandle;
129
+ }
130
+
131
+ function requireToolStepRoleAddress(
132
+ toolStepRoleAddress: ToolStepRoleAddress | undefined,
133
+ ): ToolStepRoleAddress {
134
+ if (!toolStepRoleAddress) {
135
+ throw new Error('toolStepRoleAddress is required to materialize a strategy output potential');
136
+ }
137
+
138
+ return toolStepRoleAddress;
139
+ }
140
+
@@ -5,39 +5,10 @@ import type {
5
5
  Tool,
6
6
  ToolStep,
7
7
  WhileStep,
8
- } from '@toolproof-core/schema';
9
- import { CONSTANTS } from '../../artifacts/artifacts.js';
8
+ } from '@toolproof-core/genesis';
9
+ import { CONSTANTS } from '../../lookups/lookups.js';
10
10
  import { getInputRoleNames, getOutputRoleNames } from '../roleSpec.js';
11
11
 
12
- type ToolStepId = string;
13
- type BranchStepId = string;
14
- type ForStepId = string;
15
- type WhileStepId = string;
16
-
17
- type ToolStepWithId = ToolStep & { id: ToolStepId };
18
- type BranchStepWithId = BranchStep & { id: BranchStepId };
19
- type ForStepWithId = ForStep & { id: ForStepId };
20
- type WhileStepWithId = WhileStep & { id: WhileStepId };
21
-
22
- export type LoopStepBuildIdentities =
23
- | {
24
- stepKind: typeof CONSTANTS.Enums.StepKind.for;
25
- stepId: ForStepId;
26
- whatId: ToolStepId;
27
- whenId: ToolStepId;
28
- }
29
- | {
30
- stepKind: typeof CONSTANTS.Enums.StepKind.while;
31
- stepId: WhileStepId;
32
- whatId: ToolStepId;
33
- whenId: ToolStepId;
34
- };
35
-
36
- export type BranchCaseBuildIdentities = {
37
- whatId: ToolStepId;
38
- whenId: ToolStepId;
39
- };
40
-
41
12
  function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
42
13
  if (arr.length === 0) {
43
14
  throw new Error(msg);
@@ -53,113 +24,72 @@ function getRoleBindingSpec(tool: Tool): ToolStep['roleBindingSpec'] {
53
24
 
54
25
  export function buildToolStepFromTool(
55
26
  tool: Tool,
56
- id: ToolStepId,
57
- ): ToolStepWithId {
27
+ ): ToolStep {
58
28
  return {
59
- id,
60
29
  stepKind: CONSTANTS.Enums.StepKind.tool,
61
- toolId: tool.id,
30
+ toolHandle: tool.handle,
62
31
  roleBindingSpec: getRoleBindingSpec(tool),
63
- } as ToolStepWithId;
32
+ };
64
33
  }
65
34
 
66
35
  export function buildLoopStepFromToolPair(
67
36
  whatTool: Tool,
68
37
  whenTool: Tool,
69
- identities: LoopStepBuildIdentities,
38
+ stepKind: typeof CONSTANTS.Enums.StepKind.for | typeof CONSTANTS.Enums.StepKind.while,
70
39
  ): ForStep | WhileStep {
71
- const what = buildToolStepFromTool(whatTool, identities.whatId);
72
- const when = buildToolStepFromTool(whenTool, identities.whenId);
40
+ const what = buildToolStepFromTool(whatTool);
41
+ const when = buildToolStepFromTool(whenTool);
73
42
 
74
- if (identities.stepKind === CONSTANTS.Enums.StepKind.for) {
43
+ if (stepKind === CONSTANTS.Enums.StepKind.for) {
75
44
  return {
76
- id: identities.stepId,
77
45
  stepKind: CONSTANTS.Enums.StepKind.for,
78
46
  case: { what, when },
79
- } as ForStepWithId;
47
+ };
80
48
  }
81
49
 
82
50
  return {
83
- id: identities.stepId,
84
51
  stepKind: CONSTANTS.Enums.StepKind.while,
85
52
  case: { what, when },
86
- } as WhileStepWithId;
53
+ };
87
54
  }
88
55
 
89
56
  export function buildBranchStepFromToolPairs(
90
57
  cases: Array<{ whatTool: Tool; whenTool: Tool }>,
91
- branchId: BranchStepId,
92
- caseIdentities: BranchCaseBuildIdentities[],
93
- ): BranchStepWithId {
94
- if (cases.length !== caseIdentities.length) {
95
- throw new Error('buildBranchStepFromToolPairs requires one id pair per case');
96
- }
97
-
58
+ ): BranchStep {
98
59
  const resolved = cases.map(({ whatTool, whenTool }, index) => {
99
- const identities = caseIdentities[index];
100
-
101
- if (!identities) {
102
- throw new Error(`Missing case identities for case index ${index}`);
103
- }
104
-
105
- const what = buildToolStepFromTool(whatTool, identities.whatId);
106
- const when = buildToolStepFromTool(whenTool, identities.whenId);
60
+ const what = buildToolStepFromTool(whatTool);
61
+ const when = buildToolStepFromTool(whenTool);
107
62
  return { what, when } satisfies Case;
108
63
  });
109
64
 
110
65
  assertNonEmpty(resolved, 'buildBranchStepFromToolPairs requires at least one case');
111
66
 
112
67
  return {
113
- id: branchId,
114
68
  stepKind: CONSTANTS.Enums.StepKind.branch,
115
69
  cases: resolved,
116
- } as BranchStepWithId;
70
+ };
117
71
  }
118
72
 
119
- export function cloneForStepWithIdentities(
73
+ export function cloneForStep(
120
74
  forStep: ForStep,
121
- identities: {
122
- stepId: ForStepId;
123
- whatId: ToolStepId;
124
- whenId: ToolStepId;
125
- },
126
- ): ForStepWithId {
75
+ ): ForStep {
127
76
  return {
128
- id: identities.stepId,
129
77
  stepKind: CONSTANTS.Enums.StepKind.for,
130
78
  case: {
131
- what: {
132
- ...forStep.case.what,
133
- id: identities.whatId,
134
- },
135
- when: {
136
- ...forStep.case.when,
137
- id: identities.whenId,
138
- },
79
+ what: { ...forStep.case.what },
80
+ when: { ...forStep.case.when },
139
81
  },
140
- } as ForStepWithId;
82
+ };
141
83
  }
142
84
 
143
- export function cloneWhileStepWithIdentities(
85
+ export function cloneWhileStep(
144
86
  whileStep: WhileStep,
145
- identities: {
146
- stepId: WhileStepId;
147
- whatId: ToolStepId;
148
- whenId: ToolStepId;
149
- },
150
- ): WhileStepWithId {
87
+ ): WhileStep {
151
88
  return {
152
- id: identities.stepId,
153
89
  stepKind: CONSTANTS.Enums.StepKind.while,
154
90
  case: {
155
- what: {
156
- ...whileStep.case.what,
157
- id: identities.whatId,
158
- },
159
- when: {
160
- ...whileStep.case.when,
161
- id: identities.whenId,
162
- },
91
+ what: { ...whileStep.case.what },
92
+ when: { ...whileStep.case.when },
163
93
  },
164
- } as WhileStepWithId;
94
+ };
165
95
  }
@@ -0,0 +1,42 @@
1
+ import type {
2
+ Step,
3
+ StrategyHandle,
4
+ } from '@toolproof-core/genesis';
5
+ import { getIndependentThreads } from '../parallelizeSteps.js';
6
+ import { CONSTANTS } from '../../lookups/lookups.js';
7
+
8
+ export type ThreadableStrategy<TStrategyState = unknown> = {
9
+ handle: StrategyHandle;
10
+ strategyState: TStrategyState;
11
+ } & (
12
+ | { steps: Step[] }
13
+ | { stepsByThreadIndex: Step[][] }
14
+ );
15
+
16
+ export type ThreadedStrategyLike<TStrategyState = unknown> = {
17
+ handle: StrategyHandle;
18
+ strategyKind: typeof CONSTANTS.Enums.StrategyKind.threaded;
19
+ stepsByThreadIndex: Step[][];
20
+ strategyState: TStrategyState;
21
+ };
22
+
23
+ export function getThreadedStrategyStepGroups<TStrategyState>(strategy: ThreadableStrategy<TStrategyState>): Step[][] {
24
+ if ('steps' in strategy) {
25
+ return getIndependentThreads(strategy.steps, strategy.strategyState as any);
26
+ }
27
+
28
+ return strategy.stepsByThreadIndex;
29
+ }
30
+
31
+ export function buildThreadedStrategy<TStrategyState>(
32
+ threadStepGroups: Step[][],
33
+ threadedStrategyHandle: StrategyHandle,
34
+ strategy: ThreadableStrategy<TStrategyState>,
35
+ ): ThreadedStrategyLike<TStrategyState> {
36
+ return {
37
+ handle: threadedStrategyHandle,
38
+ strategyKind: CONSTANTS.Enums.StrategyKind.threaded,
39
+ stepsByThreadIndex: threadStepGroups,
40
+ strategyState: strategy.strategyState,
41
+ };
42
+ }
@@ -1,44 +1,54 @@
1
1
  import type {
2
2
  RoleName,
3
- RoleValue,
4
- Strategy,
5
- ToolId,
3
+ ToolHandle,
6
4
  Tool,
7
5
  ToolStep,
8
6
  ResourceId,
9
- ResourceTypeId,
7
+ ResourceTypeHandle,
10
8
  Resource,
11
- } from '@toolproof-core/schema';
12
- import { CONSTANTS } from '../artifacts/artifacts.js';
9
+ } from '@toolproof-core/genesis';
13
10
  import {
14
11
  extractToolStepsFromStrategy,
15
12
  getInputRoleEntries,
16
13
  getOutputRoleEntries,
17
14
  toToolRoleDescriptor,
15
+ type StrategyStepGraph,
18
16
  type ToolRoleDescriptor,
19
17
  } from './roleSpec.js';
20
18
 
19
+ const ERROR_OUTPUT_ROLE_NAME = 'ErrorOutput' as RoleName;
21
20
 
22
- export function extractToolIdsFromRawStrategy(rawStrategy: Strategy): ToolId[] {
23
- const toolSteps = extractToolStepsFromRawStrategy(rawStrategy);
24
- const ids = new Set<ToolId>();
21
+ function extractResourceTypeHandleFromRoleValue(toolRoleValue: ToolRoleDescriptor): ResourceTypeHandle | null {
22
+ const typeRef = toolRoleValue.typeRef;
23
+
24
+ if ('resourceTypeHandle' in typeRef) {
25
+ return typeRef.resourceTypeHandle;
26
+ }
27
+
28
+ return null;
29
+ }
30
+
31
+
32
+ export function extractToolHandlesFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph): ToolHandle[] {
33
+ const toolSteps = extractToolStepsFromUnthreadedStrategy(unthreadedStrategy);
34
+ const ids = new Set<ToolHandle>();
25
35
  for (const jStep of toolSteps) {
26
- ids.add(jStep.toolId);
36
+ ids.add(jStep.toolHandle);
27
37
  }
28
38
  return Array.from(ids);
29
39
  }
30
40
 
31
- export function extractToolStepsFromRawStrategy(rawStrategy: Strategy): ToolStep[] {
32
- return extractToolStepsFromStrategy(rawStrategy);
41
+ export function extractToolStepsFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph): ToolStep[] {
42
+ return extractToolStepsFromStrategy(unthreadedStrategy);
33
43
  }
34
44
 
35
45
 
36
- export function extractRoleMapFromRawStrategy(rawStrategy: Strategy, toolMap: Map<ToolId, Tool>): Map<RoleName, ToolRoleDescriptor> {
46
+ export function extractRoleMapFromUnthreadedStrategy(unthreadedStrategy: StrategyStepGraph, toolMap: Map<ToolHandle, Tool>): Map<RoleName, ToolRoleDescriptor> {
37
47
  const roleMap = new Map<RoleName, ToolRoleDescriptor>();
38
- const toolIds = extractToolIdsFromRawStrategy(rawStrategy);
48
+ const toolHandles = extractToolHandlesFromUnthreadedStrategy(unthreadedStrategy);
39
49
 
40
- for (const toolId of toolIds) {
41
- const tool = toolMap.get(toolId);
50
+ for (const toolHandle of toolHandles) {
51
+ const tool = toolMap.get(toolHandle);
42
52
  if (!tool) continue;
43
53
 
44
54
  for (const entry of getInputRoleEntries(tool)) {
@@ -52,23 +62,21 @@ export function extractRoleMapFromRawStrategy(rawStrategy: Strategy, toolMap: Ma
52
62
  return roleMap;
53
63
  }
54
64
 
55
- export function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeId | null {
56
- const errorRoleId = CONSTANTS.Cosmos.ROLE_ErrorOutput;
57
- const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== errorRoleId);
65
+ export function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeHandle | null {
66
+ const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== ERROR_OUTPUT_ROLE_NAME);
58
67
  if (!outputEntries || !outputEntries[0] || outputEntries.length !== 1) {
59
- throw new Error(`Tool ${tool.id} must have exactly one non-error output role to be branchable/loopable`);
68
+ throw new Error(`Tool ${tool.handle} must have exactly one non-error output role to be branchable/loopable`);
60
69
  } // ATTENTION_PUREIFY
61
- const outputRole = outputEntries[0][1] as RoleValue;
62
- return outputRole.resourceTypeId;
70
+ return extractResourceTypeHandleFromRoleValue(toToolRoleDescriptor(outputEntries[0]));
63
71
  }
64
72
 
65
73
 
66
74
 
67
75
  export function extractResourceMapForType<TResource extends Resource = Resource>(
68
- resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>,
69
- resourceTypeId: ResourceTypeId
76
+ resourcesByType: Partial<Record<ResourceTypeHandle, TResource[]>>,
77
+ resourceTypeHandle: ResourceTypeHandle
70
78
  ): Map<ResourceId, TResource> {
71
- const resources = resourcesByType[resourceTypeId] ?? [];
79
+ const resources = resourcesByType[resourceTypeHandle] ?? [];
72
80
  const map = new Map<ResourceId, TResource>();
73
81
 
74
82
  for (const resource of resources) {
@@ -79,34 +87,34 @@ export function extractResourceMapForType<TResource extends Resource = Resource>
79
87
  }
80
88
 
81
89
  // Only use this for resource types whose nuclei are entity-like objects with their own intrinsic id.
82
- export function extractIdKeyedNucleusMapForType<
90
+ export function extractIdKeyedValueMapForType<
83
91
  TKey extends string = string,
84
- TNucleus extends { id: string | number | boolean } = { id: string | number | boolean },
92
+ TValue extends { id: string | number | boolean } = { id: string | number | boolean },
85
93
  TResource extends Resource = Resource
86
94
  >(
87
- resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>,
88
- resourceTypeId: ResourceTypeId,
89
- ): Map<TKey, TNucleus> {
90
- const resourceMap = extractResourceMapForType<TResource>(resourcesByType, resourceTypeId);
91
- const out = new Map<TKey, TNucleus>();
95
+ resourcesByType: Partial<Record<ResourceTypeHandle, TResource[]>>,
96
+ resourceTypeHandle: ResourceTypeHandle,
97
+ ): Map<TKey, TValue> {
98
+ const resourceMap = extractResourceMapForType<TResource>(resourcesByType, resourceTypeHandle);
99
+ const out = new Map<TKey, TValue>();
92
100
 
93
101
  for (const resource of resourceMap.values()) {
94
- const data = (resource as any).nucleus as unknown;
102
+ const data = resource.projection as unknown;
95
103
  if (data === null || data === undefined) {
96
104
  throw new Error(
97
- `Expected resource '${resource.id}' of type '${resourceTypeId}' to have a nucleus`
105
+ `Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have a projection`
98
106
  );
99
107
  }
100
108
  if (typeof data !== 'object') {
101
109
  throw new Error(
102
- `Expected resource '${resource.id}' of type '${resourceTypeId}' to have an object nucleus with id`
110
+ `Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have an object projection with id`
103
111
  );
104
112
  }
105
113
 
106
114
  const maybeId = (data as any).id as unknown;
107
115
  if (maybeId === null || maybeId === undefined) {
108
116
  throw new Error(
109
- `Expected resource '${resource.id}' of type '${resourceTypeId}' to have nucleus.id`
117
+ `Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have projection.id`
110
118
  );
111
119
  }
112
120
  if (
@@ -115,11 +123,11 @@ export function extractIdKeyedNucleusMapForType<
115
123
  typeof maybeId !== 'boolean'
116
124
  ) {
117
125
  throw new Error(
118
- `Expected resource '${resource.id}' of type '${resourceTypeId}' to have nucleus.id as string, number, or boolean`
126
+ `Expected resource '${resource.id}' of type '${resourceTypeHandle}' to have projection.id as string, number, or boolean`
119
127
  );
120
128
  }
121
129
 
122
- const value = data as TNucleus;
130
+ const value = data as TValue;
123
131
  const key = String(maybeId) as TKey;
124
132
 
125
133
  out.set(key, value);