@@ -14,6 +14,7 @@ import {
1414 type ResourceKeyResolver ,
1515} from '@sim/workflow-code'
1616import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits'
17+ import type { BlockState } from '@sim/workflow-types/workflow'
1718import { type CatalogGate , withCatalogBlockScope } from '@/lib/catalog/application/context'
1819import { loadWorkflowGraphs } from '@/lib/workflows/application/read-workflow-graph'
1920import { buildSourceCatalog , storedCatalogTypes } from '@/lib/workflows/code/catalog/workspace'
@@ -28,17 +29,21 @@ type ChildFields = ChildResultFields | null
2829 */
2930export type ChildResultsMemo = Map < string , Promise < ChildFields > >
3031
32+ /**
33+ * The files of each workflow folder a workspace source apply writes, by key: a workflow calling
34+ * one is typed by the child's Response data as the folder holds it, as the directory's check
35+ * types it, not as stored.
36+ */
37+ export type ChildSources = ReadonlyMap < string , Readonly < Record < string , string > > >
38+
3139export interface ChildResultsScope {
3240 workspaceId : string
3341 workspaceOrganizationId : string | null
3442 /** The caller's catalog gate, which the children are compiled or canonicalized under. */
3543 gate : CatalogGate
3644 memo : ChildResultsMemo
37- /**
38- * The files of each workflow folder the enclosing workspace source apply writes, by key: such a
39- * child is read as submitted, not as stored.
40- */
41- submitted ?: ReadonlyMap < string , Readonly < Record < string , string > > >
45+ /** The folders the enclosing workspace source apply writes: such a child is read as submitted. */
46+ submitted ?: ChildSources
4247}
4348
4449/**
@@ -67,6 +72,31 @@ function submittedFields(
6772 return workflow ? childResultFields ( workflow , catalog ) : null
6873}
6974
75+ /** Stored workflow graphs by id, as `loadWorkflowGraphs` reads them. */
76+ export type StoredGraphs = Awaited < ReturnType < typeof loadWorkflowGraphs > >
77+
78+ /** The fields of a stored child workflow, canonicalized under its own source catalog. */
79+ function storedGraphFields (
80+ loaded : NonNullable < ReturnType < StoredGraphs [ 'get' ] > > ,
81+ scope : ChildResultsScope
82+ ) : ChildFields {
83+ const { blocks, edges, loops, parallels, variables } = loaded . graph
84+ const catalog = buildSourceCatalog ( {
85+ gate : scope . gate ,
86+ organizationId : scope . workspaceOrganizationId ,
87+ storedTypes : storedCatalogTypes ( Object . values ( blocks ) ) ,
88+ } )
89+ const { workflow } = canonicalize (
90+ { blocks, edges, loops, parallels, variables } ,
91+ {
92+ catalog,
93+ meta : { name : loaded . name , description : loaded . description ?? null } ,
94+ resources : UNRESOLVED ,
95+ }
96+ )
97+ return childResultFields ( workflow , catalog )
98+ }
99+
70100/** The fields of each stored workflow `keys` name in the workspace, by key. */
71101async function storedFields (
72102 keys : readonly string [ ] ,
@@ -84,29 +114,38 @@ async function storedFields(
84114 for ( const key of keys ) {
85115 const id = ids . get ( key )
86116 const loaded = id === undefined ? undefined : graphs . get ( id )
87- if ( ! loaded ) {
88- fields . set ( key , null )
89- continue
90- }
91- const { blocks, edges, loops, parallels, variables } = loaded . graph
92- const catalog = buildSourceCatalog ( {
93- gate : scope . gate ,
94- organizationId : scope . workspaceOrganizationId ,
95- storedTypes : storedCatalogTypes ( Object . values ( blocks ) ) ,
96- } )
97- const { workflow } = canonicalize (
98- { blocks, edges, loops, parallels, variables } ,
99- {
100- catalog,
101- meta : { name : loaded . name , description : loaded . description ?? null } ,
102- resources : UNRESOLVED ,
103- }
104- )
105- fields . set ( key , childResultFields ( workflow , catalog ) )
117+ fields . set ( key , loaded ? storedGraphFields ( loaded , scope ) : null )
106118 }
107119 return fields
108120}
109121
122+ /**
123+ * The ids of the workflows stored `blocks` may run as children (each `workflowId` value), for a
124+ * read to load their graphs with its own: a superset of the calls its canonical model names.
125+ */
126+ export function storedChildWorkflowIds ( blocks : Iterable < BlockState > ) : string [ ] {
127+ const ids = new Set < string > ( )
128+ for ( const block of blocks ) {
129+ const value = block . subBlocks ?. workflowId ?. value
130+ if ( typeof value === 'string' && value !== '' ) ids . add ( value )
131+ }
132+ return [ ...ids ]
133+ }
134+
135+ /**
136+ * Loads the graphs of the workflows `ids` name that `loaded` lacks, in one read, and returns them
137+ * with `loaded`'s ({@link storedChildWorkflowIds}).
138+ */
139+ export async function loadChildGraphs (
140+ workspaceId : string ,
141+ ids : readonly string [ ] ,
142+ loaded : StoredGraphs = new Map ( )
143+ ) : Promise < StoredGraphs > {
144+ const missing = [ ...new Set ( ids ) ] . filter ( ( id ) => ! loaded . has ( id ) )
145+ if ( missing . length === 0 ) return loaded
146+ return new Map ( [ ...loaded , ...( await loadWorkflowGraphs ( workspaceId , missing ) ) ] )
147+ }
148+
110149/**
111150 * The typed result fields of each child workflow `keys` name that always responds, by key: a
112151 * child the enclosing apply submits as submitted, any other as its stored draft in the workspace
@@ -140,3 +179,26 @@ export async function loadChildResults(
140179 for ( const [ key , fields ] of read ) if ( fields ) results . set ( key , fields )
141180 return results
142181}
182+
183+ /**
184+ * {@link loadChildResults } for a read of stored workflows that loaded their children's graphs with
185+ * {@link loadChildGraphs} (`toId` names the workflow a key resolves to): a child read from there,
186+ * where one the workspace does not hold or holds archived is left out, so a read makes no query of
187+ * its own. A key that resolves to no workflow is read as {@link loadChildResults} reads it.
188+ */
189+ export async function loadChildResultsFrom (
190+ scope : ChildResultsScope ,
191+ keys : readonly string [ ] ,
192+ graphs : StoredGraphs ,
193+ toId : ( key : string ) => string | null
194+ ) : Promise < Map < string , ChildResultFields > > {
195+ await withCatalogBlockScope ( scope . gate , async ( ) => {
196+ for ( const key of keys ) {
197+ const id = toId ( key )
198+ if ( id === null || scope . memo . has ( key ) || scope . submitted ?. has ( key ) ) continue
199+ const loaded = graphs . get ( id )
200+ scope . memo . set ( key , Promise . resolve ( loaded ? storedGraphFields ( loaded , scope ) : null ) )
201+ }
202+ } )
203+ return loadChildResults ( scope , keys )
204+ }
0 commit comments