const __workflowRun = (call, value) => {
const ref = value.__workflow_run;
const facade = {__workflow_run:ref};
Object.defineProperty(facade,'result',{value:async () => (await call('join', {runs:[{__workflow_run:ref}]}))[0]});
return Object.freeze(facade);
};
const __workflowHandle = (call, scope, value) => {
const ref = value.__workflow_ref;
const fn = async input => (await fn.start(input)).result();
Object.defineProperties(fn, {
__workflow_ref:{value:ref},
compatibility:{value:value.__workflow_compatibility},
start:{value:async input => __workflowRun(call, await call('start', {handle:{__workflow_ref:ref}, input, scope}))}
});
return Object.freeze(fn);
};
const __workflowHydrate = (call, scope, value) => {
if (value && value.__workflow_ref) return __workflowHandle(call, scope, value);
if (value && value.__workflow_run) return __workflowRun(call, value);
if (Array.isArray(value)) return value.map(item => __workflowHydrate(call, scope, item));
if (value && value instanceof Object) return Object.fromEntries(Object.entries(value).map(([k,v])=>[k,__workflowHydrate(call, scope, v)]));
return value;
};
const __workflowAPI = (scope) => {
const call = (method, args) => __callBuiltin('workflows.' + method, args);
const hydrate = value => __workflowHydrate(call, scope, value);
const definition = (source, schemas = {}, bindings = {}) => ({source:source instanceof Function ? source.toString() : source,
input:schemas.input ?? true, output:schemas.output ?? true, bindings});
const api = {
hydrate,
define: async (source, schemas, bindings) => hydrate(await call('define', {definition:definition(source,schemas,bindings),scope})),
bundle: async definitions => hydrate(await call('bundle', {definitions,scope})),
resolve: async (name,version) => { const args={name,scope}; if (version !== undefined) args.version=version; return hydrate(await call('resolve',args)); },
inspect: workflow => call('inspect',{handle:workflow}),
bind: async (workflow,values) => hydrate(await call('bind', {handle:workflow,values,scope})),
pipe: async workflows => hydrate(await call('pipe', {handles:workflows,scope})),
start: async (workflow,input) => hydrate(workflow).start(input),
map: async (workflow,inputs,concurrency=4) => {
if (!Array.isArray(inputs) || !Number.isSafeInteger(concurrency) || concurrency < 1 || concurrency > 256) throw new Error('WORKFLOW_ARGUMENT: map requires an array and concurrency 1-256');
const fn = hydrate(workflow), values = [];
for (let i=0;i<inputs.length;i+=concurrency) {
const runs = await Promise.all(inputs.slice(i,i+concurrency).map(input=>fn.start(input)));
values.push(...await call('join', {runs}));
}
return values;
},
settle: runs => call('join',{runs,settle:true}),
publish: (workflow,name,expectedVersion,description) => { const args={handle:workflow,name}; if (expectedVersion !== undefined) args.expectedVersion=expectedVersion; if (description !== undefined) args.description=description; return call('publish',args); },
delete: (name,expectedVersion) => call('delete',{name,expectedVersion}),
release: workflow => call('release',{handle:workflow}),
scope: async body => {
const childScope = await call('scope_open',{});
let value, failure;
try { value = await body(__workflowAPI(childScope)); } catch (error) { failure=error; }
await call('scope_close',{scope:childScope,failed:failure !== undefined});
if (failure !== undefined) throw failure;
return value;
}
};
return Object.freeze(api);
};
codemode.workflows = __workflowAPI(undefined);