1.0.25 • Published 9 months ago

@pompeii-labs/magma v1.0.25

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
9 months ago

npm version Discord GitHub stars

🌋 What is Magma?

Magma is a framework that lets you create AI agents without the headache. No complex chains, no confusing abstractions - just write the logic you want your agent to have.

Want to try it out? Chat with Dialog, our user research agent built with Magma!

⚡️ Quick Start

  1. Install Magma:
npm i @pompeii-labs/magma
  1. Create your first agent:
import { MagmaAgent } from "@pompeii-labs/magma";

// That's it! You've got a working agent
const agent = new MagmaAgent();

// Want to give it some personality? Add system prompts:
agent.fetchSystemPrompts = () => [{
    role: "system",
    content: "You are a friendly assistant who loves dad jokes"
}];

// Need the agent to do something? Add tools:
agent.fetchTools = () => [{
    name: "tell_joke",
    description: "Tell a dad joke",
    target: async () => {
        return "Why don't eggs tell jokes? They'd crack up! 🥚";
    }
}];

// Run it:
const reply = await agent.main();
console.log(reply.content);

🔥 Key Features

  • Simple: Build agents in minutes with minimal code
  • Flexible: Use any AI provider (OpenAI, Anthropic, Groq)
  • Powerful: Add tools and middleware when you need them
  • Observable: See exactly what your agent is doing

🚀 MagmaFlow

Want even more power? MagmaFlow gives you instant access to:

  • Voice input/output
  • Streaming responses
  • Tool execution
  • Usage tracking
  • And more!
const agent = new MagmaAgent({
    apiKey: "mf_..." // Get your key at magmaflow.dev
});

🎉 MagmaFlow is currently in private beta! Join the waitlist to get early access.

🛠 Examples

Add Tools

import { MagmaAgent, toolparam } from "@pompeii-labs/magma";

/** Decorate any agent class method with @toolparam or @tool. 
 * @tool is used to define the tool itself
 * @toolparam is used to define the parameters of the tool (key, type, description, required)
 */
class MyAgent extends MagmaAgent {

    @toolparam({ key: 'city', type: 'string' })
    async getWeather({ city }) {
        return `It's sunny in ${city}! 🌞`;
    }
}

Add Middleware

import { MagmaAgent, middleware } from "@pompeii-labs/magma";

/**
 * Decorate any agent class method with @middleware to add custom logging, validation, etc.
 * Types: "preCompletion", "onCompletion", "preToolExecution", "onToolExecution"
 */
class MyAgent extends MagmaAgent {

    @middleware("preCompletion")
    async logBeforeCompletion(message) {
        console.log("About to generate a response!");
    }
}

Use Different Providers

// Use OpenAI (default)
const openai = new MagmaAgent();

// Use Anthropic
const claude = new MagmaAgent({
    providerConfig: {
        provider: "anthropic",
        model: "claude-3.5-sonnet-20240620"
    }
});

// Use Groq
const groq = new MagmaAgent({
    providerConfig: {
        provider: "groq",
        model: "llama-3.1-70b-versatile"
    }
});

Core Methods

import { MagmaAgent } from "@pompeii-labs/magma";

class MyAgent extends MagmaAgent {
    // Initialize your agent
    async setup() {
        // Load resources, connect to databases, etc.
        await this.loadDatabase();
        return "I'm ready to help!";
    }

    // Handle incoming messages
    async receive(message: any) {
        // Process user input before main() is called
        if (message.type === 'image') {
            await this.processImage(message.content);
        }
    }

    // Clean up resources
    async cleanup();

    // Manually trigger a specific tool
    async trigger({ name: "get_weather" });

    // Stop the current execution
    kill();
}

Event Handlers

import { MagmaAgent } from "@pompeii-labs/magma";

class MyAgent extends MagmaAgent {
    // Handle agent shutdown
    async onCleanup() {
        console.log("Agent shutting down...");
    }

    // Handle errors
    async onError(error: Error) {
        console.error("Something went wrong:", error);
        await this.notifyAdmin(error);
    }

    // Track token usage
    async onUsageUpdate(usage: MagmaUsage) {
        await this.saveUsageMetrics(usage);
    }

    // Process streaming responses
    async onStreamChunk(chunk: MagmaStreamChunk) {
        console.log("Received chunk:", chunk.content);
    }

    // MagmaFlow Handlers
    async onConnect() {
        console.log("Connected to MagmaFlow!");
    }

    // Handle agent disconnection from MagmaFlow
    async onDisconnect() {
        console.log("Disconnected from MagmaFlow");
    }

    // Handle incoming audio chunks
    async onAudioChunk(chunk: Buffer) {
        // Process incoming audio
        await this.processAudioChunk(chunk);
    }

    // Handle audio stream completion
    async onAudioCommit() {
        // Audio stream complete
        await this.finalizeAudioProcessing();
    }

    // Handle request abortion
    async onAbort() {
        await this.cleanup();
    }
}

📚 Want More?

📝 License

Magma is Apache 2.0 licensed.

1.0.22

9 months ago

1.0.21

9 months ago

1.0.25

9 months ago

1.0.24

9 months ago

1.0.23

9 months ago

1.0.19

9 months ago

1.0.20

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.16

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago