0.8.3 • Published 12 months ago

@ai.ntellect/core v0.8.3

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

@ai.ntellect/core

A TypeScript framework for building workflow automation with event-driven architecture.

Features

  • Graph-based workflow execution
  • Event-driven node processing
  • TypeScript type safety
  • Zod schema validation
  • Retry mechanisms
  • State observation
  • AI Agent integration

Installation

npm install @ai.ntellect/core zod

Basic Usage

Building a simple workflow

import { z } from "zod";
import { GraphFlow } from "@ai.ntellect/core";
import { GraphContext, GraphNodeConfig } from "@ai.ntellect/core/types";

// Define the schema
const Schema = z.object({
  message: z.string(),
});

// Define a node
const greetNode = {
  name: "greet",
  execute: async (context: GraphContext<typeof Schema>) => {
    context.message = "Hello, World!";
  },
};

// Create workflow
const workflow = new GraphFlow({
  name: "hello",
  schema: Schema,
  context: { message: "" },
  nodes: [greetNode],
});

// Execute and observe
const main = async () => {
  // Observe state changes
  workflow
    .observe()
    .state()
    .subscribe((context) => {
      console.log(context);
    });

  // Execute workflow
  await workflow.execute("greet");
};

main();

Handling events in a workflow

import { z } from "zod";
import { GraphFlow } from "@ai.ntellect/core";
import { GraphContext } from "@ai.ntellect/core/types";

// Define schema
const OrderSchema = z.object({
  orderId: z.string(),
  status: z.string(),
  amount: z.number(),
});

// Define nodes
const paymentNode: GraphNodeConfig<typeof OrderSchema> = {
  name: "payment",
  when: {
    events: ["payment.received"],
    timeout: 30000,
    strategy: { type: "single" },
  },
  execute: async (context: GraphContext<typeof OrderSchema>) => {
    context.status = "processing";
  },
  next: ["validation"],
};

const validationNode: GraphNodeConfig<typeof OrderSchema> = {
  name: "validation",
  when: {
    events: ["payment.validated", "inventory.checked"],
    timeout: 5000,
    strategy: {
      type: "correlate",
      correlation: (events) => {
        return events.every(
          (e) => e.payload.orderId === events[0].payload.orderId
        );
      },
    },
  },
  execute: async (context: GraphContext<typeof OrderSchema>) => {
    context.status = "validated";
  },
};

// Create workflow
const orderWorkflow = new GraphFlow({
  name: "order",
  schema: OrderSchema,
  context: {
    orderId: "",
    status: "pending",
    amount: 0,
  },
  nodes: [paymentNode, validationNode],
});

// Usage
const main = async () => {
  // Observe state
  orderWorkflow
    .observe()
    .property("status")
    .subscribe((status) => {
      console.log("Status:", status);
    });

  // Start listening for events
  orderWorkflow.execute("payment");

  // Emit event after a short delay
  setTimeout(async () => {
    await orderWorkflow.emit("payment.received", {
      orderId: "123",
      amount: 100,
    });
  }, 100);

  // Observe payment received event
  orderWorkflow
    .observe()
    .event("payment.received")
    .subscribe((event) => {
      console.log("Payment received:", event);
      orderWorkflow.emit("inventory.checked", {
        orderId: event.payload.orderId,
      });
    });

  // Observe inventory checked event
  orderWorkflow
    .observe()
    .event("inventory.checked")
    .subscribe((event) => {
      console.log("Inventory checked:", event);
      orderWorkflow.emit("payment.validated", {
        orderId: event.payload.orderId,
      });
    });

  // Observe payment validated event
  orderWorkflow
    .observe()
    .event("payment.validated")
    .subscribe((event) => {
      console.log("Payment validated:", event);
    });
};

main();

Creating a workflow with Agent

import { z } from "zod";
import { GraphFlow, Agent } from "@ai.ntellect/core";
import { GraphContext } from "@ai.ntellect/core/types";

const EmailSchema = z.object({
  to: z.string(),
  subject: z.string(),
  content: z.string(),
  status: z.string(),
});

const sendNode = {
  name: "send",
  execute: async (context: GraphContext<typeof EmailSchema>) => {
    context.status = "sending";
    // Email sending implementation
    context.status = "sent";
  },
};

const emailFlow = new GraphFlow({
  name: "email",
  schema: EmailSchema,
  context: {
    to: "",
    subject: "",
    content: "",
    status: "pending",
  },
  nodes: [sendNode],
});

const assistant = new Agent({
  role: "Email Assistant",
  goal: "Help users send emails",
  tools: [emailFlow],
  llmConfig: {
    provider: "openai",
    model: "gpt-4",
    apiKey: process.env.OPENAI_API_KEY,
  },
});

const main = async () => {
  const result = await assistant.process(
    "Send an email to john@example.com about the project update"
  );
  console.log(result);
};

main();

Documentation

See the documentation for detailed usage examples.

Contributing

Contributions are welcome. Please submit pull requests with tests.

License

MIT

0.8.3

12 months ago

0.8.2

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.14

1 year ago

0.7.13

1 year ago

0.7.12

1 year ago

0.7.11

1 year ago

0.7.10

1 year ago

0.7.9

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.7.4

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.21

1 year ago

0.6.20

1 year ago

0.6.19

1 year ago

0.6.17

1 year ago

0.6.16

1 year ago

0.6.15

1 year ago

0.6.14

1 year ago

0.6.13

1 year ago

0.6.12

1 year ago

0.6.11

1 year ago

0.6.10

1 year ago

0.6.9

1 year ago

0.6.8

1 year ago

0.6.7

1 year ago

0.6.6

1 year ago

0.6.5

1 year ago

0.6.4

1 year ago

0.6.3

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

1.0.0

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.3

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.102

1 year ago

0.1.101

1 year ago

0.1.100

1 year ago

0.1.99

1 year ago

0.1.98

1 year ago

0.1.97

1 year ago

0.1.96

1 year ago

0.1.95

1 year ago

0.1.94

1 year ago

0.1.93

1 year ago

0.1.92

1 year ago

0.1.91

1 year ago

0.1.90

1 year ago

0.1.89

1 year ago

0.1.85

1 year ago

0.1.84

1 year ago

0.1.83

1 year ago

0.1.81

1 year ago

0.1.8

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago