Socket
Book a DemoInstallSign in
Socket

@agentai/agentai

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@agentai/agentai

A JavaScript client for the Agent.ai API

0.1.1
latest
Source
npmnpm
Version published
Weekly downloads
3
Maintainers
0
Weekly downloads
 
Created
Source

Agent.ai JavaScript Library

npm version License: MIT

A modern JavaScript library for interacting with the Agent.ai Actions API.

Installation

# Using npm
npm install @agentai/agentai

# Using yarn
yarn add @agentai/agentai

# Using pnpm
pnpm add @agentai/agentai

Example Usage

Before you can start using the library, you'll need to sign up for an account at Agent.ai and obtain a Bearer token from https://agent.ai/user/settings#credits.

JavaScript

import AgentAi from '@agentai/agentai';
// Using CommonJS
#const { AgentAiClient } = require('@agentai/agentai');

// Create a client instance with your bearer token
const client = new AgentAiClient('YOUR_BEARER_TOKEN_HERE');

// Example: Grab web text
async function fetchWebText() {
  try {
    const webTextResponse = await client.action(
      'grabWebText',
      { url: 'https://agent.ai' }
    );

    if (webTextResponse.status === 200) {
      console.log("Web Text Response Status:", webTextResponse.status);
      console.log("First 100 chars of Response:", webTextResponse.results.substring(0, 100) + "...");
    } else {
      console.error(`Error: Status Code: ${webTextResponse.status}, Message: ${webTextResponse.error}`);
    }
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

// Example: Chat with an LLM
async function chatWithLlm() {
  try {
    const chatResponse = await client.chat(
      "What is an AI agent?", 
      { model: "gpt4o" }
    );

    if (chatResponse.status === 200) {
      console.log("Chat Response:", chatResponse.results);
    } else {
      console.error(`Error: ${chatResponse.error}`);
    }
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

// Example: Get Google News
async function getGoogleNews() {
  try {
    const googleNewsResponse = await client.action(
      'getGoogleNews',
      {
        query: "AI advancements",
        date_range: "7d",
        location: "Boston"
      }
    );

    if (googleNewsResponse.status === 200) {
      console.log("Google News Location:", googleNewsResponse.metadata.search_information.location_used);
      console.log("Number of articles:", googleNewsResponse.results.length);
    } else {
      console.error(`Error: ${googleNewsResponse.error}`);
    }
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

// Run the examples
fetchWebText();
chatWithLlm();
getGoogleNews();

API Reference

Creating a Client

// Method 1: Using API key only
const client = new AgentAiClient('YOUR_API_KEY');

// Method 2: Using API key with config object
const client = new AgentAiClient('YOUR_API_KEY', {
  timeout: 60000, // Optional: custom timeout in ms
  baseUrl: 'https://custom-url.com', // Optional: custom base URL
  headers: {
    'X-Custom-Header': 'value' // Optional: additional headers
  }
});

Methods

client.action(actionId, params)

Execute an AI action by its ID.

  • actionId (string): The ID of the action to execute (e.g., 'grabWebText')
  • params (object): Parameters for the action
  • Returns: Promise

client.chat(prompt, options)

Use the invokeLlm action to generate text based on a prompt.

  • prompt (string): The text prompt for the LLM
  • options (object, optional):
    • model (string, default: 'gpt4o'): LLM model to use
    • Additional parameters for the invokeLlm action
  • Returns: Promise

Response Structure

All methods return a Promise that resolves to an object with the following structure:

{
  status: 200,           // HTTP status code
  error: null,           // Error message (if any)
  results: {...},        // API response data (if successful)
  metadata: {...}        // Metadata from the API response (if available)
}

Error Handling

All methods return Promises that may reject with errors. It's recommended to use try/catch blocks when calling these methods:

try {
  const response = await client.action('grabWebText', { url: 'https://agent.ai' });
  if (response.status !== 200) {
    console.error(`API Error: ${response.error}`);
  }
} catch (error) {
  console.error('Network or client error:', error);
}

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues for feature requests or bug reports.

License

This project is licensed under the MIT License.

Keywords

agent.ai

FAQs

Package last updated on 07 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.