@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.
- package/dist/artifacts/artifacts.d.ts +51 -51
- package/dist/artifacts/artifacts.d.ts.map +1 -1
- package/dist/integrations/firebase/createRunnableStrategy.d.ts +2 -2
- package/dist/integrations/firebase/createRunnableStrategy.d.ts.map +1 -1
- package/dist/integrations/firebase/createRunnableStrategy.js +2 -3
- package/dist/integrations/firebase/createRunnableStrategy.js.map +1 -1
- package/dist/integrations/firebase/createStep.d.ts +8 -8
- package/dist/integrations/firebase/createStep.d.ts.map +1 -1
- package/dist/integrations/firebase/createStep.js.map +1 -1
- package/dist/integrations/firebase/firebaseAdminHelpers.d.ts +8 -4
- package/dist/integrations/firebase/firebaseAdminHelpers.d.ts.map +1 -1
- package/dist/integrations/firebase/firebaseAdminHelpers.js.map +1 -1
- package/dist/types/types.d.ts +5 -3
- package/dist/types/types.d.ts.map +1 -1
- package/dist/utils/bindInputRoleToResource.d.ts +5 -4
- package/dist/utils/bindInputRoleToResource.d.ts.map +1 -1
- package/dist/utils/bindInputRoleToResource.js +15 -65
- package/dist/utils/bindInputRoleToResource.js.map +1 -1
- package/dist/utils/creation/resourceCreation.d.ts +20 -8
- package/dist/utils/creation/resourceCreation.d.ts.map +1 -1
- package/dist/utils/creation/resourceCreation.js +16 -12
- package/dist/utils/creation/resourceCreation.js.map +1 -1
- package/dist/utils/creation/runnableStrategyCreation.d.ts +3 -3
- package/dist/utils/creation/runnableStrategyCreation.d.ts.map +1 -1
- package/dist/utils/creation/runnableStrategyCreation.js +8 -14
- package/dist/utils/creation/runnableStrategyCreation.js.map +1 -1
- package/dist/utils/creation/stepCreation.d.ts +41 -24
- package/dist/utils/creation/stepCreation.d.ts.map +1 -1
- package/dist/utils/creation/stepCreation.js +3 -2
- package/dist/utils/creation/stepCreation.js.map +1 -1
- package/dist/utils/extractData.d.ts +8 -7
- package/dist/utils/extractData.d.ts.map +1 -1
- package/dist/utils/extractData.js +7 -49
- package/dist/utils/extractData.js.map +1 -1
- package/dist/utils/parallelizeSteps.d.ts +3 -2
- package/dist/utils/parallelizeSteps.d.ts.map +1 -1
- package/dist/utils/parallelizeSteps.js +17 -10
- package/dist/utils/parallelizeSteps.js.map +1 -1
- package/dist/utils/resolveResourceChain.d.ts +10 -14
- package/dist/utils/resolveResourceChain.d.ts.map +1 -1
- package/dist/utils/resolveResourceChain.js +12 -20
- package/dist/utils/resolveResourceChain.js.map +1 -1
- package/dist/utils/roleSpec.d.ts +12 -0
- package/dist/utils/roleSpec.d.ts.map +1 -0
- package/dist/utils/roleSpec.js +58 -0
- package/dist/utils/roleSpec.js.map +1 -0
- package/dist/utils/strategyState.d.ts +7 -0
- package/dist/utils/strategyState.d.ts.map +1 -0
- package/dist/utils/strategyState.js +29 -0
- package/dist/utils/strategyState.js.map +1 -0
- package/package.json +3 -3
- package/src/integrations/firebase/createRunnableStrategy.ts +5 -7
- package/src/integrations/firebase/createStep.ts +13 -13
- package/src/integrations/firebase/firebaseAdminHelpers.ts +10 -9
- package/src/types/types.ts +5 -5
- package/src/utils/bindInputRoleToResource.ts +36 -93
- package/src/utils/creation/resourceCreation.ts +54 -36
- package/src/utils/creation/runnableStrategyCreation.ts +16 -30
- package/src/utils/creation/stepCreation.ts +54 -48
- package/src/utils/extractData.ts +37 -79
- package/src/utils/parallelizeSteps.ts +37 -28
- package/src/utils/resolveResourceChain.ts +24 -38
- package/src/utils/roleSpec.ts +80 -0
- package/src/utils/strategyState.ts +56 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,35 +1,41 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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:
|
|
20
|
-
whatId:
|
|
21
|
-
whenId:
|
|
25
|
+
stepId: ForStepId;
|
|
26
|
+
whatId: ToolStepId;
|
|
27
|
+
whenId: ToolStepId;
|
|
22
28
|
}
|
|
23
29
|
| {
|
|
24
30
|
stepKind: typeof CONSTANTS.Enums.StepKind.while;
|
|
25
|
-
stepId:
|
|
26
|
-
whatId:
|
|
27
|
-
whenId:
|
|
31
|
+
stepId: WhileStepId;
|
|
32
|
+
whatId: ToolStepId;
|
|
33
|
+
whenId: ToolStepId;
|
|
28
34
|
};
|
|
29
35
|
|
|
30
36
|
export type BranchCaseBuildIdentities = {
|
|
31
|
-
whatId:
|
|
32
|
-
whenId:
|
|
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:
|
|
47
|
+
function getRoleBindingSpec(tool: Tool): ToolStep['roleBindingSpec'] {
|
|
42
48
|
return {
|
|
43
|
-
inputBindings:
|
|
44
|
-
outputBindings:
|
|
49
|
+
inputBindings: getInputRoleNames(tool),
|
|
50
|
+
outputBindings: getOutputRoleNames(tool),
|
|
45
51
|
};
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export function buildToolStepFromTool(
|
|
49
|
-
tool:
|
|
50
|
-
id:
|
|
51
|
-
):
|
|
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:
|
|
62
|
-
whenTool:
|
|
67
|
+
whatTool: Tool,
|
|
68
|
+
whenTool: Tool,
|
|
63
69
|
identities: LoopStepBuildIdentities,
|
|
64
|
-
):
|
|
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:
|
|
85
|
-
branchId:
|
|
90
|
+
cases: Array<{ whatTool: Tool; whenTool: Tool }>,
|
|
91
|
+
branchId: BranchStepId,
|
|
86
92
|
caseIdentities: BranchCaseBuildIdentities[],
|
|
87
|
-
):
|
|
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
|
|
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:
|
|
120
|
+
forStep: ForStep,
|
|
115
121
|
identities: {
|
|
116
|
-
stepId:
|
|
117
|
-
whatId:
|
|
118
|
-
whenId:
|
|
122
|
+
stepId: ForStepId;
|
|
123
|
+
whatId: ToolStepId;
|
|
124
|
+
whenId: ToolStepId;
|
|
119
125
|
},
|
|
120
|
-
):
|
|
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:
|
|
144
|
+
whileStep: WhileStep,
|
|
139
145
|
identities: {
|
|
140
|
-
stepId:
|
|
141
|
-
whatId:
|
|
142
|
-
whenId:
|
|
146
|
+
stepId: WhileStepId;
|
|
147
|
+
whatId: ToolStepId;
|
|
148
|
+
whenId: ToolStepId;
|
|
143
149
|
},
|
|
144
|
-
):
|
|
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
|
}
|
package/src/utils/extractData.ts
CHANGED
|
@@ -1,117 +1,75 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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:
|
|
22
|
+
export function extractToolIdsFromRawStrategy(rawStrategy: Strategy): ToolId[] {
|
|
18
23
|
const toolSteps = extractToolStepsFromRawStrategy(rawStrategy);
|
|
19
|
-
const ids = new Set<
|
|
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:
|
|
27
|
-
|
|
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:
|
|
73
|
-
const roleMap = new Map<
|
|
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
|
|
81
|
-
roleMap.set(
|
|
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
|
|
87
|
-
roleMap.set(
|
|
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:
|
|
55
|
+
export function extractSingleNonErrorOutputTypeId(tool: Tool): ResourceTypeId | null {
|
|
98
56
|
const errorRoleId = CONSTANTS.Cosmos.ROLE_ErrorOutput;
|
|
99
|
-
const outputEntries =
|
|
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
|
|
110
|
-
resourcesByType: Partial<Record<
|
|
111
|
-
resourceTypeId:
|
|
112
|
-
): Map<
|
|
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<
|
|
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
|
|
85
|
+
TResource extends Resource = Resource
|
|
128
86
|
>(
|
|
129
|
-
resourcesByType: Partial<Record<
|
|
130
|
-
resourceTypeId:
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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<
|
|
24
|
-
const ownerToToolStepIds = new Map<OwnerIndex,
|
|
25
|
-
const toolStepById = new Map<
|
|
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:
|
|
28
|
-
|
|
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(
|
|
37
|
+
const existingOwner = toolStepIdToOwner.get(toolStepId);
|
|
31
38
|
if (existingOwner !== undefined) {
|
|
32
39
|
throw new Error(
|
|
33
|
-
`Duplicate toolStep.id '${
|
|
40
|
+
`Duplicate toolStep.id '${toolStepId}' found in ${getOwnerLabel(ownerIndex)} and ${getOwnerLabel(existingOwner)}`
|
|
34
41
|
);
|
|
35
42
|
}
|
|
36
43
|
|
|
37
|
-
toolStepIdToOwner.set(
|
|
38
|
-
toolStepById.set(
|
|
44
|
+
toolStepIdToOwner.set(toolStepId, ownerIndex);
|
|
45
|
+
toolStepById.set(toolStepId, toolStep as ToolStepWithId);
|
|
39
46
|
const bucket = ownerToToolStepIds.get(ownerIndex) ?? [];
|
|
40
|
-
bucket.push(
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
98
|
-
if (!entry || entry.
|
|
104
|
+
const entry = bucket?.[inputRoleId] as any;
|
|
105
|
+
if (!entry || entry.strategyStateInputKind !== 'referenceInput') continue;
|
|
99
106
|
|
|
100
|
-
const creatorToolStepId = entry?.
|
|
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
|
|
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
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
ResourcePotentialJson,
|
|
8
|
-
StrategyStateJson,
|
|
2
|
+
CreationContext,
|
|
3
|
+
ExternalInput,
|
|
4
|
+
ReferenceInput,
|
|
5
|
+
StrategyStateInputEntry,
|
|
6
|
+
ValueInput,
|
|
9
7
|
} from '@toolproof-core/schema';
|
|
10
|
-
import {
|
|
8
|
+
import { getStrategyStateEntry, type StrategyState } from './strategyState.js';
|
|
11
9
|
|
|
12
10
|
export type ResolveResult =
|
|
13
|
-
| { status:
|
|
14
|
-
| { status:
|
|
15
|
-
| { status:
|
|
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:
|
|
20
|
-
start:
|
|
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:
|
|
26
|
-
let current:
|
|
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.
|
|
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
|
|
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.
|
|
46
|
-
return { status:
|
|
36
|
+
if (entry.strategyStateInputKind === 'valueInput') {
|
|
37
|
+
return { status: 'valueInput', entry: entry, path };
|
|
47
38
|
}
|
|
48
|
-
if (entry.
|
|
49
|
-
return { status:
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
+
}
|