1.0.0 • Published 5 months ago

@silyze/browsary-pipeline v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Browsary Pipeline

Core library for defining, compiling, and executing JSON-defined pipelines in Browsary.


Installation

npm install @silyze/browsary-pipeline

Quick Start

import { createJsonLogger } from "@silyze/logger";
import {
  hasPipeline,
  PipelineCompiler,
  StandardLibraryProvider,
  waitForPipelineThread,
} from "@silyze/browsary-pipeline";
import { UrlBrowserProvider } from "@silyze/browser-provider";

const pipelineSource = {
  create_browser: {
    node: "browser::create",
    inputs: {},
    outputs: { browser: "browser" },
    dependsOn: [],
  },
  goto_google: {
    node: "page::goto",
    inputs: {
      page: {
        type: "outputOf",
        nodeName: "create_browser",
        outputName: "browser",
      },
      url: { type: "constant", value: "https://www.google.com" },
      waitUntil: { type: "constant", value: "load" },
    },
    outputs: {},
    dependsOn: "create_browser",
  },
};

async function main() {
  const compiler = new PipelineCompiler();
  const result = compiler.compile(pipelineSource);

  if (!hasPipeline(result)) {
    console.error("Compilation errors:", result.errors);
    return;
  }

  const logger = createJsonLogger(console.log);
  const evaluation = result.pipeline.createEvaluation({
    logger,
    browserProvider: new UrlBrowserProvider(),
    libraryProvider: StandardLibraryProvider,
    viewport: { width: 1024, height: 768 },
  });

  try {
    for await (const thread of evaluation.evaluate()) {
      const outputs = await waitForPipelineThread(thread);
      console.log("Thread outputs:", outputs);
    }
  } finally {
    evaluation.stop();
  }
}

main();

API Reference

Classes & Functions

PipelineCompiler

constructor(): PipelineCompiler
compile(source: PipelineSchema): PipelineCompileResult
  • Validates source against pipelineSchema and returns:

    • { errors: PipelineCompileError[] } on failure
    • { errors: []; pipeline: Pipeline } on success

hasPipeline

declare function hasPipeline(
  result: PipelineCompileResult
): result is { pipeline: Pipeline };

Type guard for compile results.

waitForPipelineThread

declare function waitForPipelineThread(
  thread: PipelineThread
): Promise<Record<string, unknown>>;

Awaits a single thread’s completion and returns its outputs.

pipelineSchema

AJV JSON schema for pipeline definitions.

Types & Interfaces

PipelineSchema

type PipelineSchema = Record<string, GenericNodeDefinition>;

GenericNodeDefinition

interface GenericNodeDefinition {
  node: string;
  inputs?: Record<string, InputNode>;
  outputs?: Record<string, string>;
  dependsOn?: string | string[];
}

InputNode

type InputNode =
  | { type: "constant"; value: unknown }
  | { type: "outputOf"; nodeName: string; outputName: string };

PipelineCompileResult

type PipelineCompileResult =
  | { errors: PipelineCompileError[] }
  | { errors: []; pipeline: Pipeline };

Pipeline

interface Pipeline {
  createEvaluation(config: EvaluationConfig): PipelineEvaluation;
}

PipelineEvaluation

interface PipelineEvaluation {
  evaluate(): AsyncIterable<PipelineThread>;
  stop(): void;
}

PipelineThread

Opaque execution path handle. Use waitForPipelineThread.

PipelineCompileError

TypeMessageAdditional Fields
pipeline-not-objectThe pipeline is not an object
node-not-objectThe node is not an objectnodeName
node-missing-propertyThe node is missing a propertynodeName, propertyName
node-invalid-property-typeThe node has an invalid property typenodeName, propertyName, expectedType, actualType
node-invalid-property-valueThe node has an invalid property valuenodeName, propertyName, expectedFormat, actualValue
dependency-not-foundThe node has an invalid dependencynodeName, dependency
self-dependencyThe depends on itself - this causes an infinite loopnodeName
no-entrypointsThe pipeline has no entrypoints - make sure there is at least a single node with zero dependencies
node-type-not-foundThe node type was not foundnodeType, nodeName
node-not-foundThe node was not foundnodeName
node-type-reference-not-foundThe node type was not found in referencenodeType, nodeName, referencedIn
node-type-missing-outputThe output is missing from the node typenodeType, nodeName, outputName
node-type-missing-inputThe input is missing from the node typenodeType, nodeName, inputName
node-type-input-no-constThe input cannot be represented as a constantnodeType, nodeName, inputName
const-input-type-mismatchThe input type does not match the expected schemanodeType, nodeName, inputName, value, expectedSchema
node-type-input-no-output-ofThe input cannot be represented as an output referencenodeType, nodeName, inputName
node-input-reference-not-foundThe node referenced by an input was not foundnodeName, inputName, referenceNodeName
output-reference-not-foundThe output referenced by an input was not foundnodeName, inputName, referenceNodeName, referenceOutputName
output-reference-node-type-not-foundThe output node type referenced by an input was not foundnodeName, inputName, referenceNodeName, referenceNodeType
output-reference-type-not-foundThe output type was not found in the referenced node typenodeName, inputName, referenceNodeName, referenceNodeType, referenceOutputName, referenceOutput
ref-input-type-mismatchThe referenced output type does not match the input typenodeName, inputName, inputType, referenceNodeName, referenceNodeType, referenceOutputName, referenceOutput, referenceOutputType
unreachable-nodeThe node is not reachable from any entrypointnodeName
ref-input-not-dependantThe referenced output's node is not a dependency to the input's nodenodeName, inputName, referenceNodeName, referenceOutputName
export type PipelineCompileError = {
  type: string;
  message: string;
  [key: string]: unknown;
};

EvaluationConfig

interface EvaluationConfig {
  logger: Logger;
  browserProvider: BrowserProvider;
  libraryProvider: EvaluationLibrary;
  viewport?: ViewportConfig;
}

StandardLibraryProvider Nodes

Built-in node implementations available via StandardLibraryProvider:

NodeDescriptionInputsOutputs
browser::createLaunches a new browser instancenonebrowser
browser::closeCloses an existing browser instancebrowsernone
browser::createPageOpens a new page within a browserbrowserpage
page::closeCloses an existing page instancepagenone
page::gotoNavigates a page to a specified URLpage, url (string), waitUntil? (load | domcontentloaded)none
page::clickClicks an element matching a CSS selectorpage, selector (string)none
page::typeTypes text into an input elementpage, selector (string), text (string)none
page::displayRetrieves the HTML content of the pagepagecontent (string)