Client

Initialize and configure the CognitiveX client for your application.

Basic Usage

client.tstypescript
import { CognitiveXClient } from '@cognitivex/sdk';

const client = new CognitiveXClient({
  apiKey: process.env.COGNITIVEX_API_KEY
});

Configuration Options

OptionTypeRequiredDescription
apiKeystringYesYour API key
baseUrlstringNoCustom API URL (default: https://api.cognitivex.ai)
timeoutnumberNoRequest timeout in ms (default: 30000)
maxRetriesnumberNoMax retry attempts (default: 3)
defaultModelstringNoDefault LLM model
typescript
const client = new CognitiveXClient({
  apiKey: process.env.COGNITIVEX_API_KEY,
  baseUrl: 'https://api.cognitivex.ai',
  timeout: 60000, // 60 seconds
  maxRetries: 5,
  defaultModel: 'gpt-4'
});

Advanced Configuration

Custom Headers

typescript
const client = new CognitiveXClient({
  apiKey: process.env.COGNITIVEX_API_KEY,
  headers: {
    'X-Organization-ID': 'org_123',
    'X-Environment': 'production'
  }
});

Retry Strategy

typescript
const client = new CognitiveXClient({
  apiKey: process.env.COGNITIVEX_API_KEY,
  retryStrategy: {
    maxRetries: 5,
    initialDelay: 1000, // ms
    maxDelay: 10000,    // ms
    backoffMultiplier: 2
  }
});

Request Interceptors

typescript
client.interceptors.request.use((config) => {
  // Add custom logic before request
  console.log('Request:', config);
  return config;
});

client.interceptors.response.use(
  (response) => {
    // Handle successful response
    return response;
  },
  (error) => {
    // Handle error
    console.error('Error:', error);
    return Promise.reject(error);
  }
);

Environment Variables

Recommended environment variable setup:

.envbash
# Required
COGNITIVEX_API_KEY=your_api_key_here

# Optional
COGNITIVEX_BASE_URL=https://api.cognitivex.ai
COGNITIVEX_DEFAULT_MODEL=gpt-4
COGNITIVEX_TIMEOUT=60000
COGNITIVEX_MAX_RETRIES=3

Client Methods

client.memory

Access Memory Layer operations (store, retrieve, search, delete)

client.cognition

Access Cognition Layer operations (reason, reflect, patterns)

client.decision

Access Decision Layer operations (route, coordinate, optimize)

Error Handling

typescript
try {
  const result = await client.memory.store({
    content: "Your content"
  });
} catch (error) {
  if (error.code === 'AUTHENTICATION_ERROR') {
    console.error('Invalid API key');
  } else if (error.code === 'RATE_LIMIT_EXCEEDED') {
    console.error('Rate limit exceeded');
  } else {
    console.error('Unknown error:', error);
  }
}