npm package: @koreai/agentai-sdk
Description: AgentAI SDK - Universal JavaScript SDK for Node.js and Browser
Author: Kore
Latest version: 1.0.1

# @koreai/agentai-sdk

A Node.js and browser SDK for AgentAI with event-based messaging and socket communication.

## Installation

You can use the AgentAI SDK in two ways:

### 1. NPM package

Install the package from npm:

```bash
npm install @koreai/agentai-sdk
```

### 2. CDN (browser)

Load the SDK from the published CDN in your HTML:

```html
<script src="https://cdn.jsdelivr.net/npm/@koreai/agentai-sdk@1.0.0/dist/agentai-sdk.min.js"></script>
```

The SDK is exposed as a global `AgentAISDK` when loaded via script tag.

---

## Importing the AgentAI class

There are two ways to import the `AgentAI` class, depending on your environment.

### Node.js (CommonJS)

```javascript
const { AgentAI } = require("@koreai/agentai-sdk");
```

### ES modules (Node or bundlers)

```javascript
import { AgentAI } from "@koreai/agentai-sdk";
```

When using the CDN script tag, use the global instead of importing:

```javascript
const { AgentAI } = AgentAISDK;
```

---

## Quick Start

```javascript
const client = new AgentAI({
  connection: {
    domainURL: "https://your-agentassist-server.com",
    botId: "your-bot-id",
    conversationId: "your-conversation-id",
    token: "your-jwt-token",
    interactiveLanguage: "en",
    customData: {},
    channel: "voice", // or 'chat'
    sessionId: "", // session id from platform of self service
  },
});

// Wait for connection to be ready
client.on("agentai_ready", (event) => {
  console.log("AgentAI is ready!", event);
  // You can start interacting with the SDK from now!
});
```

## Configuration

### Required Connection Parameters

| Parameter           | Type   | Description                             | Field Type |
| ------------------- | ------ | --------------------------------------- | ---------- |
| domainURL           | string | AgentAssist server URL                  | Required   |
| botId               | string | Bot identifier                          | Required   |
| conversationId      | string | Conversation identifier                 | Required   |
| token               | string | JWT authentication token                | Required   |
| interactiveLanguage | string | Language code (e.g., 'en')              | Required   |
| channel             | string | Channel type ('voice', 'chat', 'email') | Required   |
| customData          | object | Custom data object                      | Optional   |
| sessionId           | string | Kore's Customer Bot Interaction Id      | Optional   |

## API

### Methods

#### `sendAgentMessage(payload)`

Send a message as an agent. Returns `true` if sent successfully, `false` otherwise.

```javascript
const success = client.sendAgentMessage({
  message: "Hello!",
});
```

#### `sendUserMessage(payload)`

Send a message as a user. Returns `true` if sent successfully, `false` otherwise.

```javascript
const success = client.sendUserMessage({
  message: "Hello!",
});
```

#### `internalTransfer(payload)`

Call this method when internal agent transfer happens. Returns `true` on success, `false` otherwise.

```javascript
const transferData = {
  transfertype: "NA", //warm, //cold
  transition: "entry", //exit
  acceptedAt: "Agent join timestamp (UTC)"
  participant: {
    identity: "a-123xxxxx",
    name: "a-xxxxxx",
    type: "agent",
  },
};
const success = client.internalTransfer(transferData);
```

#### `disconnect()`

Disconnect from the socket server.

```javascript
client.disconnect();
```

#### `isConnected()`

Check if the client is currently connected.

```javascript
if (client.isConnected()) {
  // Socket is connected
}
```

#### `generateIntermSummary()`

Call this method to generate an intermittent summary in AgentAI. It returns `true` on success and `false` otherwise. To use this feature, the Generate Summary setting must be enabled.

```javascript
client.generateIntermSummary();
```

#### `endOfConversation()`

call this method to end the conversation between the user and the agent. The method returns `true` if the operation is successful, and `false` otherwise. After the method is called, the final conversation summary will be received if the conversation summary feature is enabled.

```javascript
client.endOfConversation();
```

### Events

| Event                        | Description                                                   |
| ---------------------------- | ------------------------------------------------------------- |
| `agentai_ready`              | Emitted when the connection is ready to send/receive messages |
| `agent_message_ack`          | Acknowledgment when agent message is saved                    |
| `user_message_ack`           | Acknowledgment when user message is saved                     |
| `error`                      | Emitted for all errors (validation, connection, etc.)         |
| `connect`                    | Socket connected                                              |
| `disconnect`                 | Socket disconnected                                           |
| `connect_error`              | Connection error occurred                                     |
| `reconnect`                  | Successfully reconnected                                      |
| `reconnect_attempt`          | Attempting to reconnect                                       |
| `internal_transfer_response` | Response for internal transfer event                          |
| `interm_summary_response`    | Intermittent summary response for the request                 |
| `interm_summary_event_ack`   | Acknowledgement for intermittent summary generation           |

## Error Handling

All errors are emitted as events instead of being thrown. Always listen to the `'error'` event:

```javascript
client.on("error", (event) => {
  console.error("Error:", event.payload.message);
});
```

## License

MIT