forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.ts
106 lines (91 loc) · 2.97 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { AVATAR_IMAGE_MAX_SIZE } from '@/constants';
import type { UUID } from '@elizaos/core';
import { type ClassValue, clsx } from 'clsx';
import dayjs from 'dayjs';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import { twMerge } from 'tailwind-merge';
/**
* Combines multiple class names into a single string.
* * @param {...ClassValue} inputs - Array of class names to be combined.
* @returns { string } - Combined class names as a single string.
*/
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
dayjs.extend(localizedFormat);
export const moment = dayjs;
export const formatAgentName = (name: string) => {
return name.substring(0, 2);
};
/**
* Converts a character name to a URL-friendly format by replacing spaces with hyphens
*/
/**
* Converts a character name to a URL-friendly format by replacing spaces with hyphens.
*
* @param {string} name - The name of the character to convert.
* @returns {string} The URL-friendly version of the character name.
*/
export function characterNameToUrl(name: string): string {
return name.replace(/\s+/g, '-');
}
/**
* Converts a URL-friendly character name back to its original format by replacing hyphens with spaces
*/
export function urlToCharacterName(urlName: string): string {
return urlName.replace(/-+/g, ' ');
}
// crypto.randomUUID only works in https context in firefox
export function randomUUID(): UUID {
return URL.createObjectURL(new Blob()).split('/').pop() as UUID;
}
export function getEntityId(): UUID {
const USER_ID_KEY = 'elizaos-client-user-id';
const existingUserId = localStorage.getItem(USER_ID_KEY);
if (existingUserId) {
return existingUserId as UUID;
}
const newUserId = randomUUID() as UUID;
localStorage.setItem(USER_ID_KEY, newUserId);
return newUserId;
}
export const compressImage = (
file: File,
maxSize = AVATAR_IMAGE_MAX_SIZE,
quality = 0.8
): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
if (e.target?.result) {
const img = new Image();
img.src = e.target.result as string;
img.onload = () => {
const canvas = document.createElement('canvas');
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxSize) {
height *= maxSize / width;
width = maxSize;
}
} else {
if (height > maxSize) {
width *= maxSize / height;
height = maxSize;
}
}
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx?.drawImage(img, 0, 0, width, height);
const resizedBase64 = canvas.toDataURL('image/jpeg', quality);
resolve(resizedBase64);
};
img.onerror = reject;
}
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
};