@olakai/sdk
Version:
This document demonstrates how to use the Olakai SDK with all its features.
130 lines (129 loc) • 4.95 kB
JavaScript
;
/**
* Helper functions to make monitoring easier and more intuitive
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.olakaiConfig = olakaiConfig;
exports.olakai = olakai;
exports.olakaiMonitor = olakaiMonitor;
exports.olakaiReport = olakaiReport;
const monitor_1 = require("./monitor");
/**
* Event-based configuration function
* @param config - Configuration object
*/
async function olakaiConfig(config) {
const { initClient } = await Promise.resolve().then(() => __importStar(require("./client")));
await initClient(config.apiKey, config.endpoint, {
debug: config.debug || false,
verbose: config.debug || false,
});
}
/**
* Event-based tracking function
* @param eventType - Always 'event'
* @param eventName - Always 'ai_activity'
* @param params - Event parameters
*/
function olakai(eventType, eventName, params) {
// Fire and forget - don't await
olakaiReport(params.prompt, params.response, {
email: params.userEmail,
chatId: params.chatId,
task: params.task,
subTask: params.subTask,
tokens: params.tokens,
requestTime: params.requestTime,
shouldScore: params.shouldScore,
customDimensions: params.customDimensions,
customMetrics: params.customMetrics,
sanitize: false, // Don't sanitize for event-based usage
}).catch((error) => {
// Silent fail for event-based usage
if (typeof console !== "undefined" && console.warn) {
console.warn("[Olakai SDK] Failed to track event:", error);
}
});
}
/**
/**
* Monitor function that automatically captures everything by default and sends the data to the Olakai API
* No type parameters needed - TypeScript will infer them
* @param fn - The function to monitor
* @param options - The eventual options for the monitored function
* @returns The monitored function
*/
function olakaiMonitor(fn, options) {
const monitorOptions = {
...options,
};
return (0, monitor_1.monitor)(monitorOptions)(fn);
}
/**
* Report an AI interaction event directly to Olakai without wrapping a function
* @param prompt - The input/prompt sent to the AI
* @param response - The response received from the AI
* @param options - Optional parameters for the report
* @returns Promise that resolves when the report is sent
*/
async function olakaiReport(prompt, response, options) {
const { sendToAPI, getConfig } = await Promise.resolve().then(() => __importStar(require("./client")));
const { toJsonValue } = await Promise.resolve().then(() => __importStar(require("./utils")));
try {
const config = getConfig();
const payload = {
prompt: toJsonValue(prompt, options?.sanitize),
response: toJsonValue(response, options?.sanitize),
email: options?.email || "anonymous@olakai.ai",
chatId: options?.chatId,
task: options?.task,
subTask: options?.subTask,
tokens: options?.tokens || 0,
requestTime: options?.requestTime || 0,
blocked: false,
sensitivity: [],
shouldScore: options?.shouldScore,
customDimensions: options?.customDimensions,
customMetrics: options?.customMetrics,
};
await sendToAPI(payload, "monitoring");
}
catch (error) {
// Log error but don't throw - reporting should be fail-safe
console.warn("[Olakai SDK] Failed to report event:", error);
}
}
//# sourceMappingURL=helpers.js.map