|
| 1 | +/** |
| 2 | + * How the server types a child workflow call's `<call.result.data.<field>>` in a JSON field: by |
| 3 | + * the fields the child's Response data holds (`childResultFields`), read from the child's draft |
| 4 | + * as stored, or as the workspace source apply that holds the call submits it. Every source compile |
| 5 | + * and print of a workflow goes through the same reading, so a get prints a reference in the form |
| 6 | + * the next apply stores again, and a workspace directory's check (which reads the children from |
| 7 | + * the directory) agrees with both. |
| 8 | + */ |
| 9 | +import { |
| 10 | + type ChildResultFields, |
| 11 | + canonicalize, |
| 12 | + childResultFields, |
| 13 | + compileWorkflow, |
| 14 | + type ResourceKeyResolver, |
| 15 | +} from '@sim/workflow-code' |
| 16 | +import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits' |
| 17 | +import { type CatalogGate, withCatalogBlockScope } from '@/lib/catalog/application/context' |
| 18 | +import { loadWorkflowGraphs } from '@/lib/workflows/application/read-workflow-graph' |
| 19 | +import { buildSourceCatalog, storedCatalogTypes } from '@/lib/workflows/code/catalog/workspace' |
| 20 | +import { resolveIds } from '@/lib/workspaces/resource-keys' |
| 21 | + |
| 22 | +/** A child's typed result fields, or `null` when it can end without responding or is unreadable. */ |
| 23 | +type ChildFields = ChildResultFields | null |
| 24 | + |
| 25 | +/** |
| 26 | + * One request's readings of child workflows by key, each read at most once: a workspace apply |
| 27 | + * shares one across every workflow it evaluates. |
| 28 | + */ |
| 29 | +export type ChildResultsMemo = Map<string, Promise<ChildFields>> |
| 30 | + |
| 31 | +export interface ChildResultsScope { |
| 32 | + workspaceId: string |
| 33 | + workspaceOrganizationId: string | null |
| 34 | + /** The caller's catalog gate, which the children are compiled or canonicalized under. */ |
| 35 | + gate: CatalogGate |
| 36 | + 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>>> |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Canonicalize reads resources only to lift ids to keys, which no Response field's type depends |
| 46 | + * on, so the children are read without the workspace's resource keys. |
| 47 | + */ |
| 48 | +const UNRESOLVED: ResourceKeyResolver = { toKey: () => null, toId: () => null } |
| 49 | + |
| 50 | +/** The fields of a submitted folder, compiled on its own as the directory's check compiles it. */ |
| 51 | +function submittedFields( |
| 52 | + files: Readonly<Record<string, string>>, |
| 53 | + scope: ChildResultsScope |
| 54 | +): ChildFields { |
| 55 | + const entry = files[SOURCE_ENTRY_FILE] |
| 56 | + if (entry === undefined) return null |
| 57 | + const catalog = buildSourceCatalog({ |
| 58 | + gate: scope.gate, |
| 59 | + organizationId: scope.workspaceOrganizationId, |
| 60 | + }) |
| 61 | + const { workflow } = compileWorkflow({ |
| 62 | + entry: { path: SOURCE_ENTRY_FILE, text: entry }, |
| 63 | + files: new Map(Object.entries(files).filter(([path]) => path !== SOURCE_ENTRY_FILE)), |
| 64 | + catalog, |
| 65 | + sdkMajor: catalog.sdkMajor, |
| 66 | + }) |
| 67 | + return workflow ? childResultFields(workflow, catalog) : null |
| 68 | +} |
| 69 | + |
| 70 | +/** The fields of each stored workflow `keys` name in the workspace, by key. */ |
| 71 | +async function storedFields( |
| 72 | + keys: readonly string[], |
| 73 | + scope: ChildResultsScope |
| 74 | +): Promise<Map<string, ChildFields>> { |
| 75 | + const ids = |
| 76 | + ( |
| 77 | + await resolveIds( |
| 78 | + scope.workspaceId, |
| 79 | + keys.map((key) => ({ kind: 'workflow' as const, key })) |
| 80 | + ) |
| 81 | + ).get('workflow') ?? new Map<string, string>() |
| 82 | + const graphs = await loadWorkflowGraphs(scope.workspaceId, [...ids.values()]) |
| 83 | + const fields = new Map<string, ChildFields>() |
| 84 | + for (const key of keys) { |
| 85 | + const id = ids.get(key) |
| 86 | + 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)) |
| 106 | + } |
| 107 | + return fields |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * The typed result fields of each child workflow `keys` name that always responds, by key: a |
| 112 | + * child the enclosing apply submits as submitted, any other as its stored draft in the workspace |
| 113 | + * (one the workspace does not hold, or holds archived, is left out). Each key is read once per |
| 114 | + * `scope.memo`. |
| 115 | + */ |
| 116 | +export async function loadChildResults( |
| 117 | + scope: ChildResultsScope, |
| 118 | + keys: readonly string[] |
| 119 | +): Promise<Map<string, ChildResultFields>> { |
| 120 | + const unread = keys.filter((key) => !scope.memo.has(key)) |
| 121 | + const stored = unread.filter((key) => !scope.submitted?.has(key)) |
| 122 | + if (unread.length > 0) { |
| 123 | + const reading = withCatalogBlockScope(scope.gate, async () => { |
| 124 | + const fields = stored.length > 0 ? await storedFields(stored, scope) : new Map() |
| 125 | + for (const key of unread) { |
| 126 | + const files = scope.submitted?.get(key) |
| 127 | + if (files) fields.set(key, submittedFields(files, scope)) |
| 128 | + } |
| 129 | + return fields |
| 130 | + }) |
| 131 | + for (const key of unread) { |
| 132 | + scope.memo.set( |
| 133 | + key, |
| 134 | + reading.then((fields) => fields.get(key) ?? null) |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + const read = await Promise.all(keys.map(async (key) => [key, await scope.memo.get(key)] as const)) |
| 139 | + const results = new Map<string, ChildResultFields>() |
| 140 | + for (const [key, fields] of read) if (fields) results.set(key, fields) |
| 141 | + return results |
| 142 | +} |
0 commit comments