@toolproof-core/lib 1.0.31 → 1.0.33

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 (65) hide show
  1. package/dist/artifacts/artifacts.d.ts +51 -51
  2. package/dist/artifacts/artifacts.d.ts.map +1 -1
  3. package/dist/integrations/firebase/createRunnableStrategy.d.ts +2 -2
  4. package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +1 -1
  5. package/dist/integrations/firebase/createRunnableStrategy.js +2 -3
  6. package/dist/integrations/firebase/createRunnableStrategy.js.map +1 -1
  7. package/dist/integrations/firebase/createStep.d.ts +8 -8
  8. package/dist/integrations/firebase/createStep.d.ts.map +1 -1
  9. package/dist/integrations/firebase/createStep.js.map +1 -1
  10. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +8 -4
  11. package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +1 -1
  12. package/dist/integrations/firebase/firebaseAdminHelpers.js.map +1 -1
  13. package/dist/types/types.d.ts +5 -3
  14. package/dist/types/types.d.ts.map +1 -1
  15. package/dist/utils/bindInputRoleToResource.d.ts +5 -4
  16. package/dist/utils/bindInputRoleToResource.d.ts.map +1 -1
  17. package/dist/utils/bindInputRoleToResource.js +15 -65
  18. package/dist/utils/bindInputRoleToResource.js.map +1 -1
  19. package/dist/utils/creation/resourceCreation.d.ts +20 -8
  20. package/dist/utils/creation/resourceCreation.d.ts.map +1 -1
  21. package/dist/utils/creation/resourceCreation.js +16 -12
  22. package/dist/utils/creation/resourceCreation.js.map +1 -1
  23. package/dist/utils/creation/runnableStrategyCreation.d.ts +3 -3
  24. package/dist/utils/creation/runnableStrategyCreation.d.ts.map +1 -1
  25. package/dist/utils/creation/runnableStrategyCreation.js +8 -14
  26. package/dist/utils/creation/runnableStrategyCreation.js.map +1 -1
  27. package/dist/utils/creation/stepCreation.d.ts +41 -24
  28. package/dist/utils/creation/stepCreation.d.ts.map +1 -1
  29. package/dist/utils/creation/stepCreation.js +3 -2
  30. package/dist/utils/creation/stepCreation.js.map +1 -1
  31. package/dist/utils/extractData.d.ts +8 -7
  32. package/dist/utils/extractData.d.ts.map +1 -1
  33. package/dist/utils/extractData.js +7 -49
  34. package/dist/utils/extractData.js.map +1 -1
  35. package/dist/utils/parallelizeSteps.d.ts +3 -2
  36. package/dist/utils/parallelizeSteps.d.ts.map +1 -1
  37. package/dist/utils/parallelizeSteps.js +17 -10
  38. package/dist/utils/parallelizeSteps.js.map +1 -1
  39. package/dist/utils/resolveResourceChain.d.ts +10 -14
  40. package/dist/utils/resolveResourceChain.d.ts.map +1 -1
  41. package/dist/utils/resolveResourceChain.js +12 -20
  42. package/dist/utils/resolveResourceChain.js.map +1 -1
  43. package/dist/utils/roleSpec.d.ts +12 -0
  44. package/dist/utils/roleSpec.d.ts.map +1 -0
  45. package/dist/utils/roleSpec.js +58 -0
  46. package/dist/utils/roleSpec.js.map +1 -0
  47. package/dist/utils/strategyState.d.ts +7 -0
  48. package/dist/utils/strategyState.d.ts.map +1 -0
  49. package/dist/utils/strategyState.js +29 -0
  50. package/dist/utils/strategyState.js.map +1 -0
  51. package/package.json +3 -3
  52. package/src/integrations/firebase/createRunnableStrategy.ts +5 -7
  53. package/src/integrations/firebase/createStep.ts +13 -13
  54. package/src/integrations/firebase/firebaseAdminHelpers.ts +10 -9
  55. package/src/types/types.ts +5 -5
  56. package/src/utils/bindInputRoleToResource.ts +36 -93
  57. package/src/utils/creation/resourceCreation.ts +54 -36
  58. package/src/utils/creation/runnableStrategyCreation.ts +16 -30
  59. package/src/utils/creation/stepCreation.ts +54 -48
  60. package/src/utils/extractData.ts +37 -79
  61. package/src/utils/parallelizeSteps.ts +37 -28
  62. package/src/utils/resolveResourceChain.ts +24 -38
  63. package/src/utils/roleSpec.ts +80 -0
  64. package/src/utils/strategyState.ts +56 -0
  65. package/tsconfig.tsbuildinfo +1 -1
