0.8.3 • Published 8 months ago

@ai.ntellect/core v0.8.3

Weekly downloads
-
License
ISC
Repository
github
Last release
8 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

8 months ago

0.8.2

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago

0.7.14

10 months ago

0.7.13

10 months ago

0.7.12

10 months ago

0.7.11

10 months ago

0.7.10

10 months ago

0.7.9

10 months ago

0.7.8

10 months ago

0.7.7

10 months ago

0.7.6

10 months ago

0.7.5

10 months ago

0.7.4

10 months ago

0.7.2

10 months ago

0.7.1

10 months ago

0.7.0

10 months ago

0.6.21

11 months ago

0.6.20

11 months ago

0.6.19

11 months ago

0.6.17

11 months ago

0.6.16

11 months ago

0.6.15

11 months ago

0.6.14

11 months ago

0.6.13

11 months ago

0.6.12

11 months ago

0.6.11

11 months ago

0.6.10

11 months ago

0.6.9

11 months ago

0.6.8

11 months ago

0.6.7

11 months ago

0.6.6

11 months ago

0.6.5

11 months ago

0.6.4

11 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.5.0

11 months ago

1.0.0

11 months ago

0.4.1

11 months ago

0.4.0

11 months ago

0.3.3

11 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.2.8

11 months ago

0.2.7

11 months ago

0.2.6

11 months ago

0.2.5

11 months ago

0.2.4

11 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.102

11 months ago

0.1.101

11 months ago

0.1.100

11 months ago

0.1.99

11 months ago

0.1.98

11 months ago

0.1.97

11 months ago

0.1.96

11 months ago

0.1.95

11 months ago

0.1.94

11 months ago

0.1.93

11 months ago

0.1.92

11 months ago

0.1.91

11 months ago

0.1.90

11 months ago

0.1.89

11 months ago

0.1.85

11 months ago

0.1.84

11 months ago

0.1.83

11 months ago

0.1.81

11 months ago

0.1.8

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.0.37

11 months ago

0.0.36

11 months ago

0.0.35

11 months ago

0.0.34

11 months ago

0.0.33

11 months ago

0.0.32

11 months ago

0.0.31

11 months ago

0.0.30

11 months ago

0.0.29

11 months ago

0.0.28

11 months ago

0.0.27

11 months ago

0.0.26

11 months ago

0.0.25

11 months ago

0.0.24

11 months ago

0.0.23

11 months ago

0.0.22

11 months ago

0.0.21

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago