@toolproof-core/lib 1.0.36 → 1.0.37

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 (78) hide show
  1. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +1 -2
  2. package/dist/integrations/firebase/firebaseAdminHelpers.js +5 -14
  3. package/dist/lookups/lookups.d.ts +17 -24
  4. package/dist/lookups/lookups.js +10 -0
  5. package/dist/types/types.d.ts +10 -6
  6. package/dist/utils/cosmosDataExtraction.d.ts +5 -0
  7. package/dist/utils/cosmosDataExtraction.js +15 -0
  8. package/dist/utils/identifierGeneration.d.ts +2 -0
  9. package/dist/utils/identifierGeneration.js +5 -0
  10. package/dist/utils/mutableStrategyOverlay.d.ts +55 -0
  11. package/dist/utils/mutableStrategyOverlay.js +115 -0
  12. package/dist/utils/resourceCreation.d.ts +11 -0
  13. package/dist/utils/resourceCreation.js +32 -0
  14. package/dist/utils/stepCreation.d.ts +9 -0
  15. package/dist/utils/stepCreation.js +79 -0
  16. package/dist/utils/stepParallelization.d.ts +2 -0
  17. package/dist/utils/stepParallelization.js +129 -0
  18. package/dist/utils/strategyAssembly.d.ts +3 -0
  19. package/dist/utils/strategyAssembly.js +6 -0
  20. package/dist/utils/strategyCanonicalization.d.ts +4 -0
  21. package/dist/utils/strategyCanonicalization.js +215 -0
  22. package/dist/utils/strategyExtraction.d.ts +17 -0
  23. package/dist/utils/strategyExtraction.js +88 -0
  24. package/dist/utils/strategyStateResolution.d.ts +17 -0
  25. package/dist/utils/{resolveStrategyStateChain.js → strategyStateResolution.js} +5 -4
  26. package/dist/utils/strategyThreading.d.ts +2 -0
  27. package/dist/utils/strategyThreading.js +10 -0
  28. package/dist/utils/toolStepPaths.d.ts +2 -0
  29. package/dist/utils/toolStepPaths.js +21 -0
  30. package/package.json +41 -21
  31. package/src/integrations/firebase/firebaseAdminHelpers.ts +5 -17
  32. package/src/lookups/lookups.ts +16 -6
  33. package/src/types/types.ts +10 -4
  34. package/src/utils/cosmosDataExtraction.ts +25 -0
  35. package/src/utils/identifierGeneration.ts +12 -0
  36. package/src/utils/mutableStrategyOverlay.ts +286 -0
  37. package/src/utils/resourceCreation.ts +87 -0
  38. package/src/utils/stepCreation.ts +114 -0
  39. package/src/utils/stepParallelization.ts +181 -0
  40. package/src/utils/strategyAssembly.ts +14 -0
  41. package/src/utils/strategyCanonicalization.ts +294 -0
  42. package/src/utils/strategyExtraction.ts +150 -0
  43. package/src/utils/strategyStateResolution.ts +57 -0
  44. package/src/utils/strategyThreading.ts +27 -0
  45. package/src/utils/toolStepPaths.ts +34 -0
  46. package/tsconfig.tsbuildinfo +1 -1
  47. package/dist/integrations/firebase/createStep.d.ts +0 -10
  48. package/dist/integrations/firebase/createStep.js +0 -17
  49. package/dist/integrations/firebase/createThreadedStrategy.d.ts +0 -2
  50. package/dist/integrations/firebase/createThreadedStrategy.js +0 -8
  51. package/dist/utils/creation/resourceCreation.d.ts +0 -21
  52. package/dist/utils/creation/resourceCreation.js +0 -67
  53. package/dist/utils/creation/stepCreation.d.ts +0 -10
  54. package/dist/utils/creation/stepCreation.js +0 -64
  55. package/dist/utils/creation/threadedStrategyCreation.d.ts +0 -18
  56. package/dist/utils/creation/threadedStrategyCreation.js +0 -16
  57. package/dist/utils/extractData.d.ts +0 -12
  58. package/dist/utils/extractData.js +0 -78
  59. package/dist/utils/parallelizeSteps.d.ts +0 -3
  60. package/dist/utils/parallelizeSteps.js +0 -159
  61. package/dist/utils/resolveStrategyStateChain.d.ts +0 -20
  62. package/dist/utils/roleSpec.d.ts +0 -16
  63. package/dist/utils/roleSpec.js +0 -57
  64. package/dist/utils/strategyState.d.ts +0 -12
  65. package/dist/utils/strategyState.js +0 -58
  66. package/dist/utils_2/threadedStrategyCreation.d.ts +0 -5
  67. package/dist/utils_2/threadedStrategyCreation.js +0 -12
  68. package/src/integrations/firebase/createStep.ts +0 -41
  69. package/src/integrations/firebase/createThreadedStrategy.ts +0 -19
  70. package/src/utils/creation/resourceCreation.ts +0 -140
  71. package/src/utils/creation/stepCreation.ts +0 -95
  72. package/src/utils/creation/threadedStrategyCreation.ts +0 -42
  73. package/src/utils/extractData.ts +0 -137
  74. package/src/utils/parallelizeSteps.ts +0 -239
  75. package/src/utils/resolveStrategyStateChain.ts +0 -58
  76. package/src/utils/roleSpec.ts +0 -84
  77. package/src/utils/strategyState.ts +0 -128
  78. package/src/utils_2/threadedStrategyCreation.ts +0 -19
