MCPcopy Create free account
hub / github.com/Make-md/makemd / runCommand

Method runCommand

src/core/superstate/commands.ts:964–1023  ·  view source on GitHub ↗
(action: string, instance: ActionInstance)

Source from the content-addressed store, hash-verified

962 }
963
964 public async runCommand(action: string, instance: ActionInstance): Promise<ActionInstance> {
965 const uri = parseURI(action);
966
967 if (uri.authority !== '$api') {
968 const command = this.commandForAction(action);
969 if (!command) {
970 return {...instance, error: new Error(`Command not found for action: ${action}`)};
971 }
972
973 let newInstance = instance;
974 try {
975 if (command.schema.type === 'actions') {
976 newInstance = await runActionString(this.superstate, command.code || "", instance);
977 } else if (command.schema.type === 'script') {
978 newInstance.result = await executeCode(command.code, {...instance.instanceProps, $prev: instance.result});
979 } else if (command.schema.type === 'formula') {
980 newInstance.result = runFormulaWithContext(
981 this.superstate.formulaContext,
982 this.superstate.pathsIndex,
983 this.superstate.spacesMap,
984 command.code,
985 command.fields.reduce((p, c) => ({ ...p, [c.name]: c }), {$prev: instance.result}),
986 instance.instanceProps
987 );
988 }
989 } catch (e) {
990 newInstance.error = e;
991 }
992 return newInstance;
993 }
994
995 const command = this.commandForAction(action);
996
997 if (!command) {
998 return {...instance, error: new Error(`Command not found for action: ${action}`)};
999 }
1000
1001 let newInstance = instance;
1002 let result;
1003
1004 try {
1005 if (command.schema.type === 'api') {
1006 const [namespace, method] = command.schema.id.split('.');
1007 const api = this.superstate.api as unknown as Record<string, any>;
1008 const namespaceMethods = api[namespace];
1009
1010 if (namespaceMethods && typeof namespaceMethods[method] === 'function') {
1011 result = await namespaceMethods[method](...command.fields.map(f => instance.instanceProps[f.name]));
1012 }
1013 }
1014 } catch (e) {
1015 newInstance.error = e;
1016 }
1017
1018 if (command.schema.type === 'api' && result !== undefined) {
1019 newInstance = {...newInstance, result};
1020 }
1021

Callers

nothing calls this directly

Calls 8

commandForActionMethod · 0.95
parseURIFunction · 0.90
runActionStringFunction · 0.90
executeCodeFunction · 0.90
runFormulaWithContextFunction · 0.90
reduceMethod · 0.80
splitMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected