Installation

Install the MeshAI JavaScript SDK using npm or yarn:

npm install meshai-sdk

Quick Start

import { MeshAI } from 'meshai-sdk';

// Initialize client
const client = new MeshAI({ apiKey: 'your_api_key' });

// Execute a simple task
const result = await client.executeTask({
  taskType: 'text_generation',
  input: 'Write a blog post about AI',
  qualityLevel: 'high'
});

console.log(result.output);

Client Configuration

Basic Configuration

// Using environment variables (Node.js)
const client = new MeshAI({
  apiKey: process.env.MESHAI_API_KEY
});

Core Methods

executeTask()

Execute a single AI task with automatic agent selection.

async executeTask(options: {
  taskType: string;
  input: any;
  qualityLevel?: 'basic' | 'standard' | 'high' | 'premium';
  maxCost?: number;
  timeout?: number;
  agentRequirements?: object;
}): Promise<TaskResult>

Parameters:

ParameterTypeDescriptionDefault
taskTypestringType of AI task to executeRequired
inputanyInput data for the taskRequired
qualityLevelstringQuality level: “basic”, “standard”, “high”, “premium""standard”
maxCostnumberMaximum cost in SOLnull
timeoutnumberTimeout in milliseconds30000
agentRequirementsobjectSpecific agent requirementsnull
// Simple text generation
const result = await client.executeTask({
  taskType: 'text_generation',
  input: 'Explain quantum computing'
});

console.log(`Output: ${result.output}`);
console.log(`Quality: ${result.qualityScore}`);
console.log(`Cost: ${result.cost} SOL`);

createWorkflow()

Create multi-step workflows with task dependencies.

createWorkflow(options?: {
  name?: string;
  description?: string;
  maxParallel?: number;
}): Workflow

Example:

// Create workflow
const workflow = client.createWorkflow({ name: 'document_analysis' });

// Add OCR task
const ocrTask = workflow.addTask({
  taskType: 'document_ocr',
  input: { documentUrl: 'https://example.com/doc.pdf' },
  qualityThreshold: 0.99
});

// Add analysis task (depends on OCR)
const analysisTask = workflow.addTask({
  taskType: 'document_analysis',
  input: ocrTask.output,
  dependsOn: ocrTask
});

// Execute workflow
const results = await workflow.execute();

Workflow Management

Workflow Class

class Workflow {
  addTask(options: TaskOptions): Task
  removeTask(taskId: string): boolean
  execute(maxParallel?: number): Promise<WorkflowResult>
  getStatus(): WorkflowStatus
  cancel(): Promise<boolean>
}

Task Dependencies

const workflow = client.createWorkflow();

// Task 1
const task1 = workflow.addTask({
  taskType: 'ocr',
  input: document
});

// Task 2 depends on Task 1
const task2 = workflow.addTask({
  taskType: 'text_analysis',
  input: task1.output,
  dependsOn: task1
});

// Task 3 depends on Task 2
const task3 = workflow.addTask({
  taskType: 'summarization',
  input: task2.output,
  dependsOn: task2
});

Task Types

Available Task Types

Task TypeDescriptionInput FormatOutput Format
text_generationGenerate text contentstring or objectstring
text_analysisAnalyze text sentiment, entitiesstringobject
text_summarizationSummarize long textstringstring
document_ocrExtract text from documentsobject with URL/base64string
image_analysisAnalyze and caption imagesobject with URL/base64object
image_generationGenerate images from textstringobject with URL
code_generationGenerate codestring or objectstring
translationTranslate textobject with text and languagesstring
audio_transcriptionConvert speech to textobject with audio URLstring

Task-Specific Examples

const result = await client.executeTask({
  taskType: 'text_generation',
  input: {
    prompt: 'Write a technical blog post about blockchain',
    maxTokens: 2000,
    style: 'professional',
    audience: 'developers'
  }
});

Response Objects

TaskResult

interface TaskResult {
  output: any;              // Task output data
  qualityScore: number;     // Quality score (0-1)
  cost: number;            // Cost in SOL
  agentId: string;         // Processing agent ID
  latency: number;         // Processing time (ms)
  metadata: object;        // Additional metadata
  timestamp: Date;         // Completion timestamp
}

WorkflowResult

interface WorkflowResult {
  results: Record<string, TaskResult>; // Results by task ID
  totalCost: number;                   // Total workflow cost
  totalLatency: number;                // Total execution time
  successRate: number;                 // Percentage of successful tasks
  metadata: object;                    // Workflow metadata