@@ -5,29 +5,17 @@ import type {
5
5
  TimestampedResource,
6
6
  } from '@toolproof-core/genesis';
7
7
  import type {
8
- IdentifierName,
9
- IdentifierStringByName,
10
8
  ResourcesByType,
11
9
  } from '../../types/types.js';
12
- import { CONSTANTS, MAPPINGS } from '../../lookups/lookups.js';
10
+ import { CONSTANTS } from '../../lookups/lookups.js';
13
11
  import { dbAdmin, storageAdmin } from './firebaseAdminInit.js';
14
12
 
15
- export function getNewId<K extends IdentifierName>(identifierName: K): IdentifierStringByName<K> {
16
- const prefix = MAPPINGS.IdentifierNameToPrefix[identifierName];
17
- const docRef = dbAdmin
18
- .collection(CONSTANTS.Persistence.Collections.resources)
19
- .doc(identifierName)
20
- .collection(CONSTANTS.Persistence.Collections.members)
21
- .doc();
22
-
23
- return (prefix + docRef.id) as IdentifierStringByName<K>;
24
- }
25
13
 
26
14
  export async function listResources(
27
15
  resourceTypeHandles: ResourceTypeHandle[],
28
16
  ): Promise<ResourcesByType> {
29
- const bucketResources = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_resources);
30
- const bucketStrategies = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_strategies);
17
+ const resourcesBucket = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_resources);
18
+ const strategiesBucket = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_strategies);
31
19
 
