add mcId
This commit is contained in:
parent
e023e5bc44
commit
ee659d9e13
2 changed files with 85 additions and 16 deletions
|
@ -12,6 +12,7 @@ import { getSuggestions } from '@/lib/actions';
|
|||
import {Settings} from 'lucide-react';
|
||||
import SettingsDialog from './SettingsDialog';
|
||||
import NextError from 'next/error';
|
||||
import {Mcid} from "@/lib/mcid";
|
||||
|
||||
export type Message = {
|
||||
messageId: string;
|
||||
|
@ -385,7 +386,7 @@ const loadMessages = async (
|
|||
const ChatWindow = ({id}: { id?: string }) => {
|
||||
const searchParams = useSearchParams();
|
||||
const initialMessage = searchParams.get('q');
|
||||
|
||||
const [userId, setUserId] = useState<string | undefined>();
|
||||
const [chatId, setChatId] = useState<string | undefined>(id);
|
||||
const [newChatCreated, setNewChatCreated] = useState(false);
|
||||
|
||||
|
@ -417,6 +418,36 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
|||
|
||||
const [isSettingsOpen, setIsSettingsOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const initializeUserId = () => {
|
||||
try {
|
||||
// 从 localStorage 读取现有用户 ID
|
||||
const storedUserId = localStorage.getItem('userId');
|
||||
|
||||
if (storedUserId) {
|
||||
setUserId(storedUserId);
|
||||
console.debug('Using existing user ID:', storedUserId);
|
||||
} else {
|
||||
// 生成新的 MCID
|
||||
const newUserId = new Mcid().generate().toString(); // 转换为字符串
|
||||
|
||||
// 存储到 localStorage
|
||||
localStorage.setItem('userId', newUserId);
|
||||
setUserId(newUserId);
|
||||
console.debug('Generated new user ID:', newUserId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error initializing user ID:', error);
|
||||
// 生成随机 ID 作为 fallback
|
||||
const fallbackId = crypto.randomBytes(20).toString('hex');
|
||||
localStorage.setItem('userId', fallbackId);
|
||||
setUserId(fallbackId);
|
||||
}
|
||||
};
|
||||
|
||||
initializeUserId();
|
||||
}, []); // 空依赖数组确保只运行一次
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
chatId &&
|
||||
|
@ -465,7 +496,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
|||
} else {
|
||||
setIsReady(false);
|
||||
}
|
||||
}, [isMessagesLoaded, isWSReady]);
|
||||
}, [isMessagesLoaded, isWSReady, userId]);
|
||||
|
||||
const sendMessage = async (message: string, messageId?: string) => {
|
||||
if (loading) return;
|
||||
|
|
38
ui/lib/mcid.ts
Normal file
38
ui/lib/mcid.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
// utils/mcid.ts
|
||||
export class Mcid {
|
||||
private lastTimestamp = -1;
|
||||
private sequence = 0;
|
||||
|
||||
constructor(private readonly machineId: number = 0) {
|
||||
if (machineId < 0 || machineId > 255) {
|
||||
throw new Error('Machine ID must be between 0 and 255');
|
||||
}
|
||||
}
|
||||
|
||||
generate(): number {
|
||||
let now = Date.now();
|
||||
|
||||
if (now < this.lastTimestamp) {
|
||||
throw new Error('Clock moved backwards');
|
||||
}
|
||||
|
||||
if (now === this.lastTimestamp) {
|
||||
this.sequence = (this.sequence + 1) & 0xf;
|
||||
if (this.sequence === 0) {
|
||||
while (now <= this.lastTimestamp) {
|
||||
now = Date.now();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.sequence = 0;
|
||||
}
|
||||
|
||||
this.lastTimestamp = now;
|
||||
|
||||
return (
|
||||
(now * 0x1000) + // 时间戳左移 12 位
|
||||
(this.machineId * 16) + // 机器 ID 左移 4 位
|
||||
this.sequence // 序列号
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue