Skip to content

Commit

Permalink
mixpanel-enrich & chat sync
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-did authored Feb 6, 2025
2 parents 36b785d + ad76d70 commit cbe0c72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/createAgentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ async function newChat(

return newChat;
} catch (error: any) {
try {
console.error(error);
const parsedError = JSON.parse(error.message);
let parsedError;

if (parsedError?.kind === 'InsufficientCreditsError') {
throw new Error('InsufficientCreditsError');
}
try {
parsedError = JSON.parse(error.message);
} catch (jsonError) {
console.error('Error parsing the error message:', jsonError);
}

if (parsedError?.kind === 'InsufficientCreditsError') {
throw new Error('InsufficientCreditsError');
}

throw new Error('Cannot create new chat');
}
}
Expand Down Expand Up @@ -122,12 +123,12 @@ function initializeStreamAndChat(
outerReject(error);
};

let chatPromise;

if (!chat && options.mode !== ChatMode.DirectPlayback) {
chatPromise = newChat(agent.id, agentsApi, analytics, options.mode, options.persistentChat).catch(e => {
reject(e);
});
try {
chat = await newChat(agent.id, agentsApi, analytics, options.mode, options.persistentChat);
} catch (error) {
return reject(error);
}
}

const streamingManager = await createStreamingManager(
Expand All @@ -141,9 +142,6 @@ function initializeStreamAndChat(
...options.callbacks,
onConnectionStateChange: async state => {
if (state === ConnectionState.Connected) {
if (chatPromise) {
chat = await chatPromise;
}
if (streamingManager) {
options.callbacks.onConnectionStateChange?.(state);
resolve({ chat, streamingManager });
Expand Down Expand Up @@ -424,6 +422,7 @@ export async function createAgentManager(agent: string, options: AgentManagerOpt
starterMessages: agentInstance.knowledge?.starter_message || [],
getSTTToken: () => agentsApi.getSTTToken(agentInstance.id),
changeMode,
enrichAnalytics: analytics.enrich,
async connect() {
await connect(true);

Expand Down
19 changes: 19 additions & 0 deletions src/services/mixpanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Analytics {
getRandom(): string;
track(event: string, props?: Record<string, any>): Promise<any>;
linkTrack(mixpanelEvent: string, props: Record<string, any>, event: string, dependencies: string[]): any;
enrich(props: Record<string, any>): void;
additionalProperties: Record<string, any>;
}

interface MixpanelEvent {
Expand Down Expand Up @@ -47,8 +49,24 @@ export function initializeAnalytics(config: AnalyticsOptions): Analytics {

return {
...analyticProps,
additionalProperties: {},
isEnabled: config.isEnabled ?? true,
getRandom: () => Math.random().toString(16).slice(2),
enrich(properties: Record<string, any>) {
const props = {};

if (properties && typeof properties !== 'object') {
throw new Error('properties must be a flat json object');
}

for (let prop in properties) {
if (typeof properties[prop] === 'string' || typeof properties[prop] === 'number') {
props[prop] = properties[prop];
}
}

this.additionalProperties = { ...this.additionalProperties, ...props };
},
track(event: string, props?: Record<string, any>) {
if (!this.isEnabled) {
return Promise.resolve();
Expand All @@ -66,6 +84,7 @@ export function initializeAnalytics(config: AnalyticsOptions): Analytics {
{
event,
properties: {
...this.additionalProperties,
...sendProps,
...analyticProps,
source,
Expand Down
6 changes: 6 additions & 0 deletions src/types/entities/agents/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@ export interface AgentManager {
* @param mode - ChatMode
*/
changeMode(mode: ChatMode): void;

/**
* Method to enrich analytics properties
* @param properties flat json object with properties that will be added to analytics events fired from the sdk
*/
enrichAnalytics: (properties: Record<string, any>) => void;
}

0 comments on commit cbe0c72

Please sign in to comment.