32
20
  async function fetchTimestampedMetadataUnder(
33
21
  resourceTypeHandle: ResourceTypeHandle,
@@ -63,8 +51,8 @@ export async function listResources(
63
51
  resourceId: ResourcePointer['id'],
64
52
  ): Promise<unknown> {
65
53
  const bucket = resourceTypeHandle === CONSTANTS.Handles.ResourceTypes.TYPE_Strategy
66
- ? bucketStrategies
67
- : bucketResources;
54
+ ? strategiesBucket
55
+ : resourcesBucket;
68
56
  const file = bucket.file(`${resourceTypeHandle}/${resourceId}`);
69
57
  const [exists] = await file.exists();
70
58
  if (!exists) {
@@ -4,22 +4,32 @@ export const CONSTANTS = {
4
4
  ..._CONSTANTS,
5
5
  Persistence: {
6
6
  Buckets: {
7
- tp_resources: 'tp-resources',
8
- tp_strategies: 'tp-strategies',
7
+ tp_resources: 'tp-resources' as const,
8
+ tp_strategies: 'tp-strategies' as const,
9
9
  },
10
10
  Collections: {
11
- resources: 'resources',
12
- members: 'members',
11
+ resources: 'resources' as const,
12
+ members: 'members' as const,
13
13
  },
14
14
  },
15
15
  Engine: {
16
16
  Graphs: {
17
- GraphRunStrategy: 'GraphRunStrategy',
18
- GraphBuildStrategy: 'GraphBuildStrategy',
17
+ GraphRunStrategy: 'GraphRunStrategy' as const,
18
+ GraphBuildStrategy: 'GraphBuildStrategy' as const,
19
19
  },
20
20
  },
21
+ Cosmos: {
22
+ ErrorOutputRoleName: 'ErrorOutput' as const,
23
+ }
21
24
  } as const;
22
25
 
23
26
  export const MAPPINGS = {
24
27
  ..._MAPPINGS,
28
+ IdentifierNameToPrefix: {
29
+ ..._MAPPINGS.IdentifierNameToPrefix,
30
+ ToolStepKey: 'TOOL_STEP-',
31
+ BranchStepKey: 'BRANCH_STEP-',
32
+ WhileStepKey: 'WHILE_STEP-',
33
+ ForStepKey: 'FOR_STEP-',
34
+ },
25
35
  } as const;
@@ -5,7 +5,7 @@ import type {
5
5
  import { CONSTANTS } from '../lookups/lookups.js';
6
6
  import { MAPPINGS } from '../lookups/lookups.js';
7
7
 
8
- export type Bucket = typeof CONSTANTS.Persistence.Buckets.tp_resources;
8
+ export type Bucket = keyof typeof CONSTANTS.Persistence.Buckets;
9
9
 
10
10
  export type Collection = keyof typeof CONSTANTS.Persistence.Collections;
11
11
 
@@ -17,11 +17,17 @@ export type IdentifierName = keyof typeof MAPPINGS.IdentifierNameToPrefix;
17
17
 
18
18
  export type IdentifierStringByName<K extends IdentifierName> = `${(typeof MAPPINGS.IdentifierNameToPrefix)[K]}${string}`;
19
19
 
20
- export interface SmallBaseProjection<T extends string = string> {
21
- id: T;
20
+ export type ItemKey = string;
21
+
22
+ export type BaseItem = {
23
+ itemKey: ItemKey;
22
24
  }
23
25
 
24
- export interface LargeBaseProjection<T extends string = string> extends SmallBaseProjection<T> {
26
+ export type DocumentedBaseItem = BaseItem & { // ATTENTION: reuse ontology concept
25
27
  name: string;
26
28
  description: string;
29
+ }
30
+
31
+ export type Item = DocumentedBaseItem & {
32
+ label: string;
27
33
  }
@@ -0,0 +1,25 @@
1
+ import type { Resource, TimestampedResource } from '@toolproof-core/genesis';
2
+
3
+
4
+ export function timestampedResourceToResource(timestampedResource: TimestampedResource): Resource {
5
+ const { timestamp: _timestamp, ...resource } = timestampedResource;
6
+ return resource;
7
+ }
8
+
9
+ export function extractProjectionMapByHandle<TKey extends string, TProjection extends { handle: TKey }>(
10
+ resources: Resource[] | undefined,
11
+ ): Map<TKey, TProjection> {
12
+ const map = new Map<TKey, TProjection>();
13
+
14
+ for (const resource of resources ?? []) {
15
+ const projection = resource.projection as (TProjection & { handle?: TKey }) | null;
16
+
17
+ if (!projection || typeof projection !== 'object' || !projection.handle) {
18
+ continue;
19
+ }
20
+
21
+ map.set(projection.handle, projection);
22
+ }
23
+
24
+ return map;
25
+ }
@@ -0,0 +1,12 @@
1
+ import type {
2
+ IdentifierName,
3
+ IdentifierStringByName,
4
+ } from '../types/types.js';
5
+ import { MAPPINGS } from '../lookups/lookups.js';
6
+
7
+ export function generateIdentifier<K extends IdentifierName>(identifierName: K): IdentifierStringByName<K> {
8
+ const prefix = MAPPINGS.IdentifierNameToPrefix[identifierName];
9
+
10
+
11
+ return (prefix + crypto.randomUUID()) as IdentifierStringByName<K>;
12
+ }
@@ -0,0 +1,286 @@
1
+ import type {
2
+ BranchStep,
3
+ Case,
4
+ ExternalInputPotential,
5
+ ForStep,
6
+ InputResource,
7
+ InternalInputPotential,
8
+ Resource,
9
+ RoleName,
10
+ Strategy,
11
+ Tool,
12
+ ToolHandle,
13
+ ToolStep,
14
+ ToolStepRoleAddress,
15
+ WhileStep,
16
+ } from '@toolproof-core/genesis';
17
+ import { extractResourceTypeHandleFromRoleValue } from './strategyExtraction.js';
18
+
19
+ export type MutableToolStepKey = string;
20
+ export type MutableBranchStepKey = string;
21
+ export type MutableWhileStepKey = string;
22
+ export type MutableForStepKey = string;
23
+ export type MutableLoopStepKey = MutableWhileStepKey | MutableForStepKey;
24
+ export type MutableMacroStepKey = MutableBranchStepKey | MutableLoopStepKey;
25
+
26
+ export type MutableInputResource = InputResource;
27
+
28
+ export type MutableToolStepRoleAddress<TKey extends string = MutableToolStepKey> =
29
+ Omit<ToolStepRoleAddress, 'toolStepPath'> & {
30
+ toolStepKey: TKey;
31
+ };
32
+
33
+ export type MutableInternalInputPotential<TKey extends string = MutableToolStepKey> =
34
+ Omit<InternalInputPotential, 'toolStepRoleAddress'> & {
35
+ toolStepRoleAddress: MutableToolStepRoleAddress<TKey>;
36
+ };
37
+
38
+ export type MutableExternalInputPotential = ExternalInputPotential;
39
+
40
+ export type MutableStrategyStateInputEntry<TKey extends string = MutableToolStepKey> =
41
+ | MutableInputResource
42
+ | MutableInternalInputPotential<TKey>
43
+ | MutableExternalInputPotential;
44
+
45
+ export type MutableStrategyStateInputEntryByRoleName<
46
+ TKey extends string = MutableToolStepKey,
47
+ > = Partial<Record<RoleName, MutableStrategyStateInputEntry<TKey>>>;
48
+
49
+ export type MutableStrategyState<
50
+ TKey extends string = MutableToolStepKey,
51
+ > = Partial<Record<TKey, MutableStrategyStateInputEntryByRoleName<TKey>>>;
52
+
53
+ export type MutableToolStep<TKey extends string = MutableToolStepKey> = ToolStep & {
54
+ toolStepKey: TKey;
55
+ };
56
+
57
+ export type MutableCase<TKey extends string = MutableToolStepKey> = Omit<Case, 'when' | 'what'> & {
58
+ when: MutableToolStep<TKey>;
59
+ what: MutableToolStep<TKey>;
60
+ };
61
+
62
+ export type MutableBranchStep<TKey extends string = MutableToolStepKey> = Omit<BranchStep, 'cases'> & {
63
+ macroStepKey: MutableBranchStepKey;
64
+ cases: [MutableCase<TKey>, ...MutableCase<TKey>[]];
65
+ };
66
+
67
+ export type MutableWhileStep<TKey extends string = MutableToolStepKey> = Omit<WhileStep, 'case'> & {
68
+ macroStepKey: MutableWhileStepKey;
69
+ case: MutableCase<TKey>;
70
+ };
71
+
72
+ export type MutableForStep<TKey extends string = MutableToolStepKey> = Omit<ForStep, 'case'> & {
73
+ macroStepKey: MutableForStepKey;
74
+ case: MutableCase<TKey>;
75
+ };
76
+
77
+ export type MutableStep<TKey extends string = MutableToolStepKey> =
78
+ | MutableToolStep<TKey>
79
+ | MutableBranchStep<TKey>
80
+ | MutableWhileStep<TKey>
81
+ | MutableForStep<TKey>;
82
+
83
+ export type MutableStrategy<
84
+ TKey extends string = MutableToolStepKey,
85
+ TStep extends MutableStep<TKey> = MutableStep<TKey>,
86
+ TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
87
+ > = Omit<Strategy, 'stepsByThreadIndex' | 'strategyState'> & {
88
+ stepsByThreadIndex: TStep[][];
89
+ strategyState: TState;
90
+ };
91
+
92
+ export function getAuthoringSteps<
93
+ TKey extends string = MutableToolStepKey,
94
+ TStep extends MutableStep<TKey> = MutableStep<TKey>,
95
+ TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
96
+ >(
97
+ strategy: MutableStrategy<TKey, TStep, TState>,
98
+ ): TStep[] {
99
+ return strategy.stepsByThreadIndex[0] ?? [];
100
+ }
101
+
102
+ export function setAuthoringSteps< // ATTENTION
103
+ TKey extends string = MutableToolStepKey,
104
+ TStep extends MutableStep<TKey> = MutableStep<TKey>,
105
+ TState extends MutableStrategyState<TKey> = MutableStrategyState<TKey>,
106
+ >(
107
+ strategy: MutableStrategy<TKey, TStep, TState>,
108
+ steps: TStep[],
109
+ ): MutableStrategy<TKey, TStep, TState> {
110
+ const nextStepsByThreadIndex = [...strategy.stepsByThreadIndex];
111
+ nextStepsByThreadIndex[0] = steps;
112
+
113
+ return {
114
+ ...strategy,
115
+ stepsByThreadIndex: nextStepsByThreadIndex,
116
+ };
117
+ }
118
+
119
+ export function getStrategyStateInputEntryByRoleName<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
120
+ strategyState: TStrategyState,
121
+ toolStepKey: TKey,
122
+ ): MutableStrategyStateInputEntryByRoleName<TKey> | undefined {
123
+ return strategyState[toolStepKey];
124
+ }
125
+
126
+ export function getStrategyStateInputEntry<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
127
+ strategyState: TStrategyState,
128
+ toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
129
+ ): MutableStrategyStateInputEntry<TKey> | undefined {
130
+ const inputEntryByRoleName = getStrategyStateInputEntryByRoleName(strategyState, toolStepRoleAddress.toolStepKey);
131
+ return inputEntryByRoleName?.[toolStepRoleAddress.roleName];
132
+ }
133
+
134
+ export function setStrategyStateInputEntry<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
135
+ strategyState: TStrategyState,
136
+ toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
137
+ entry: MutableStrategyStateInputEntry<TKey>,
138
+ ): TStrategyState {
139
+ const inputEntryByRoleName = getStrategyStateInputEntryByRoleName(strategyState, toolStepRoleAddress.toolStepKey) ?? {};
140
+
141
+ return {
142
+ ...strategyState,
143
+ [toolStepRoleAddress.toolStepKey]: {
144
+ ...inputEntryByRoleName,
145
+ [toolStepRoleAddress.roleName]: entry,
146
+ },
147
+ };
148
+ }
149
+
150
+ export function clearInputBinding<
151
+ TKey extends string,
152
+ TStrategyState extends MutableStrategyState<TKey>
153
+ >(
154
+ strategyState: TStrategyState,
155
+ toolStepRoleAddress: MutableToolStepRoleAddress<TKey>,
156
+ ): TStrategyState {
157
+ const inputEntryByRoleName =
158
+ getStrategyStateInputEntryByRoleName(
159
+ strategyState,
160
+ toolStepRoleAddress.toolStepKey,
161
+ );
162
+
163
+ if (!inputEntryByRoleName || !(toolStepRoleAddress.roleName in inputEntryByRoleName)) {
164
+ return strategyState;
165
+ }
166
+
167
+ const nextInputEntryByRoleName = { ...inputEntryByRoleName };
168
+ delete nextInputEntryByRoleName[toolStepRoleAddress.roleName];
169
+
170
+ if (Object.keys(nextInputEntryByRoleName).length === 0) {
171
+ const nextStrategyState = { ...strategyState };
172
+ delete nextStrategyState[toolStepRoleAddress.toolStepKey];
173
+ return nextStrategyState as TStrategyState;
174
+ }
175
+
176
+ return {
177
+ ...strategyState,
178
+ [toolStepRoleAddress.toolStepKey]: nextInputEntryByRoleName,
179
+ };
180
+ }
181
+
182
+ export function bindInputResource<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
183
+ strategyState: TStrategyState,
184
+ target: MutableToolStepRoleAddress<TKey>,
185
+ resource: Resource,
186
+ ): TStrategyState {
187
+ const mutableInputResource: MutableInputResource = {
188
+ ...resource,
189
+ strategyStateInputKind: 'inputResource',
190
+ };
191
+
192
+ return setStrategyStateInputEntry(strategyState, target, mutableInputResource);
193
+ }
194
+
195
+ function getInputRoleValue(tool: Tool, roleName: RoleName) {
196
+ return tool.roleSpec.inputRoleValueByName[roleName];
197
+ }
198
+
199
+ function getOutputRoleValue(tool: Tool, roleName: RoleName) {
200
+ return tool.roleSpec.outputRoleValueByName[roleName];
201
+ }
202
+
203
+ export function bindInternalInputPotential<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
204
+ strategyState: TStrategyState,
205
+ targetAddress: MutableToolStepRoleAddress<TKey>,
206
+ sourceAddress: MutableToolStepRoleAddress<TKey>,
207
+ context: {
208
+ toolStepByKey: ReadonlyMap<TKey, MutableToolStep<TKey>>;
209
+ toolMap: ReadonlyMap<ToolHandle, Tool>;
210
+ }
211
+ ): TStrategyState {
212
+
213
+ const sourceToolStep = context.toolStepByKey.get(sourceAddress.toolStepKey) ?? null;
214
+ if (!sourceToolStep) {
215
+ throw new Error(`Source tool step not found for '${sourceAddress.toolStepKey}'.`);
216
+ }
217
+
218
+ const targetToolStep = context.toolStepByKey.get(targetAddress.toolStepKey) ?? null;
219
+ if (!targetToolStep) {
220
+ throw new Error(`Target tool step not found for '${targetAddress.toolStepKey}'.`);
221
+ }
222
+
223
+ const sourceTool = context.toolMap.get(sourceToolStep.toolHandle) ?? null;
224
+ if (!sourceTool) {
225
+ throw new Error(`Source tool not found for '${sourceToolStep.toolHandle}'.`);
226
+ }
227
+
228
+ const targetTool = context.toolMap.get(targetToolStep.toolHandle) ?? null;
229
+ if (!targetTool) {
230
+ throw new Error(`Target tool not found for '${targetToolStep.toolHandle}'.`);
231
+ }
232
+
233
+ const targetInputRole = getInputRoleValue(targetTool, targetAddress.roleName);
234
+ if (!targetInputRole) {
235
+ throw new Error(`Target input role not found for (${targetAddress.toolStepKey}, ${targetAddress.roleName}).`);
236
+ }
237
+
238
+ const sourceOutputRole = getOutputRoleValue(sourceTool, sourceAddress.roleName);
239
+ const sourceInputRole = getInputRoleValue(sourceTool, sourceAddress.roleName);
240
+
241
+ const sourceInputEntryByRoleName = strategyState[sourceAddress.toolStepKey];
242
+ const sourceInputEntry = sourceInputEntryByRoleName?.[sourceAddress.roleName];
243
+
244
+ const sourceRoleType = sourceOutputRole
245
+ ? extractResourceTypeHandleFromRoleValue(sourceOutputRole)
246
+ : sourceInputRole
247
+ ? extractResourceTypeHandleFromRoleValue(sourceInputRole)
248
+ : null;
249
+
250
+ const targetRoleType = extractResourceTypeHandleFromRoleValue(targetInputRole);
251
+
252
+ if (!sourceOutputRole && sourceInputRole) {
253
+ if (!sourceInputEntry) {
254
+ throw new Error(`Source input role is not bound for (${sourceAddress.toolStepKey}, ${sourceAddress.roleName}).`); // ATTENTION: not an ontological invariant, but it might help catch some mistakes early on.
255
+ }
256
+ }
257
+
258
+ if (!sourceOutputRole && !sourceInputRole) {
259
+ throw new Error(`Source role not found for (${sourceAddress.toolStepKey}, ${sourceAddress.roleName}).`);
260
+ }
261
+
262
+ if (!sourceRoleType || !targetRoleType || sourceRoleType !== targetRoleType) {
263
+ throw new Error(
264
+ `Reference type mismatch between source (${sourceAddress.toolStepKey}, ${sourceAddress.roleName}) and target (${targetAddress.toolStepKey}, ${targetAddress.roleName}).`
265
+ );
266
+ }
267
+
268
+ return setStrategyStateInputEntry(strategyState, targetAddress, {
269
+ strategyStateInputKind: 'internalInputPotential',
270
+ toolStepRoleAddress: {
271
+ toolStepKey: sourceAddress.toolStepKey,
272
+ roleName: sourceAddress.roleName,
273
+ },
274
+ });
275
+ }
276
+
277
+ export function bindExternalInputPotential<TKey extends string, TStrategyState extends MutableStrategyState<TKey>>(
278
+ strategyState: TStrategyState,
279
+ target: MutableToolStepRoleAddress<TKey>,
280
+ ): TStrategyState {
281
+ const mutableExternalInputPotential: MutableExternalInputPotential = {
282
+ strategyStateInputKind: 'externalInputPotential',
283
+ };
284
+
285
+ return setStrategyStateInputEntry(strategyState, target, mutableExternalInputPotential);
286
+ }
@@ -0,0 +1,87 @@
1
+ import type {
2
+ ToolStepRoleAddress,
3
+ ResourceId,
4
+ Resource,
5
+ ResourcePointer,
6
+ ResourceTypeHandle,
7
+ RuntimeProvenance,
8
+ StrategyProvenance,
9
+ Provenance,
10
+ StrategyTraceHandle,
11
+ } from '@toolproof-core/genesis';
12
+ import { CONSTANTS } from '../lookups/lookups.js';
13
+
14
+
15
+ export type ResourceCreationDescriptor =
16
+ | {
17
+ id: ResourceId;
18
+ resourceTypeHandle: ResourceTypeHandle;
19
+ }
20
+ | {
21
+ id: ResourceId;
22
+ resourceTypeHandle: ResourceTypeHandle;
23
+ toolStepRoleAddress: ToolStepRoleAddress;
24
+ strategyTraceHandle: StrategyTraceHandle;
25
+ };
26
+
27
+ function createStrategyProvenance(
28
+ strategyTraceHandle: StrategyTraceHandle,
29
+ toolStepRoleAddress: ToolStepRoleAddress,
30
+ ): StrategyProvenance {
31
+ return {
32
+ provenanceKind: CONSTANTS.Enums.ProvenanceKind.strategy,
33
+ strategyTraceHandle,
34
+ toolStepRoleAddress,
35
+ };
36
+ }
37
+
38
+ function createRuntimeProvenance(): RuntimeProvenance {
39
+ return {
40
+ provenanceKind: CONSTANTS.Enums.ProvenanceKind.runtime,
41
+ };
42
+ }
43
+
44
+ function createResourcePointer(
45
+ id: ResourceId,
46
+ resourceTypeHandle: ResourceTypeHandle,
47
+ provenance: Provenance,
48
+ ): ResourcePointer {
49
+ return {
50
+ id,
51
+ resourceTypeHandle,
52
+ provenance,
53
+ };
54
+ }
55
+
56
+ function createResource(
57
+ resourcePointer: ResourcePointer,
58
+ projection: unknown,
59
+ ): Resource {
60
+ return {
61
+ ...resourcePointer,
62
+ projection,
63
+ };
64
+ }
65
+
66
+ export function createResourceFromDescriptor(
67
+ descriptor: ResourceCreationDescriptor,
68
+ projection: unknown,
69
+ ): Resource {
70
+ const provenance =
71
+ 'toolStepRoleAddress' in descriptor
72
+ ? createStrategyProvenance(
73
+ descriptor.strategyTraceHandle,
74
+ descriptor.toolStepRoleAddress,
75
+ )
76
+ : createRuntimeProvenance();
77
+
78
+ return createResource(
79
+ createResourcePointer(
80
+ descriptor.id,
81
+ descriptor.resourceTypeHandle,
82
+ provenance,
83
+ ),
84
+ projection,
85
+ );
86
+ }
87
+
@@ -0,0 +1,114 @@
1
+ import type {
2
+ Tool,
3
+ } from '@toolproof-core/genesis';
4
+ import type {
5
+ MutableToolStep,
6
+ MutableCase,
7
+ MutableWhileStep,
8
+ MutableForStep,
9
+ MutableBranchStep,
10
+ } from './mutableStrategyOverlay.js';
11
+ import { CONSTANTS } from '../lookups/lookups.js';
12
+ import { getInputRoleNamesFromTool, getOutputRoleNamesFromTool } from './strategyExtraction.js';
13
+ import { generateIdentifier } from './identifierGeneration.js';
14
+
15
+
16
+ function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
17
+ if (arr.length === 0) {
18
+ throw new Error(msg);
19
+ }
20
+ }
21
+
22
+ function getRoleBindingSpec(tool: Tool): MutableToolStep['roleBindingSpec'] {
23
+ return {
24
+ inputBindings: getInputRoleNamesFromTool(tool),
25
+ outputBindings: getOutputRoleNamesFromTool(tool),
26
+ };
27
+ }
28
+
29
+ export function createToolStepFromTool(
30
+ tool: Tool,
31
+ ): MutableToolStep {
32
+ return {
33
+ toolStepKey: generateIdentifier('ToolStepKey'),
34
+ stepKind: CONSTANTS.Enums.StepKind.tool,
35
+ toolHandle: tool.handle,
36
+ roleBindingSpec: getRoleBindingSpec(tool),
37
+ };
38
+ }
39
+
40
+ export function createCaseFromToolStepPair(
41
+ what: MutableToolStep,
42
+ when: MutableToolStep,
43
+ ): MutableCase {
44
+ return { what, when };
45
+ }
46
+
47
+ export function createLoopStepFromCase(
48
+ stepCase: MutableCase,
49
+ stepKind: typeof CONSTANTS.Enums.StepKind.for | typeof CONSTANTS.Enums.StepKind.while,
50
+ ): MutableForStep | MutableWhileStep {
51
+ if (stepKind === CONSTANTS.Enums.StepKind.for) {
52
+ return {
53
+ macroStepKey: generateIdentifier('ForStepKey'),
54
+ stepKind: CONSTANTS.Enums.StepKind.for,
55
+ case: stepCase,
56
+ };
57
+ }
58
+
59
+ return {
60
+ macroStepKey: generateIdentifier('WhileStepKey'),
61
+ stepKind: CONSTANTS.Enums.StepKind.while,
62
+ case: stepCase,
63
+ };
64
+ }
65
+
66
+ export function createBranchStepFromCases(
67
+ cases: MutableCase[],
68
+ ): MutableBranchStep {
69
+ assertNonEmpty(cases, 'createBranchStepFromCases requires at least one case');
70
+
71
+ return {
72
+ macroStepKey: generateIdentifier('BranchStepKey'),
73
+ stepKind: CONSTANTS.Enums.StepKind.branch,
74
+ cases,
75
+ };
76
+ }
77
+
78
+ export function cloneForStep(
79
+ forStep: MutableForStep,
80
+ ): MutableForStep {
81
+ return {
82
+ macroStepKey: generateIdentifier('ForStepKey'),
83
+ stepKind: CONSTANTS.Enums.StepKind.for,
84
+ case: {
85
+ what: {
86
+ ...forStep.case.what,
87
+ toolStepKey: generateIdentifier('ToolStepKey'),
88
+ },
89
+ when: {
90
+ ...forStep.case.when,
91
+ toolStepKey: generateIdentifier('ToolStepKey'),
92
+ },
93
+ },
94
+ };
95
+ }
96
+
97
+ export function cloneWhileStep(
98
+ whileStep: MutableWhileStep,
99
+ ): MutableWhileStep {
100
+ return {
101
+ macroStepKey: generateIdentifier('WhileStepKey'),
102
+ stepKind: CONSTANTS.Enums.StepKind.while,
103
+ case: {
104
+ what: {
105
+ ...whileStep.case.what,
106
+ toolStepKey: generateIdentifier('ToolStepKey'),
107
+ },
108
+ when: {
109
+ ...whileStep.case.when,
110
+ toolStepKey: generateIdentifier('ToolStepKey'),
111
+ },
112
+ },
113
+ };
114
+ }