0.8.3 • Published 6 months ago

@ai.ntellect/core v0.8.3

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

6 months ago

0.8.2

7 months ago

0.8.1

7 months ago

0.8.0

7 months ago

0.7.14

8 months ago

0.7.13

8 months ago

0.7.12

8 months ago

0.7.11

8 months ago

0.7.10

8 months ago

0.7.9

8 months ago

0.7.8

8 months ago

0.7.7

8 months ago

0.7.6

8 months ago

0.7.5

8 months ago

0.7.4

8 months ago

0.7.2

8 months ago

0.7.1

8 months ago

0.7.0

8 months ago

0.6.21

8 months ago

0.6.20

8 months ago

0.6.19

8 months ago

0.6.17

9 months ago

0.6.16

9 months ago

0.6.15

9 months ago

0.6.14

9 months ago

0.6.13

9 months ago

0.6.12

9 months ago

0.6.11

9 months ago

0.6.10

9 months ago

0.6.9

9 months ago

0.6.8

9 months ago

0.6.7

9 months ago

0.6.6

9 months ago

0.6.5

9 months ago

0.6.4

9 months ago

0.6.3

9 months ago

0.6.2

9 months ago

0.6.1

9 months ago

0.6.0

9 months ago

0.5.0

9 months ago

1.0.0

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.3

9 months ago

0.3.1

9 months ago

0.3.0

9 months ago

0.2.8

9 months ago

0.2.7

9 months ago

0.2.6

9 months ago

0.2.5

9 months ago

0.2.4

9 months ago

0.2.3

9 months ago

0.2.2

9 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.1.102

9 months ago

0.1.101

9 months ago

0.1.100

9 months ago

0.1.99

9 months ago

0.1.98

9 months ago

0.1.97

9 months ago

0.1.96

9 months ago

0.1.95

9 months ago

0.1.94

9 months ago

0.1.93

9 months ago

0.1.92

9 months ago

0.1.91

9 months ago

0.1.90

9 months ago

0.1.89

9 months ago

0.1.85

9 months ago

0.1.84

9 months ago

0.1.83

9 months ago

0.1.81

9 months ago

0.1.8

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.0.37

9 months ago

0.0.36

9 months ago

0.0.35

9 months ago

0.0.34

9 months ago

0.0.33

9 months ago

0.0.32

9 months ago

0.0.31

9 months ago

0.0.30

9 months ago

0.0.29

9 months ago

0.0.28

9 months ago

0.0.27

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

9 months ago

0.0.23

9 months ago

0.0.22

9 months ago

0.0.21

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago