Installation
Package Manager
npm install @egain/ai-agent-sdkyarn add @egain/ai-agent-sdkpnpm add @egain/ai-agent-sdkGitHub Packages Configuration
This package is published to GitHub Packages. Configure your .npmrc:
@egain:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKENGenerating a GitHub Token
- Go to GitHub → Settings → Developer settings → Personal access tokens
- Generate a new token with
read:packagesscope - Replace
YOUR_GITHUB_TOKENwith your token
Environment-Specific Setup
Browser (ES Modules)
import { AiAgent } from "@egain/ai-agent-sdk";
const agent = new AiAgent({
id: "agent-id",
endpoint: "https://your-endpoint.com",
auth: { type: "pre-auth", accessToken: "your-access-token" }
});Browser (UMD / Script Tag)
<script src="https://unpkg.com/@egain/ai-agent-sdk/dist/browser.js"></script>
<script>
const agent = new eGain.AiAgent({
id: "agent-id",
endpoint: "https://your-endpoint.com",
auth: { type: "pre-auth", accessToken: "your-access-token" }
});
</script>Node.js
Node.js requires the ws package for WebSocket support. The SDK automatically loads it when running in Node.js:
npm install wsimport { AiAgent } from "@egain/ai-agent-sdk";
const agent = new AiAgent({
id: "agent-id",
endpoint: "https://your-endpoint.com",
auth: { type: "pre-auth", accessToken: "your-access-token" }
});Automatic Polyfill
The SDK automatically detects Node.js and loads the ws package. No manual setup is required in most cases.
TypeScript Configuration
The SDK includes TypeScript definitions. For optimal support, ensure your tsconfig.json includes:
{
"compilerOptions": {
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true
}
}Verifying Installation
import { AiAgent, ConnectionState } from "@egain/ai-agent-sdk";
// Check types are working
const config: Parameters<typeof AiAgent>[0] = {
id: "test",
endpoint: "https://example.com",
auth: { type: "anonymous" }
};
console.log("SDK installed successfully!");Troubleshooting
Module Resolution Errors
If you see module resolution errors, ensure your bundler supports ES modules:
export default {
optimizeDeps: {
include: ['@egain/ai-agent-sdk']
}
}module.exports = {
resolve: {
mainFields: ['module', 'main']
}
}WebSocket Errors in Node.js
If you see WebSocket is not defined, ensure the ws package is installed:
npm install wsThe SDK automatically loads the ws package. If auto-loading fails in your environment, you can set it up manually:
import WebSocket from "ws";
(global as any).WebSocket = WebSocket;CORS Errors in Browser
Ensure your endpoint allows requests from your origin. Contact your eGain administrator to configure CORS headers.