Skip to content

Commit

Permalink
ensure uniqueness of the auto generated end user ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoudz committed Apr 23, 2024
1 parent 114059e commit 03b3b86
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 40 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,22 @@ const aiFunctions = [
];
```

For functions that accepts parameters:
Register the functions using `registerFunctions([...]);` inside a `useEffect` hook.

```js
const { registerFunctions } = useAiAssistant();

useEffect(() => {
if (registerFunctions) {
registerFunctions(aiFunctions);
}

}, [registerFunctions]);
```

> Just like that, your app is voice-interactive. Magic! :sparkles:
For functions that accepts parameters: simply describe the parameters

```js
const sayHello = (name) => {
Expand Down Expand Up @@ -153,22 +168,7 @@ const aiFunctions = [
];
```

Register the functions using `registerFunctions([...]);` inside a `useEffect` hook.

```js
const { registerFunctions } = useAiAssistant();

useEffect(() => {
if (registerFunctions) {
registerFunctions(voiceControlledFunctions);
}

}, [registerFunctions]);
```

> Just like that, your app is voice-interactive. Magic! :sparkles:
To customize the AI assistant's voice or feed information about your product, visit the [Admin Panel](https://admin.sista.ai/applications).
To customize the AI personality or voice or provide extra information, beyound what's on the user screen, visit the [Admin Panel](https://admin.sista.ai/applications).

## Full Example: (Todo App)

Expand Down
30 changes: 7 additions & 23 deletions src/core/AiAssistantEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import Logger from './Logger';
import Scraper from './Scraper';
import config from './config';
import { VoiceFunction } from './commonTypes';

interface EndUserDetails {
endUserAgent: string;
generatedEndUserId: string;
providedEndUserId: string | null;
}
import User from './User';

interface ApiResponse {
warningMessage?: string;
Expand All @@ -27,13 +22,13 @@ interface ApiResponse {
class AiAssistantEngine extends EventEmitter {
private readonly apiKey: string;
private readonly apiUrl: string;
private readonly userId: string | null;
private readonly scrapeContent: boolean;
private readonly sdkVersion: string;
private readonly audioPlayer: AudioPlayer;
private readonly audioRecorder: AudioRecorder;
private readonly functionExecutor: FunctionExecutor;
private readonly scraper: Scraper;
private readonly user: User;
private readonly pageContent: Record<string, string[]> | null;

constructor(
Expand Down Expand Up @@ -64,13 +59,13 @@ class AiAssistantEngine extends EventEmitter {
);
this.apiUrl = apiUrl;
Logger.log('--[SISTA]-- Using Base URL:', this.apiUrl);
this.userId = userId;

this.audioPlayer = new AudioPlayer();
this.audioRecorder = new AudioRecorder();
this.functionExecutor = new FunctionExecutor();
this.scraper = new Scraper();
this.pageContent = this.scrapeContent ? this.scraper.getText() : null;
this.user = new User(userId);
}

registerFunctions(voiceFunctions: VoiceFunction[]): void {
Expand Down Expand Up @@ -110,7 +105,10 @@ class AiAssistantEngine extends EventEmitter {

const formData = new FormData();
formData.append('sdkVersion', this.sdkVersion);
formData.append('endUser', JSON.stringify(this._getEndUserDetails()));
formData.append(
'endUser',
JSON.stringify(this.user.getEndUserDetails()),
);
formData.append('audio', audioBlob);
formData.append(
'functionsSignatures',
Expand Down Expand Up @@ -138,20 +136,6 @@ class AiAssistantEngine extends EventEmitter {
}
};

private _getEndUserDetails(): EndUserDetails {
let endUserId = localStorage.getItem('endUserId');
if (!endUserId) {
endUserId = Math.random().toString(36).substring(2);
localStorage.setItem('endUserId', endUserId);
}

return {
endUserAgent: navigator.userAgent,
generatedEndUserId: endUserId,
providedEndUserId: this.userId,
};
}

private _handleApiResponse = (response: ApiResponse): void => {
Logger.log('--[SISTA]-- _handleApiResponse:', response);

Expand Down
37 changes: 37 additions & 0 deletions src/core/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// src/core/User.ts

interface EndUserDetails {
endUserAgent: string;
generatedEndUserId: string;
providedEndUserId: string | null;
}

class User {
private providedUserId: string | null;

constructor(providedUserId: string | null) {
this.providedUserId = providedUserId;
}

public getEndUserDetails(): EndUserDetails {
return {
endUserAgent: navigator.userAgent,
generatedEndUserId: this._generateEndUserId(),
providedEndUserId: this.providedUserId,
};
}

private _generateEndUserId(): string {
let endUserId = localStorage.getItem('generatedEndUserId');
if (!endUserId) {
const timestamp = new Date().getTime();
const randomPart = Math.random().toString(36).substring(2);
endUserId = `${timestamp}-${randomPart}`;
localStorage.setItem('generatedEndUserId', endUserId);
}

return endUserId;
}
}

export default User;

0 comments on commit 03b3b86

Please sign in to comment.