# @browserstack/ai-sdk

A TypeScript/NodeJS SDK for AI application observability, testing, and evaluation.

> **Requires Node.js >= 18.**

## Installation

```bash
npm install @browserstack/ai-sdk@beta
```

## Quick Start

```typescript
import { AISDK } from '@browserstack/ai-sdk';

// Initialize the client
const client = new AISDK({
  publicKey: 'your-public-key',
  secretKey: 'your-secret-key',
});

// Create a trace
const trace = client.trace({
  name: 'user_login_flow',
  metadata: {
    testSuite: 'authentication_tests',
    testFile: 'auth.test.js',
  },
  tags: ['auth', 'critical'],
});

// Create spans
const loginSpan = trace.span({
  name: 'navigate_to_login',
  input: { url: '/login' }
});

loginSpan.end({
  output: { pageLoaded: true, loadTime: 250 }
});

// Create events
trace.event({
  name: 'assertion_check',
  metadata: {
    assertion: 'Login button should be visible',
    passed: true,
    expected: 'button visible',
    actual: 'button visible'
  }
});

// Cleanup
await client.flushAsync();
await client.shutdownAsync();
```

## Auto-Instrumentation

Automatically trace LLM calls without code changes:

```bash
node --require @browserstack/ai-sdk/instrument app.js
```

## Configuration

The client can be configured using environment variables or constructor options:

### Environment Variables

```bash
AISDK_PUBLIC_KEY=your_public_key
AISDK_SECRET_KEY=your_secret_key
```

### Constructor Options

```typescript
const client = new AISDK({
  publicKey: 'your_public_key',
  secretKey: 'your_secret_key',
  environment: 'production',
  flushAt: 20,
  flushInterval: 10000,
});
```
