@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
@@ -0,0 +1,7 @@
1
+ import type { CreationContext, Strategy, StrategyStateInputEntry, StrategyStateInputEntryByRoleName } from '@toolproof-core/schema';
2
+ export type StrategyState = Strategy['strategyState'];
3
+ export declare function getStrategyStateBucket(strategyState: StrategyState, toolStepPath: CreationContext['toolStepPath']): StrategyStateInputEntryByRoleName | undefined;
4
+ export declare function getStrategyStateEntry(strategyState: StrategyState, context: CreationContext): StrategyStateInputEntry | undefined;
5
+ export declare function setStrategyStateEntry(strategyState: StrategyState, context: CreationContext, entry: StrategyStateInputEntry): StrategyState;
6
+ export declare function clearStrategyStateEntry(strategyState: StrategyState, context: CreationContext): StrategyState;
7
+ //# sourceMappingURL=strategyState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategyState.d.ts","sourceRoot":"","sources":["../../src/utils/strategyState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,eAAe,EACf,QAAQ,EACR,uBAAuB,EACvB,iCAAiC,EACpC,MAAM,wBAAwB,CAAC;AAEhC,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,eAAe,CAAC,CAAC;AAEtD,wBAAgB,sBAAsB,CAClC,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,eAAe,CAAC,cAAc,CAAC,GAC9C,iCAAiC,GAAG,SAAS,CAE/C;AAED,wBAAgB,qBAAqB,CACjC,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,eAAe,GACzB,uBAAuB,GAAG,SAAS,CAErC;AAED,wBAAgB,qBAAqB,CACjC,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,eAAe,EACxB,KAAK,EAAE,uBAAuB,GAC/B,aAAa,CAUf;AAED,wBAAgB,uBAAuB,CACnC,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,eAAe,GACzB,aAAa,CAaf"}
@@ -0,0 +1,29 @@
1
+ export function getStrategyStateBucket(strategyState, toolStepPath) {
2
+ return strategyState[toolStepPath];
3
+ }
4
+ export function getStrategyStateEntry(strategyState, context) {
5
+ return getStrategyStateBucket(strategyState, context.toolStepPath)?.[context.roleName];
6
+ }
7
+ export function setStrategyStateEntry(strategyState, context, entry) {
8
+ const bucket = getStrategyStateBucket(strategyState, context.toolStepPath) ?? {};
9
+ return {
10
+ ...strategyState,
11
+ [context.toolStepPath]: {
12
+ ...bucket,
13
+ [context.roleName]: entry,
14
+ },
15
+ };
16
+ }
17
+ export function clearStrategyStateEntry(strategyState, context) {
18
+ const bucket = getStrategyStateBucket(strategyState, context.toolStepPath);
19
+ if (!bucket || !(context.roleName in bucket)) {
20
+ return strategyState;
21
+ }
22
+ const nextBucket = { ...bucket };
23
+ delete nextBucket[context.roleName];
24
+ return {
25
+ ...strategyState,
26
+ [context.toolStepPath]: nextBucket,
27
+ };
28
+ }
29
+ //# sourceMappingURL=strategyState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strategyState.js","sourceRoot":"","sources":["../../src/utils/strategyState.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,sBAAsB,CAClC,aAA4B,EAC5B,YAA6C;IAE7C,OAAO,aAAa,CAAC,YAAY,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,aAA4B,EAC5B,OAAwB;IAExB,OAAO,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC3F,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,aAA4B,EAC5B,OAAwB,EACxB,KAA8B;IAE9B,MAAM,MAAM,GAAG,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IAEjF,OAAO;QACH,GAAG,aAAa;QAChB,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE;YACpB,GAAG,MAAM;YACT,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,KAAK;SAC5B;KACJ,CAAC;AACN,CAAC;AAED,MAAM,UAAU,uBAAuB,CACnC,aAA4B,EAC5B,OAAwB;IAExB,MAAM,MAAM,GAAG,sBAAsB,CAAC,aAAa,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;IAC3E,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,IAAI,MAAM,CAAC,EAAE,CAAC;QAC3C,OAAO,aAAa,CAAC;IACzB,CAAC;IAED,MAAM,UAAU,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IACjC,OAAO,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpC,OAAO;QACH,GAAG,aAAa;QAChB,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,UAAU;KACrC,CAAC;AACN,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolproof-core/lib",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "keywords": [],
@@ -52,11 +52,11 @@
52
52
  "@types/node": "^22.0.0"
