{"library":"machine","title":"Node-Machine Runner","description":"The `machine` package is a JavaScript runner for functions that adhere to the Node-Machine specification, an open standard for atomic, context-free subroutines. It allows developers to define functions (called 'machines') with explicit inputs and exits, enabling robust static analysis, automatic documentation generation, UI inference, and advanced toolchain integration. This package, currently at version 15.2.3, provides the core `Machine.build()` method to transform machine definitions into callable JavaScript functions. While it sees continuous development with incremental releases and pre-releases, direct usage is often unnecessary, as higher-level `node-machine` modules (like `machine-as-action` or `machine-as-script`) frequently abstract its functionality. Its key differentiator is enforcing a strict function signature and behavior via the machine spec, which promotes maintainability and composability.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install machine"],"cli":null},"imports":["const Machine = require('machine');","const callable = Machine.build({ /* machine definition */ });","const result = await callable(argins).exec();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Machine = require('machine');\n\n// Define a simple machine to greet a user\nconst greetMachine = Machine({\n  identity: 'greet-user',\n  friendlyName: 'Greet User',\n  description: 'Greets a user by their provided name.',\n  inputs: {\n    name: {\n      type: 'string',\n      required: true,\n      description: 'The name of the user to greet.'\n    },\n    greeting: {\n      type: 'string',\n      defaultsTo: 'Hello',\n      description: 'The greeting to use (e.g., \"Hi\", \"Bonjour\").'\n    }\n  },\n  exits: {\n    success: {\n      outputFriendlyName: 'Greeting message',\n      outputDescription: 'The personalized greeting.',\n      outputExample: 'Hello Alice!'\n    },\n    error: {\n      outputFriendlyName: 'Error',\n      outputDescription: 'An unexpected error occurred.'\n    }\n  },\n  fn: function(inputs, exits) {\n    try {\n      const message = `${inputs.greeting} ${inputs.name}!`;\n      return exits.success(message);\n    } catch (err) {\n      return exits.error(err);\n    }\n  }\n});\n\nasync function runGreeting() {\n  try {\n    const formalGreeting = await greetMachine({\n      name: 'Dr. Smith',\n      greeting: 'Good day'\n    });\n    console.log(formalGreeting);\n    // Expected: Good day Dr. Smith!\n\n    const casualGreeting = await greetMachine({\n      name: 'Alice'\n    });\n    console.log(casualGreeting);\n    // Expected: Hello Alice!\n\n    // Example of calling with missing required input (will hit 'error' exit)\n    await greetMachine({}); // This will throw an error if not caught via .catch()\n  } catch (err) {\n    console.error('Machine execution failed:', err.raw.errors[0].message); // Accessing the raw error in v13+\n  }\n}\n\nrunGreeting();","lang":"javascript","description":"This quickstart demonstrates defining a simple 'machine' with inputs and exits, building it into a callable function, and executing it using async/await syntax to handle both success and error paths, showcasing input defaults and basic error handling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}