@@ -1,35 +1,41 @@
1
1
  import type {
2
- BranchStepIdJson,
3
- BranchStepJson,
4
- CaseJson,
5
- ForStepIdJson,
6
- ForStepJson,
7
- ToolJson,
8
- ToolStepIdJson,
9
- ToolStepJson,
10
- ResourceRoleIdJson,
11
- WhileStepIdJson,
12
- WhileStepJson,
2
+ BranchStep,
3
+ Case,
4
+ ForStep,
5
+ Tool,
6
+ ToolStep,
7
+ WhileStep,
13
8
  } from '@toolproof-core/schema';
14
9
  import { CONSTANTS } from '../../artifacts/artifacts.js';
10
+ import { getInputRoleNames, getOutputRoleNames } from '../roleSpec.js';
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 };
15
21
 
16
22
  export type LoopStepBuildIdentities =
17
23
  | {
18
24
  stepKind: typeof CONSTANTS.Enums.StepKind.for;
19
- stepId: ForStepIdJson;
20
- whatId: ToolStepIdJson;
21
- whenId: ToolStepIdJson;
25
+ stepId: ForStepId;
26
+ whatId: ToolStepId;
27
+ whenId: ToolStepId;
22
28
  }
23
29
  | {
24
30
  stepKind: typeof CONSTANTS.Enums.StepKind.while;
25
- stepId: WhileStepIdJson;
26
- whatId: ToolStepIdJson;
27
- whenId: ToolStepIdJson;
31
+ stepId: WhileStepId;
32
+ whatId: ToolStepId;
33
+ whenId: ToolStepId;
28
34
  };
29
35
 
30
36
  export type BranchCaseBuildIdentities = {
31
- whatId: ToolStepIdJson;
32
- whenId: ToolStepIdJson;
37
+ whatId: ToolStepId;
38
+ whenId: ToolStepId;
33
39
  };
34
40
 
35
41
  function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
@@ -38,30 +44,30 @@ function assertNonEmpty<T>(arr: T[], msg: string): asserts arr is [T, ...T[]] {
38
44
  }
39
45
  }
40
46
 
41
- function getRoleBindingSpec(tool: ToolJson): ToolStepJson['roleBindingSpec'] {
47
+ function getRoleBindingSpec(tool: Tool): ToolStep['roleBindingSpec'] {
42
48
  return {
43
- inputBindings: Object.keys(tool.roleSpec.inputRoleValueById) as ResourceRoleIdJson[],
44
- outputBindings: Object.keys(tool.roleSpec.outputRoleValueById) as ResourceRoleIdJson[],
49
+ inputBindings: getInputRoleNames(tool),
50
+ outputBindings: getOutputRoleNames(tool),
45
51
  };
46
52
  }
47
53
 
48
54
  export function buildToolStepFromTool(
49
- tool: ToolJson,
50
- id: ToolStepIdJson,
51
- ): ToolStepJson {
55
+ tool: Tool,
56
+ id: ToolStepId,
57
+ ): ToolStepWithId {
52
58
  return {
53
59
  id,
54
60
  stepKind: CONSTANTS.Enums.StepKind.tool,
55
61
  toolId: tool.id,
56
62
  roleBindingSpec: getRoleBindingSpec(tool),
57
- };
63
+ } as ToolStepWithId;
58
64
  }
59
65
 
60
66
  export function buildLoopStepFromToolPair(
61
- whatTool: ToolJson,
62
- whenTool: ToolJson,
67
+ whatTool: Tool,
68
+ whenTool: Tool,
63
69
  identities: LoopStepBuildIdentities,
64
- ): ForStepJson | WhileStepJson {
70
+ ): ForStep | WhileStep {
65
71
  const what = buildToolStepFromTool(whatTool, identities.whatId);
66
72
  const when = buildToolStepFromTool(whenTool, identities.whenId);
67
73
 
@@ -70,21 +76,21 @@ export function buildLoopStepFromToolPair(
70
76
  id: identities.stepId,
71
77
  stepKind: CONSTANTS.Enums.StepKind.for,
72
78
  case: { what, when },
73
- };
79
+ } as ForStepWithId;
74
80
  }
75
81
 
76
82
  return {
77
83
  id: identities.stepId,
78
84
  stepKind: CONSTANTS.Enums.StepKind.while,
79
85
  case: { what, when },
80
- };
86
+ } as WhileStepWithId;
81
87
  }
82
88
 
83
89
  export function buildBranchStepFromToolPairs(
84
- cases: Array<{ whatTool: ToolJson; whenTool: ToolJson }>,
85
- branchId: BranchStepIdJson,
90
+ cases: Array<{ whatTool: Tool; whenTool: Tool }>,
91
+ branchId: BranchStepId,
86
92
  caseIdentities: BranchCaseBuildIdentities[],
87
- ): BranchStepJson {
93
+ ): BranchStepWithId {
88
94
  if (cases.length !== caseIdentities.length) {
89
95
  throw new Error('buildBranchStepFromToolPairs requires one id pair per case');
90
96
  }
@@ -98,7 +104,7 @@ export function buildBranchStepFromToolPairs(
98
104
 
99
105
  const what = buildToolStepFromTool(whatTool, identities.whatId);
100
106
  const when = buildToolStepFromTool(whenTool, identities.whenId);
101
- return { what, when } satisfies CaseJson;
107
+ return { what, when } satisfies Case;
102
108
  });
103
109
 
104
110
  assertNonEmpty(resolved, 'buildBranchStepFromToolPairs requires at least one case');
@@ -107,17 +113,17 @@ export function buildBranchStepFromToolPairs(
107
113
  id: branchId,
108
114
  stepKind: CONSTANTS.Enums.StepKind.branch,
109
115
  cases: resolved,
110
- };
116
+ } as BranchStepWithId;
111
117
  }
112
118
 
113
119
  export function cloneForStepWithIdentities(
114
- forStep: ForStepJson,
120
+ forStep: ForStep,
115
121
  identities: {
116
- stepId: ForStepIdJson;
117
- whatId: ToolStepIdJson;
118
- whenId: ToolStepIdJson;
122
+ stepId: ForStepId;
123
+ whatId: ToolStepId;
124
+ whenId: ToolStepId;
119
125
  },
120
- ): ForStepJson {
126
+ ): ForStepWithId {
121
127
  return {
122
128
  id: identities.stepId,
123
129
  stepKind: CONSTANTS.Enums.StepKind.for,
@@ -131,17 +137,17 @@ export function cloneForStepWithIdentities(
131
137
  id: identities.whenId,
132
138
  },
133
139
  },
134
- };
140
+ } as ForStepWithId;
135
141
  }
136
142
 
137
143
  export function cloneWhileStepWithIdentities(
138
- whileStep: WhileStepJson,
144
+ whileStep: WhileStep,
139
145
  identities: {
140
- stepId: WhileStepIdJson;
141
- whatId: ToolStepIdJson;
142
- whenId: ToolStepIdJson;
146
+ stepId: WhileStepId;
147
+ whatId: ToolStepId;
148
+ whenId: ToolStepId;
143
149
  },
144
- ): WhileStepJson {
150
+ ): WhileStepWithId {
145
151
  return {
146
152
  id: identities.stepId,
147
153
  stepKind: CONSTANTS.Enums.StepKind.while,
@@ -155,5 +161,5 @@ export function cloneWhileStepWithIdentities(
155
161
  id: identities.whenId,
156
162
  },
157
163
  },
158
- };
164
+ } as WhileStepWithId;
159
165
  }
@@ -1,117 +1,75 @@
1
1
  import type {
2
- RawStrategyJson,
3
- ToolIdJson,
4
- ToolJson,
5
- ToolStepIdJson,
6
- ToolStepJson,
7
- StepJson,
8
- ResourceRoleIdJson,
9
- ResourceRoleJson,
10
- ResourceIdJson,
11
- ResourceTypeIdJson,
12
- ResourceJson,
2
+ RoleName,
3
+ RoleValue,
4
+ Strategy,
5
+ ToolId,
6
+ Tool,
7
+ ToolStep,
8
+ ResourceId,
9
+ ResourceTypeId,
10
+ Resource,
13
11
  } from '@toolproof-core/schema';
14
12
  import { CONSTANTS } from '../artifacts/artifacts.js';
13
+ import {
14
+ extractToolStepsFromStrategy,
15
+ getInputRoleEntries,
16
+ getOutputRoleEntries,
17
+ toToolRoleDescriptor,
18
+ type ToolRoleDescriptor,
19
+ } from './roleSpec.js';
15
20
 
16
21
 
17
- export function extractToolIdsFromRawStrategy(rawStrategy: RawStrategyJson): ToolIdJson[] {
22
+ export function extractToolIdsFromRawStrategy(rawStrategy: Strategy): ToolId[] {
18
23
  const toolSteps = extractToolStepsFromRawStrategy(rawStrategy);
19
- const ids = new Set<ToolIdJson>();
24
+ const ids = new Set<ToolId>();
20
25
  for (const jStep of toolSteps) {
21
26
  ids.add(jStep.toolId);
22
27
  }
23
28
  return Array.from(ids);
24
29
  }
25
30
 
26
- export function extractToolStepsFromRawStrategy(rawStrategy: RawStrategyJson): ToolStepJson[] {
27
- const toolSteps: ToolStepJson[] = [];
28
- const seen = new Set<ToolStepIdJson>();
29
-
30
- const addToolStep = (toolStep: ToolStepJson) => {
31
- if (seen.has(toolStep.id)) {
32
- throw new Error(`Duplicate tool step id encountered: ${toolStep.id}`);
33
- }
34
- seen.add(toolStep.id);
35
- toolSteps.push(toolStep);
36
- };
37
-
38
- const visitStep = (step: StepJson) => {
39
- switch (step.stepKind) {
40
- case CONSTANTS.Enums.StepKind.tool: {
41
- addToolStep(step);
42
- return;
43
- }
44
- case CONSTANTS.Enums.StepKind.branch: {
45
- for (const conditional of step.cases) {
46
- addToolStep(conditional.when);
47
- addToolStep(conditional.what);
48
- }
49
- return;
50
- }
51
- case CONSTANTS.Enums.StepKind.while:
52
- case CONSTANTS.Enums.StepKind.for: {
53
- addToolStep(step.case.when);
54
- addToolStep(step.case.what);
55
- return;
56
- }
57
- default: {
58
- const _exhaustive: never = step;
59
- return _exhaustive;
60
- }
61
- }
62
- };
63
-
64
- for (const step of rawStrategy.steps) {
65
- visitStep(step);
66
- }
67
-
68
- return toolSteps;
31
+ export function extractToolStepsFromRawStrategy(rawStrategy: Strategy): ToolStep[] {
32
+ return extractToolStepsFromStrategy(rawStrategy);
69
33
  }
70
34
 
71
35
 
72
- export function extractRoleMapFromRawStrategy(rawStrategy: RawStrategyJson, toolMap: Map<ToolIdJson, ToolJson>): Map<ResourceRoleIdJson, ResourceRoleJson> {
73
- const roleMap = new Map<ResourceRoleIdJson, ResourceRoleJson>();
36
+ export function extractRoleMapFromRawStrategy(rawStrategy: Strategy, toolMap: Map<ToolId, Tool>): Map<RoleName, ToolRoleDescriptor> {
37
+ const roleMap = new Map<RoleName, ToolRoleDescriptor>();
74
38
  const toolIds = extractToolIdsFromRawStrategy(rawStrategy);
75
39
 
76
40
  for (const toolId of toolIds) {
77
41
  const tool = toolMap.get(toolId);
78
42
  if (!tool) continue;
79
43
 
80
- for (const [rid, role] of Object.entries(tool.roleSpec.inputRoleValueById)) {
81
- roleMap.set(rid as ResourceRoleIdJson, {
82
- id: rid as ResourceRoleIdJson,
83
- ...role,
84
- });
44
+ for (const entry of getInputRoleEntries(tool)) {
45
+ roleMap.set(entry[0], toToolRoleDescriptor(entry));
85
46
  }
86
- for (const [rid, role] of Object.entries(tool.roleSpec.outputRoleValueById)) {
87
- roleMap.set(rid as ResourceRoleIdJson, {
88
- id: rid as ResourceRoleIdJson,
89
- ...role,
90
- });
47
+ for (const entry of getOutputRoleEntries(tool)) {
48
+ roleMap.set(entry[0], toToolRoleDescriptor(entry));
91
49
  }
92
50
  }
93
51
 
94
52
  return roleMap;
95
53
  }
96
54
 
97
- export function extractSingleNonErrorOutputTypeId(tool: ToolJson): ResourceTypeIdJson | null {
55
+ export function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeId | null {
98
56
  const errorRoleId = CONSTANTS.Cosmos.ROLE_ErrorOutput;
99
- const outputEntries = Object.entries(tool.roleSpec.outputRoleValueById).filter(([roleId]) => roleId !== errorRoleId);
57
+ const outputEntries = getOutputRoleEntries(tool).filter(([roleName]) => roleName !== errorRoleId);
100
58
  if (!outputEntries || !outputEntries[0] || outputEntries.length !== 1) {
101
59
  throw new Error(`Tool ${tool.id} must have exactly one non-error output role to be branchable/loopable`);
102
60
  } // ATTENTION_PUREIFY
103
- const outputRole = outputEntries[0][1];
61
+ const outputRole = outputEntries[0][1] as RoleValue;
104
62
  return outputRole.resourceTypeId;
105
63
  }
106
64
 
107
65
 
108
66
 
109
- export function extractResourceMapForType<TResource extends ResourceJson = ResourceJson>(
110
- resourcesByType: Partial<Record<ResourceTypeIdJson, TResource[]>>,
111
- resourceTypeId: ResourceTypeIdJson
112
- ): Map<ResourceIdJson, TResource> {
67
+ export function extractResourceMapForType<TResource extends Resource = Resource>(
68
+ resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>,
69
+ resourceTypeId: ResourceTypeId
70
+ ): Map<ResourceId, TResource> {
113
71
  const resources = resourcesByType[resourceTypeId] ?? [];
114
- const map = new Map<ResourceIdJson, TResource>();
72
+ const map = new Map<ResourceId, TResource>();
115
73
 
116
74
  for (const resource of resources) {
117
75
  map.set(resource.id, resource);
@@ -124,10 +82,10 @@ export function extractResourceMapForType<TResource extends ResourceJson = Resou
124
82
  export function extractIdKeyedNucleusMapForType<
125
83
  TKey extends string = string,
126
84
  TNucleus extends { id: string | number | boolean } = { id: string | number | boolean },
127
- TResource extends ResourceJson = ResourceJson
85
+ TResource extends Resource = Resource
128
86
  >(
129
- resourcesByType: Partial<Record<ResourceTypeIdJson, TResource[]>>,
130
- resourceTypeId: ResourceTypeIdJson,
87
+ resourcesByType: Partial<Record<ResourceTypeId, TResource[]>>,
88
+ resourceTypeId: ResourceTypeId,
131
89
  ): Map<TKey, TNucleus> {
132
90
  const resourceMap = extractResourceMapForType<TResource>(resourcesByType, resourceTypeId);
133
91
  const out = new Map<TKey, TNucleus>();
@@ -1,17 +1,23 @@
1
1
  import type {
2
- BranchStepJson,
3
- ForStepJson,
4
- ToolStepIdJson,
5
- ToolStepJson,
6
- ResourceRoleIdJson,
7
- StepJson,
8
- StrategyStateJson,
9
- WhileStepJson,
2
+ BranchStep,
3
+ ForStep,
4
+ ToolStep,
5
+ Step,
6
+ WhileStep,
10
7
  } from '@toolproof-core/schema';
11
8
  import { CONSTANTS } from '../artifacts/artifacts.js';
9
+ import type { StrategyState } from './strategyState.js';
12
10
 
11
+ type ToolStepId = string;
12
+ type ToolStepWithId = ToolStep & { id: ToolStepId };
13
13
 
14
- export function getIndependentThreads(steps: StepJson[], strategyState: StrategyStateJson): StepJson[][] {
14
+ function getToolStepId(toolStep: ToolStep | undefined): ToolStepId | undefined {
15
+ const maybeId = (toolStep as Partial<ToolStepWithId> | undefined)?.id;
16
+ return typeof maybeId === 'string' ? maybeId : undefined;
17
+ }
18
+
19
+
20
+ export function getIndependentThreads(steps: Step[], strategyState: StrategyState): Step[][] {
15
21
  type OwnerIndex = number;
16
22
 
17
23
  const getOwnerLabel = (ownerIndex: OwnerIndex) => {
@@ -20,49 +26,50 @@ export function getIndependentThreads(steps: StepJson[], strategyState: Strategy
20
26
  };
21
27
 
22
28
  // Map each toolStep.id (including macro-nested tool steps) to owning top-level step index.
23
- const toolStepIdToOwner = new Map<ToolStepIdJson, OwnerIndex>();
24
- const ownerToToolStepIds = new Map<OwnerIndex, ToolStepIdJson[]>();
25
- const toolStepById = new Map<ToolStepIdJson, ToolStepJson>();
29
+ const toolStepIdToOwner = new Map<ToolStepId, OwnerIndex>();
30
+ const ownerToToolStepIds = new Map<OwnerIndex, ToolStepId[]>();
31
+ const toolStepById = new Map<ToolStepId, ToolStepWithId>();
26
32
 
27
- const addToolStep = (toolStep: ToolStepJson | undefined, ownerIndex: OwnerIndex) => {
28
- if (!toolStep?.id) return;
33
+ const addToolStep = (toolStep: ToolStep | undefined, ownerIndex: OwnerIndex) => {
34
+ const toolStepId = getToolStepId(toolStep);
35
+ if (!toolStepId || !toolStep) return;
29
36
 
30
- const existingOwner = toolStepIdToOwner.get(toolStep.id);
37
+ const existingOwner = toolStepIdToOwner.get(toolStepId);
31
38
  if (existingOwner !== undefined) {
32
39
  throw new Error(
33
- `Duplicate toolStep.id '${toolStep.id}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`
40
+ `Duplicate toolStep.id '${toolStepId}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`
34
41
  );
35
42
  }
36
43
 
37
- toolStepIdToOwner.set(toolStep.id, ownerIndex);
38
- toolStepById.set(toolStep.id, toolStep);
44
+ toolStepIdToOwner.set(toolStepId, ownerIndex);
45
+ toolStepById.set(toolStepId, toolStep as ToolStepWithId);
39
46
  const bucket = ownerToToolStepIds.get(ownerIndex) ?? [];
40
- bucket.push(toolStep.id);
47
+ bucket.push(toolStepId);
41
48
  ownerToToolStepIds.set(ownerIndex, bucket);
42
49
  };
43
50
 
44
51
  steps.forEach((step, ownerIndex) => {
45
52
  if (step.stepKind === CONSTANTS.Enums.StepKind.tool) {
46
- addToolStep(step as ToolStepJson, ownerIndex);
53
+ addToolStep(step as ToolStep, ownerIndex);
47
54
  return;
48
55
  }
49
56
 
50
57
  if (step.stepKind === CONSTANTS.Enums.StepKind.for) {
51
- const loop = step as ForStepJson;
58
+ const loop = step as ForStep;
52
59
  addToolStep(loop.case?.what, ownerIndex);
53
60
  addToolStep(loop.case?.when, ownerIndex);
54
61
  return;
55
62
  }
56
63
 
57
64
  if (step.stepKind === CONSTANTS.Enums.StepKind.while) {
58
- const loop = step as WhileStepJson;
65
+ const loop = step as WhileStep;
59
66
  addToolStep(loop.case?.what, ownerIndex);
60
67
  addToolStep(loop.case?.when, ownerIndex);
61
68
  return;
62
69
  }
63
70
 
64
71
  if (step.stepKind === CONSTANTS.Enums.StepKind.branch) {
65
- const branch = step as BranchStepJson;
72
+ const branch = step as BranchStep;
66
73
  for (const caseItem of branch.cases ?? []) {
67
74
  addToolStep(caseItem?.what, ownerIndex);
68
75
  addToolStep(caseItem?.when, ownerIndex);
@@ -94,20 +101,22 @@ export function getIndependentThreads(steps: StepJson[], strategyState: Strategy
94
101
  const bucket = strategyState?.[toolStepId] ?? ({} as any);
95
102
 
96
103
  for (const inputRoleId of inputBindings) {
97
- const entry = bucket?.[inputRoleId as ResourceRoleIdJson] as any;
98
- if (!entry || entry.resourceShellKind !== 'internalInputPotential') continue;
104
+ const entry = bucket?.[inputRoleId] as any;
105
+ if (!entry || entry.strategyStateInputKind !== 'referenceInput') continue;
99
106
 
100
- const creatorToolStepId = entry?.creationContext?.toolStepId as ToolStepIdJson | undefined;
107
+ const creatorToolStepId = typeof entry?.toolStepPath === 'string'
108
+ ? entry.toolStepPath as ToolStepId
109
+ : undefined;
101
110
  if (!creatorToolStepId) {
102
111
  throw new Error(
103
- `Unresolvable internalInputPotential in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): missing creationContext.toolStepId for role '${inputRoleId}'`
112
+ `Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): missing toolStepPath for role '${inputRoleId}'`
104
113
  );
105
114
  }
106
115
 
107
116
  const producerOwner = toolStepIdToOwner.get(creatorToolStepId);
108
117
  if (producerOwner === undefined) {
109
118
  throw new Error(
110
- `Unresolvable internalInputPotential in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): creator toolStepId '${creatorToolStepId}' not found in strategy steps`
119
+ `Unresolvable referenceInput in toolStep '${toolStepId}' (${getOwnerLabel(ownerIndex)}): creator toolStepPath '${creatorToolStepId}' not found in strategy steps`
111
120
  );
112
121
  }
113
122
 
@@ -1,65 +1,51 @@
1
1
  import type {
2
- CreationContextJson,
3
- ExternalInputPotentialShellJson,
4
- InternalInputPotentialShellJson,
5
- OutputPotentialShellJson,
6
- ResourceJson,
7
- ResourcePotentialJson,
8
- StrategyStateJson,
2
+ CreationContext,
3
+ ExternalInput,
4
+ ReferenceInput,
5
+ StrategyStateInputEntry,
6
+ ValueInput,
9
7
  } from '@toolproof-core/schema';
10
- import { CONSTANTS } from '../artifacts/artifacts.js';
8
+ import { getStrategyStateEntry, type StrategyState } from './strategyState.js';
11
9
 
12
10
  export type ResolveResult =
13
- | { status: typeof CONSTANTS.Enums.ResourceShellKind.materialized; entry: ResourceJson; path: CreationContextJson[] }
14
- | { status: typeof CONSTANTS.Enums.ResourceShellKind.externalInputPotential; entry: ExternalInputPotentialShellJson; path: CreationContextJson[] }
15
- | { status: typeof CONSTANTS.Enums.ResourceShellKind.outputPotential; entry: OutputPotentialShellJson; path: CreationContextJson[] }
16
- | { status: 'unresolved'; reason: 'not-found' | 'cycle' | 'depth-exceeded'; path: CreationContextJson[] };
11
+ | { status: 'valueInput'; entry: ValueInput; path: CreationContext[] }
12
+ | { status: 'externalInput'; entry: ExternalInput; path: CreationContext[] }
13
+ | { status: 'unresolved'; reason: 'not-found' | 'cycle' | 'depth-exceeded'; path: CreationContext[] };
17
14
 
18
15
  export function resolveResourceChain(
19
- strategyState: StrategyStateJson,
20
- start: CreationContextJson,
16
+ strategyState: StrategyState,
17
+ start: CreationContext,
21
18
  opts?: { maxDepth?: number }
22
19
  ): ResolveResult {
23
20
  const maxDepth = opts?.maxDepth ?? 50;
24
21
  const visited = new Set<string>();
25
- const path: CreationContextJson[] = [];
26
- let current: CreationContextJson = start;
22
+ const path: CreationContext[] = [];
23
+ let current: CreationContext = start;
27
24
 
28
25
  for (let depth = 0; depth <= maxDepth; depth++) {
29
26
  path.push(current);
30
- const visitKey = `${current.toolStepId}::${current.resourceRoleId}`;
27
+ const visitKey = `${current.toolStepPath}::${current.roleName}`;
31
28
  if (visited.has(visitKey)) {
32
29
  return { status: 'unresolved', reason: 'cycle', path };
33
30
  }
34
31
  visited.add(visitKey);
35
32
 
36
- const bucket = strategyState[current.toolStepId];
37
- if (!bucket) return { status: 'unresolved', reason: 'not-found', path };
38
- const entry = bucket[current.resourceRoleId] as (
39
- | ResourceJson
40
- | ResourcePotentialJson
41
- | undefined
42
- );
33
+ const entry = getStrategyStateEntry(strategyState, current) as StrategyStateInputEntry | undefined;
43
34
  if (!entry) return { status: 'unresolved', reason: 'not-found', path };
44
35
 
45
- if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.materialized) {
46
- return { status: CONSTANTS.Enums.ResourceShellKind.materialized, entry: entry as ResourceJson, path };
36
+ if (entry.strategyStateInputKind === 'valueInput') {
37
+ return { status: 'valueInput', entry: entry, path };
47
38
  }
48
- if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.externalInputPotential) {
49
- return { status: CONSTANTS.Enums.ResourceShellKind.externalInputPotential, entry: entry as ExternalInputPotentialShellJson, path };
50
- }
51
- if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.outputPotential) {
52
- return { status: CONSTANTS.Enums.ResourceShellKind.outputPotential, entry: entry as OutputPotentialShellJson, path };
39
+ if (entry.strategyStateInputKind === 'externalInput') {
40
+ return { status: 'externalInput', entry: entry, path };
53
41
  }
54
42
 
55
- // internalInputPotential: follow ref backwards
56
- if (entry.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.internalInputPotential) {
57
- current = (entry as InternalInputPotentialShellJson).creationContext;
58
- continue;
59
- }
43
+ current = {
44
+ toolStepPath: (entry as ReferenceInput).toolStepPath,
45
+ roleName: (entry as ReferenceInput).roleName,
46
+ };
47
+ continue;
60
48
 
61
- // Unknown case
62
- return { status: 'unresolved', reason: 'not-found', path };
63
49
  }
64
50
 
65
51
  return { status: 'unresolved', reason: 'depth-exceeded', path };
@@ -0,0 +1,80 @@
1
+ import type {
2
+ RoleName,
3
+ RoleValue,
4
+ Strategy,
5
+ Step,
6
+ Tool,
7
+ ToolStep,
8
+ } from '@toolproof-core/schema';
9
+
10
+ export interface ToolRoleDescriptor extends RoleValue {
11
+ id: RoleName;
12
+ name: string;
13
+ }
14
+
15
+ export function getInputRoleEntries(tool: Tool): Array<[RoleName, RoleValue]> {
16
+ return Object.entries(tool.roleSpec.inputRoleValueByName) as Array<[RoleName, RoleValue]>;
17
+ }
18
+
19
+ export function getOutputRoleEntries(tool: Tool): Array<[RoleName, RoleValue]> {
20
+ return Object.entries(tool.roleSpec.outputRoleValueByName) as Array<[RoleName, RoleValue]>;
21
+ }
22
+
23
+ export function getInputRoleNames(tool: Tool): RoleName[] {
24
+ return getInputRoleEntries(tool).map(([roleName]) => roleName);
25
+ }
26
+
27
+ export function getOutputRoleNames(tool: Tool): RoleName[] {
28
+ return getOutputRoleEntries(tool).map(([roleName]) => roleName);
29
+ }
30
+
31
+ export function toToolRoleDescriptor([roleName, roleValue]: [RoleName, RoleValue]): ToolRoleDescriptor {
32
+ return {
33
+ id: roleName,
34
+ name: roleName,
35
+ ...roleValue,
36
+ };
37
+ }
38
+
39
+ export function extractToolStepsFromStrategy(strategy: Strategy): ToolStep[] {
40
+ const toolSteps: ToolStep[] = [];
41
+
42
+ const visitStep = (step: Step) => {
43
+ switch (step.stepKind) {
44
+ case 'tool': {
45
+ toolSteps.push(step);
46
+ return;
47
+ }
48
+ case 'branch': {
49
+ for (const conditional of step.cases) {
50
+ toolSteps.push(conditional.when, conditional.what);
51
+ }
52
+ return;
53
+ }
54
+ case 'while':
55
+ case 'for': {
56
+ toolSteps.push(step.case.when, step.case.what);
57
+ return;
58
+ }
59
+ default: {
60
+ const _exhaustive: never = step;
61
+ return _exhaustive;
62
+ }
63
+ }
64
+ };
65
+
66
+ if ('steps' in strategy) {
67
+ for (const step of strategy.steps) {
68
+ visitStep(step);
69
+ }
70
+ return toolSteps;
71
+ }
72
+
73
+ for (const threadSteps of strategy.stepsByThreadIndex) {
74
+ for (const step of threadSteps) {
75
+ visitStep(step);
76
+ }
77
+ }
78
+
79
+ return toolSteps;
80
+ }