[Go to site: main page, start]

Skip to content

Code Flows ​

Code flows are JavaScript automations with direct access to Puppeteer and Puppetflow's magic methods.

Writing a code flow ​

A code flow exports an async run function that receives the Puppeteer page, the input object, the run context, and the CDP session:

js
async function run($page, $input, $context, $client) {
  await $gotoUrl('https://example.com', 'Default');
  return $generateResponseSuccess('Done');
}

$page is a proxy to the active Puppeteer Page. $context contains run metadata such as run_id and flow_id. $client is the active tab's CDP session.

Both trailing parameters can be omitted. See Flow Objects for details.

An optional terminate function runs after run, including when it fails. It receives the response produced by run as $output ($output.status is success or error):

js
async function terminate($page, $input, $output, $context, $client) {
  if ($output.status === 'error') await $screenshot('failure');
}

See Working with Downloaded Files for the built-in PDF and archive tools.

Editor assistance ​

The Monaco editor provides JavaScript and TypeScript language assistance without changing the JavaScript executed by the flow. Autocomplete, signatures, hover information, and diagnostics run in a separate browser worker.

Type $ to browse suggestions grouped by purpose:

  • Suggested contains the values and helpers most often used in the current context, such as $input, $page, $now, $if, and $ifEmpty.
  • Methods contains every Puppetflow helper exposed to code flows, JavaScript symbols, and object methods.
  • Tools contains resource-aware helpers and values for variables, notification channels, AI models, Data Tables, mailbox watchers, snippets, tabs, stopwatches, network profiles, and cookie jars.

Type $page. or $client. to browse the Puppeteer Page and CDP APIs. The available members and signatures come from the Puppeteer version installed with Puppetflow. Resource suggestions display readable names but insert stable IDs. Type $$ to insert an accessible snippet.

Input ​

Code flows receive input through $input:

  • Default inputs - JSON defined in the flow settings.
  • Manual input - JSON entered before a manual run.
  • Trigger input - Data sent by a webhook or cron trigger.
  • API input - JSON sent through the REST API.

Variables in input values use the ${vars.ID} syntax.

Blueprint code flows can declare resource inputs. For example:

js
// @input model [ai-model]
// @input table [datatable]

The generated input value keeps the resource-reference syntax and resolves to the configured resource ID:

json
{
  "model": "${aiModels.ai-model-id}",
  "table": "${dataTables.data-table-id}"
}

In the Flow Inputs form, Data Table values use a searchable selector showing the table name, visibility, and column count. In JSON mode, ${dataTables. provides table-name autocomplete while inserting the stable ID. At execution, ${dataTables.<tableId>} becomes the plain table ID.

Output ​

The value returned by run becomes the run output:

js
$setOutput('source', 'invoice-import');

return $generateResponseSuccess('Operation completed', {
  invoiceId: '12345',
  amount: 99.99,
});

Use $generateResponseError() to return an error. Keys added with $setOutput() are included in the response.

Calling snippets ​

Active snippets available to the flow are exposed as async functions using their $$id. Arguments are positional and follow the order declared by the snippet:

js
const address = await $$snip_k8Zt3xQ9mA2f(street, city);

The same syntax calls code and nodal snippets. A nodal snippet receives the declared arguments through $input and returns its $output object. See Snippets for creation, local functions, and import or export details.

Using AI Beta ​

Configure an AI integration and an AI model containing one provider model. Pass its AiModel.id, which starts with aim_, to $aiMessage or $aiControl.

Use $aiMessage for text generation:

js
const response = await $aiMessage(
  'ai-model-id',
  'Extract the company name from this text: ' + $input.text,
  {
    system: 'Return only the company name.',
    temperature: 0.2,
    max_tokens: 100,
  },
);

$setOutput('company', response.text);

Set outputMode to json for a JSON object, or to schema with a JSON Schema in schema for constrained structured output. Previous messages can be passed as { role, content } objects in messages.

Use $aiControl with a vision-capable model to inspect and operate the current browser page:

js
const control = await $aiControl(
  'ai-model-id',
  'Open the account menu and sign out',
  {
    maxIterations: 8,
    screenshotQuality: 65,
    persistScreenshots: false,
    allowPuppeteerFallback: true,
  },
);

if (control.status !== 'success') {
  return $generateResponseError(control.message);
}

AI Control executes only its restricted Puppetflow browser actions. When allowPuppeteerFallback is enabled, it may use restricted Puppeteer actions after a Puppetflow action fails, or use a coordinate click for a purely visual target. Page content is treated as untrusted data.

See the Flow API reference for parameters, return values, and limits.

Repository linking ​

A code flow can be linked to a file in a Git repository. Choose the integration, repository, branch, file path, and sync trigger.

Repository-linked code is read-only in Puppetflow and updates on the selected push or tag event.