53
53
  },
54
54
  "dependencies": {
55
- "@toolproof-core/schema": "^1.0.42",
55
+ "@toolproof-core/schema": "^1.0.46",
56
56
  "firebase-admin": "^13.7.0"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "echo \"Error: no test specified\" && exit 1",
60
- "build": "tsc -b"
60
+ "build": "if exist dist rmdir /s /q dist && if exist tsconfig.tsbuildinfo del /q tsconfig.tsbuildinfo && tsc -b"
61
61
  }
62
62
  }
@@ -1,4 +1,4 @@
1
- import type { RawStrategyJson, RunnableStrategyJson } from '@toolproof-core/schema';
1
+ import type { Strategy } from '@toolproof-core/schema';
2
2
  import { CONSTANTS } from '../../artifacts/artifacts.js';
3
3
  import {
4
4
  buildRunnableStrategy,
@@ -6,15 +6,13 @@ import {
6
6
  } from '../../utils/creation/runnableStrategyCreation.js';
7
7
  import { getNewId } from './firebaseAdminHelpers.js';
8
8
 
9
- export function createRunnableStrategy(rawStrategy: RawStrategyJson): RunnableStrategyJson {
9
+ export function createRunnableStrategy(rawStrategy: Strategy): Strategy {
10
10
  const threadStepGroups = getRunnableStrategyThreadGroups(rawStrategy);
11
- const runnableStrategyId = getNewId(CONSTANTS.Names.RunnableStrategyId);
12
- const threadIdentities = threadStepGroups.map(() => getNewId(CONSTANTS.Names.StrategyThreadId));
11
+ const strategyId = getNewId(CONSTANTS.Names.StrategyId);
13
12
 
14
13
  return buildRunnableStrategy(
15
- rawStrategy,
16
- runnableStrategyId,
17
14
  threadStepGroups,
18
- threadIdentities,
15
+ strategyId,
16
+ rawStrategy,
19
17
  );
20
18
  }
@@ -1,9 +1,9 @@
1
1
  import type {
2
- BranchStepJson,
3
- ForStepJson,
4
- ToolJson,
5
- ToolStepJson,
6
- WhileStepJson,
2
+ BranchStep,
3
+ ForStep,
4
+ Tool,
5
+ ToolStep,
6
+ WhileStep,
7
7
  } from '@toolproof-core/schema';
8
8
  import { CONSTANTS } from '../../artifacts/artifacts.js';
9
9
  import {
@@ -15,15 +15,15 @@ import {
15
15
  } from '../../utils/creation/stepCreation.js';
16
16
  import { getNewStepId } from './firebaseAdminHelpers.js';
17
17
 
18
- export function createToolStepFromTool(tool: ToolJson): ToolStepJson {
18
+ export function createToolStepFromTool(tool: Tool): ToolStep {
19
19
  return buildToolStepFromTool(tool, getNewStepId(CONSTANTS.Enums.StepKind.tool));
20
20
  }
21
21
 
22
22
  export function createLoopStepFromToolPair(
23
- whatTool: ToolJson,
24
- whenTool: ToolJson,
23
+ whatTool: Tool,
24
+ whenTool: Tool,
25
25
  stepKind: typeof CONSTANTS.Enums.StepKind.for | typeof CONSTANTS.Enums.StepKind.while,
26
- ): ForStepJson | WhileStepJson {
26
+ ): ForStep | WhileStep {
27
27
  if (stepKind === CONSTANTS.Enums.StepKind.for) {
28
28
  return buildLoopStepFromToolPair(whatTool, whenTool, {
29
29
  stepKind,
@@ -42,8 +42,8 @@ export function createLoopStepFromToolPair(
42
42
  }
43
43
 
44
44
  export function createBranchStepFromToolPairs(
45
- cases: Array<{ whatTool: ToolJson; whenTool: ToolJson }>,
46
- ): BranchStepJson {
45
+ cases: Array<{ whatTool: Tool; whenTool: Tool }>,
46
+ ): BranchStep {
47
47
  return buildBranchStepFromToolPairs(
48
48
  cases,
49
49
  getNewStepId(CONSTANTS.Enums.StepKind.branch),
@@ -54,7 +54,7 @@ export function createBranchStepFromToolPairs(
54
54
  );
55
55
  }
56
56
 
57
- export function cloneForStep(forStep: ForStepJson): ForStepJson {
57
+ export function cloneForStep(forStep: ForStep): ForStep {
58
58
  return cloneForStepWithIdentities(forStep, {
59
59
  stepId: getNewStepId(CONSTANTS.Enums.StepKind.for),
60
60
  whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
@@ -62,7 +62,7 @@ export function cloneForStep(forStep: ForStepJson): ForStepJson {
62
62
  });
63
63
  }
64
64
 
65
- export function cloneWhileStep(whileStep: WhileStepJson): WhileStepJson {
65
+ export function cloneWhileStep(whileStep: WhileStep): WhileStep {
66
66
  return cloneWhileStepWithIdentities(whileStep, {
67
67
  stepId: getNewStepId(CONSTANTS.Enums.StepKind.while),
68
68
  whatId: getNewStepId(CONSTANTS.Enums.StepKind.tool),
@@ -1,17 +1,18 @@
1
1
  import type {
2
- ResourceJson,
3
- ResourceTypeIdJson,
4
- StepKindJson,
2
+ Resource,
3
+ ResourceTypeId,
4
+ StepKind,
5
5
  } from '@toolproof-core/schema';
6
6
  import type {
7
7
  IdName,
8
8
  IdStringByIdName,
9
9
  ResourcesByType,
10
- StepIdStringByStepKind,
11
10
  } from '../../types/types.js';
12
11
  import { CONSTANTS, MAPPINGS } from '../../artifacts/artifacts.js';
13
12
  import { dbAdmin, storageAdmin } from './firebaseAdminInit.js';
14
13
 
14
+ type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
15
+
15
16
  function capitalizeFirst(value: string): string {
16
17
  if (!value) {
17
18
  return value;
@@ -31,7 +32,7 @@ export function getNewId<K extends IdName>(idName: K): IdStringByIdName<K> {
31
32
  return (prefix + docRef.id) as IdStringByIdName<K>;
32
33
  }
33
34
 
34
- export function getNewStepId<K extends StepKindJson>(stepKind: K): StepIdStringByStepKind<K> {
35
+ export function getNewStepId<K extends StepKind>(stepKind: K): StepIdStringByStepKind<K> {
35
36
  const idName = `${capitalizeFirst(stepKind)}StepId` as IdName;
36
37
 
37
38
  if (!(idName in MAPPINGS.IdNameToIdPrefix)) {
@@ -42,13 +43,13 @@ export function getNewStepId<K extends StepKindJson>(stepKind: K): StepIdStringB
42
43
  }
43
44
 
44
45
  export async function listResources(
45
- resourceTypeIds: ResourceTypeIdJson[],
46
+ resourceTypeIds: ResourceTypeId[],
46
47
  ): Promise<ResourcesByType> {
47
48
  const bucketResources = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_resources);
48
49
  const bucketStrategies = storageAdmin.bucket(CONSTANTS.Persistence.Buckets.tp_strategies);
49
50
 
50
51
  async function fetchFilesUnder(
51
- resourceTypeId: ResourceTypeIdJson,
52
+ resourceTypeId: ResourceTypeId,
52
53
  ): Promise<Array<{ data: unknown; meta: any; name: string }>> {
53
54
  const bucket = resourceTypeId === CONSTANTS.Cosmos.TYPE_RunnableStrategy
54
55
  ? bucketStrategies
@@ -81,7 +82,7 @@ export async function listResources(
81
82
  const entries = await Promise.all(
82
83
  resourceTypeIds.map(async (resourceTypeId) => {
83
84
  const rows = await fetchFilesUnder(resourceTypeId);
84
- const items: ResourceJson[] = rows.map(({ data, meta, name }) => {
85
+ const items: Resource[] = rows.map(({ data, meta, name }) => {
85
86
  const flat = meta?.metadata ?? {};
86
87
  const root: any = {};
87
88
 
@@ -184,7 +185,7 @@ export async function listResources(
184
185
  timestamp: timestamp as string,
185
186
  path: resourcePath as string,
186
187
  nucleus: data as any,
187
- } as unknown as ResourceJson;
188
+ } as unknown as Resource;
188
189
  });
189
190
 
190
191
  return [resourceTypeId, items] as const;
@@ -1,7 +1,7 @@
1
1
  import type {
2
- StepKindJson,
3
- ResourceJson,
4
- ResourceTypeIdJson,
2
+ StepKind,
3
+ Resource,
4
+ ResourceTypeId,
5
5
  } from '@toolproof-core/schema';
6
6
  import { CONSTANTS } from '../artifacts/artifacts.js';
7
7
  import { MAPPINGS } from '../artifacts/artifacts.js';
@@ -12,13 +12,13 @@ export type Collection = keyof typeof CONSTANTS.Persistence.Collections;
12
12
 
13
13
  export type SchemaLike = Record<string, unknown>;
14
14
 
15
- export type ResourcesByType = Record<ResourceTypeIdJson, ResourceJson[]>;
15
+ export type ResourcesByType = Record<ResourceTypeId, Resource[]>;
16
16
 
17
17
  export type IdName = keyof typeof MAPPINGS.IdNameToIdPrefix;
18
18
 
19
19
  export type IdStringByIdName<K extends IdName> = `${(typeof MAPPINGS.IdNameToIdPrefix)[K]}${string}`;
20
20
 
21
- export type StepIdStringByStepKind<K extends StepKindJson> = `${(typeof MAPPINGS.StepKindToStepIdPrefix)[K]}${string}`;
21
+ export type StepIdStringByStepKind<K extends StepKind> = string & { readonly __stepKind?: K };
22
22
 
23
23
  export interface NucleusBaseSmall<T extends string = string> {
24
24
  id: T;
@@ -1,110 +1,53 @@
1
1
  import type {
2
- CreationContextJson,
3
- ExternalInputPotentialShellJson,
4
- InternalInputPotentialShellJson,
5
- OutputPotentialShellJson,
6
- ResourceJson,
7
- ResourcePotentialJson,
8
- StrategyStateJson,
2
+ CreationContext,
3
+ Resource,
4
+ ReferenceInput,
5
+ ValueInput,
9
6
  } from '@toolproof-core/schema';
10
- import { CONSTANTS } from '../artifacts/artifacts.js';
11
- import { resolveResourceChain } from './resolveResourceChain.js';
7
+ import {
8
+ clearStrategyStateEntry,
9
+ getStrategyStateEntry,
10
+ setStrategyStateEntry,
11
+ type StrategyState,
12
+ } from './strategyState.js';
12
13
 
13
14
 
14
15
  export function bindInputResInStrategyState(
15
- strategyState: StrategyStateJson,
16
- target: CreationContextJson,
17
- resource: ResourceJson
18
- ): StrategyStateJson {
19
- const bucket = strategyState[target.toolStepId] ?? {};
20
- return {
21
- ...strategyState,
22
- [target.toolStepId]: {
23
- ...bucket,
24
- [target.resourceRoleId]: resource,
25
- },
16
+ strategyState: StrategyState,
17
+ target: CreationContext,
18
+ resource: Resource
19
+ ): StrategyState {
20
+ const entry: ValueInput = {
21
+ strategyStateInputKind: 'valueInput',
22
+ resourceTypeId: resource.resourceTypeId,
23
+ nucleus: resource.nucleus,
26
24
  };
25
+
26
+ return setStrategyStateEntry(strategyState, target, entry);
27
27
  }
28
28
 
29
29
  export function bindInputRefInStrategyState(
30
- strategyState: StrategyStateJson,
31
- target: CreationContextJson,
32
- source: CreationContextJson
33
- ): StrategyStateJson {
34
- const sourceEntry = strategyState?.[source.toolStepId]?.[source.resourceRoleId] as (
35
- | ResourceJson
36
- | ResourcePotentialJson
37
- | undefined
38
- );
30
+ strategyState: StrategyState,
31
+ target: CreationContext,
32
+ source: CreationContext
33
+ ): StrategyState {
34
+ const sourceEntry = getStrategyStateEntry(strategyState, source);
39
35
  if (!sourceEntry) {
40
- throw new Error(`resourceEntry not found for source (${source.toolStepId}, ${source.resourceRoleId})`);
41
- }
42
-
43
- const result = resolveResourceChain(strategyState, source);
44
- const bucket = strategyState[target.toolStepId] ?? {};
45
-
46
- if (result.status === CONSTANTS.Enums.ResourceShellKind.materialized) {
47
- return {
48
- ...strategyState,
49
- [target.toolStepId]: {
50
- ...bucket,
51
- [target.resourceRoleId]: result.entry,
52
- },
53
- };
54
- }
55
-
56
- if (result.status === CONSTANTS.Enums.ResourceShellKind.externalInputPotential) {
57
- const externalInput = result.entry;
58
- const reusedExternalInput: ExternalInputPotentialShellJson = {
59
- id: externalInput.id,
60
- resourceTypeId: externalInput.resourceTypeId,
61
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.externalInputPotential,
62
- };
63
-
64
- return {
65
- ...strategyState,
66
- [target.toolStepId]: {
67
- ...bucket,
68
- [target.resourceRoleId]: reusedExternalInput,
69
- },
70
- };
36
+ throw new Error(`resourceEntry not found for source (${source.toolStepPath}, ${source.roleName})`);
71
37
  }
72
38
 
73
- if (result.status === CONSTANTS.Enums.ResourceShellKind.outputPotential) {
74
- const potentialInput: InternalInputPotentialShellJson = {
75
- id: sourceEntry.id,
76
- resourceTypeId: sourceEntry.resourceTypeId,
77
- creationContext: {
78
- toolStepId: source.toolStepId,
79
- resourceRoleId: source.resourceRoleId,
80
- },
81
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.internalInputPotential,
82
- };
83
-
84
- return {
85
- ...strategyState,
86
- [target.toolStepId]: {
87
- ...bucket,
88
- [target.resourceRoleId]: potentialInput,
89
- },
90
- };
91
- }
39
+ const entry: ReferenceInput = {
40
+ strategyStateInputKind: 'referenceInput',
41
+ toolStepPath: source.toolStepPath,
42
+ roleName: source.roleName,
43
+ };
92
44
 
93
- throw new Error(`Failed to resolve resource chain: ${result.reason}`);
45
+ return setStrategyStateEntry(strategyState, target, entry);
94
46
  }
95
47
 
96
48
  export function clearInputBindingInStrategyState(
97
- strategyState: StrategyStateJson,
98
- target: CreationContextJson
99
- ): StrategyStateJson {
100
- const bucket = strategyState?.[target.toolStepId];
101
- if (!bucket?.[target.resourceRoleId]) return strategyState;
102
-
103
- const nextBucket = { ...bucket } as Record<string, unknown>;
104
- delete nextBucket[target.resourceRoleId];
105
-
106
- return {
107
- ...strategyState,
108
- [target.toolStepId]: nextBucket as any,
109
- };
49
+ strategyState: StrategyState,
50
+ target: CreationContext
51
+ ): StrategyState {
52
+ return clearStrategyStateEntry(strategyState, target);
110
53
  }
@@ -1,31 +1,47 @@
1
1
  import type {
2
- CreationContextJson,
3
- ExternalInputPotentialShellJson,
4
- InternalInputPotentialShellJson,
5
- OutputPotentialShellJson,
6
- ResourceIdJson,
7
- ResourceJson,
8
- ResourceTypeIdJson,
2
+ CreationContext,
3
+ ExternalInput,
4
+ ReferenceInput,
5
+ ResourceId,
6
+ Resource,
7
+ ResourceTypeId,
8
+ StrategyRunId,
9
9
  } from '@toolproof-core/schema';
10
- import { CONSTANTS } from '../../artifacts/artifacts.js';
11
10
 
12
- export function generatePath(resourceTypeId: ResourceTypeIdJson, id: ResourceIdJson): string {
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 {
13
26
  return `${resourceTypeId}/${id}.json`;
14
27
  }
15
28
 
16
29
  export function createMaterializedFromOutputPotential(
17
- outputPotential: OutputPotentialShellJson,
30
+ outputPotential: OutputPotential,
18
31
  nucleus: unknown,
19
32
  timestamp?: string,
20
- ): ResourceJson {
33
+ ): Resource {
21
34
  const { id, resourceTypeId, creationContext } = outputPotential;
22
35
  const path = generatePath(resourceTypeId, id);
23
36
 
24
37
  return {
25
38
  id,
26
39
  resourceTypeId,
27
- creationContext,
28
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.materialized,
40
+ provenance: {
41
+ resourceProvenanceKind: 'strategy',
42
+ strategyRunId: outputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
43
+ creationContext,
44
+ },
29
45
  version: 1,
30
46
  timestamp: timestamp ?? new Date().toISOString(),
31
47
  path,
@@ -34,18 +50,21 @@ export function createMaterializedFromOutputPotential(
34
50
  }
35
51
 
36
52
  export function createMaterializedFromInputPotential(
37
- inputPotential: InternalInputPotentialShellJson,
53
+ inputPotential: InputPotential,
38
54
  nucleus: unknown,
39
55
  timestamp?: string,
40
- ): ResourceJson {
56
+ ): Resource {
41
57
  const { id, resourceTypeId, creationContext } = inputPotential;
42
58
  const path = generatePath(resourceTypeId, id);
43
59
 
44
60
  return {
45
61
  id,
46
62
  resourceTypeId,
47
- creationContext,
48
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.materialized,
63
+ provenance: {
64
+ resourceProvenanceKind: 'strategy',
65
+ strategyRunId: inputPotential.strategyRunId ?? ('STRATEGY_RUN-UNKNOWN' as StrategyRunId),
66
+ creationContext,
67
+ },
49
68
  version: 1,
50
69
  timestamp: timestamp ?? new Date().toISOString(),
51
70
  path,
@@ -54,11 +73,11 @@ export function createMaterializedFromInputPotential(
54
73
  }
55
74
 
56
75
  export function createMaterializedFromPotential(
57
- potential: InternalInputPotentialShellJson | OutputPotentialShellJson,
76
+ potential: InputPotential | OutputPotential,
58
77
  nucleus: unknown,
59
78
  timestamp?: string,
60
- ): ResourceJson {
61
- if (potential.resourceShellKind === CONSTANTS.Enums.ResourceShellKind.internalInputPotential) {
79
+ ): Resource {
80
+ if ('strategyStateInputKind' in potential && potential.strategyStateInputKind === 'referenceInput') {
62
81
  return createMaterializedFromInputPotential(potential, nucleus, timestamp);
63
82
  }
64
83
 
@@ -66,38 +85,37 @@ export function createMaterializedFromPotential(
66
85
  }
67
86
 
68
87
  export function createExternalInputPotential(
69
- id: ResourceIdJson,
70
- resourceTypeId: ResourceTypeIdJson,
71
- ): ExternalInputPotentialShellJson {
88
+ _id: ResourceId,
89
+ _resourceTypeId: ResourceTypeId,
90
+ ): ExternalInput {
72
91
  return {
73
- id,
74
- resourceTypeId,
75
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.externalInputPotential,
92
+ strategyStateInputKind: 'externalInput',
76
93
  };
77
94
  }
78
95
 
79
96
  export function createInputPotential(
80
- id: ResourceIdJson,
81
- resourceTypeId: ResourceTypeIdJson,
82
- creationContext: CreationContextJson,
83
- ): InternalInputPotentialShellJson {
97
+ id: ResourceId,
98
+ resourceTypeId: ResourceTypeId,
99
+ creationContext: CreationContext,
100
+ ): InputPotential {
84
101
  return {
85
102
  id,
86
103
  resourceTypeId,
104
+ strategyStateInputKind: 'referenceInput',
105
+ toolStepPath: creationContext.toolStepPath,
106
+ roleName: creationContext.roleName,
87
107
  creationContext,
88
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.internalInputPotential,
89
108
  };
90
109
  }
91
110
 
92
111
  export function createOutputPotential(
93
- id: ResourceIdJson,
94
- resourceTypeId: ResourceTypeIdJson,
95
- creationContext: CreationContextJson,
96
- ): OutputPotentialShellJson {
112
+ id: ResourceId,
113
+ resourceTypeId: ResourceTypeId,
114
+ creationContext: CreationContext,
115
+ ): OutputPotential {
97
116
  return {
98
117
  id,
99
118
  resourceTypeId,
100
119
  creationContext,
101
- resourceShellKind: CONSTANTS.Enums.ResourceShellKind.outputPotential,
102
120
  };
103
121
  }
@@ -1,42 +1,28 @@
1
1
  import type {
2
- RawStrategyJson,
3
- RunnableStrategyIdJson,
4
- RunnableStrategyJson,
5
- StepJson,
6
- StepArrayByStrategyThreadJson,
7
- StrategyThreadIdJson,
2
+ Step,
3
+ Strategy,
4
+ StrategyId,
8
5
  } from '@toolproof-core/schema';
9
6
  import { getIndependentThreads } from '../parallelizeSteps.js';
7
+ import { CONSTANTS } from '../../artifacts/artifacts.js';
10
8
 
11
- export function getRunnableStrategyThreadGroups(rawStrategy: RawStrategyJson): StepJson[][] {
12
- return getIndependentThreads(rawStrategy.steps, rawStrategy.strategyState);
13
- }
14
-
15
- export function buildRunnableStrategy(
16
- rawStrategy: RawStrategyJson,
17
- runnableStrategyId: RunnableStrategyIdJson,
18
- threadStepGroups: StepJson[][],
19
- threadIdentities: StrategyThreadIdJson[],
20
- ): RunnableStrategyJson {
21
- if (threadStepGroups.length !== threadIdentities.length) {
22
- throw new Error('buildRunnableStrategy requires one thread id per thread group');
9
+ export function getRunnableStrategyThreadGroups(rawStrategy: Strategy): Step[][] {
10
+ if ('steps' in rawStrategy) {
11
+ return getIndependentThreads(rawStrategy.steps, rawStrategy.strategyState as any);
23
12
  }
24
13
 
25
- const stepsByStrategyThread: StepArrayByStrategyThreadJson = {};
26
-
27
- for (const [index, group] of threadStepGroups.entries()) {
28
- const threadId = threadIdentities[index];
29
-
30
- if (!threadId) {
31
- throw new Error(`Missing thread id for thread group index ${index}`);
32
- }
33
-
34
- stepsByStrategyThread[threadId] = group;
35
- }
14
+ return rawStrategy.stepsByThreadIndex;
15
+ }
36
16
 
17
+ export function buildRunnableStrategy(
18
+ threadStepGroups: Step[][],
19
+ runnableStrategyId: StrategyId,
20
+ rawStrategy: Strategy,
21
+ ): Strategy {
37
22
  return {
38
23
  id: runnableStrategyId,
39
- stepsByStrategyThread,
24
+ strategyKind: CONSTANTS.Enums.StrategyKind.threaded,
25
+ stepsByThreadIndex: threadStepGroups,
40
26
  strategyState: rawStrategy.strategyState,
41
27
  };
42
28
  }