) : (
{messages.length > 0 ? (
<>
-
+
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 // 序列号
+ );
+ }
+}
From 1a8f59e92b4b0d20806e9aae3f4f8c934608ecbd Mon Sep 17 00:00:00 2001
From: litongjava
Date: Sun, 2 Feb 2025 15:38:24 -1000
Subject: [PATCH 3/6] add userId
---
ui/components/ChatWindow.tsx | 8 ++++++--
ui/components/EmptyChat.tsx | 6 ++++++
ui/components/EmptyChatMessageInput.tsx | 6 +++++-
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx
index f37375c..2e0505c 100644
--- a/ui/components/ChatWindow.tsx
+++ b/ui/components/ChatWindow.tsx
@@ -411,6 +411,7 @@ const ChatWindow = ({id}: { id?: string }) => {
const [focusMode, setFocusMode] = useState('webSearch');
const [optimizationMode, setOptimizationMode] = useState('speed');
+ const [copilotEnabled, setCopilotEnabled] = useState(true);
const [isMessagesLoaded, setIsMessagesLoaded] = useState(false);
@@ -468,7 +469,7 @@ const ChatWindow = ({id}: { id?: string }) => {
} else if (!chatId) {
setNewChatCreated(true);
setIsMessagesLoaded(true);
- setChatId(crypto.randomBytes(20).toString('hex'));
+ setChatId(new Mcid().generate().toString());
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
@@ -512,11 +513,12 @@ const ChatWindow = ({id}: { id?: string }) => {
let recievedMessage = '';
let added = false;
- messageId = messageId ?? crypto.randomBytes(7).toString('hex');
+ messageId = messageId ?? new Mcid().generate().toString();
ws.send(
JSON.stringify({
type: 'message',
+ useerId:userId,
message: {
messageId: messageId,
chatId: chatId!,
@@ -699,6 +701,8 @@ const ChatWindow = ({id}: { id?: string }) => {
void;
focusMode: string;
setFocusMode: (mode: string) => void;
+ copilotEnabled: boolean;
+ setCopilotEnabled: (enabled: boolean) => void;
optimizationMode: string;
setOptimizationMode: (mode: string) => void;
fileIds: string[];
@@ -44,6 +48,8 @@ const EmptyChat = ({
sendMessage={sendMessage}
focusMode={focusMode}
setFocusMode={setFocusMode}
+ copilotEnabled={copilotEnabled}
+ setCopilotEnabled={setCopilotEnabled}
optimizationMode={optimizationMode}
setOptimizationMode={setOptimizationMode}
fileIds={fileIds}
diff --git a/ui/components/EmptyChatMessageInput.tsx b/ui/components/EmptyChatMessageInput.tsx
index 43d1e28..195b8d3 100644
--- a/ui/components/EmptyChatMessageInput.tsx
+++ b/ui/components/EmptyChatMessageInput.tsx
@@ -11,6 +11,8 @@ const EmptyChatMessageInput = ({
sendMessage,
focusMode,
setFocusMode,
+ copilotEnabled,
+ setCopilotEnabled,
optimizationMode,
setOptimizationMode,
fileIds,
@@ -23,12 +25,13 @@ const EmptyChatMessageInput = ({
setFocusMode: (mode: string) => void;
optimizationMode: string;
setOptimizationMode: (mode: string) => void;
+ copilotEnabled:boolean
+ setCopilotEnabled:(mode: boolean) => void;
fileIds: string[];
setFileIds: (fileIds: string[]) => void;
files: File[];
setFiles: (files: File[]) => void;
}) => {
- const [copilotEnabled, setCopilotEnabled] = useState(false);
const [message, setMessage] = useState('');
const inputRef = useRef(null);
@@ -94,6 +97,7 @@ const EmptyChatMessageInput = ({
/>
+
Date: Sun, 2 Feb 2025 15:42:08 -1000
Subject: [PATCH 4/6] update fallbackId
---
ui/components/ChatWindow.tsx | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx
index 2e0505c..1be4ec5 100644
--- a/ui/components/ChatWindow.tsx
+++ b/ui/components/ChatWindow.tsx
@@ -429,25 +429,22 @@ const ChatWindow = ({id}: { id?: string }) => {
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');
+ const fallbackId = "1234567890";
localStorage.setItem('userId', fallbackId);
setUserId(fallbackId);
}
};
initializeUserId();
- }, []); // 空依赖数组确保只运行一次
+ }, []);
useEffect(() => {
if (
From 969f01e2757527a650a5c75a828726b5f91ec3c5 Mon Sep 17 00:00:00 2001
From: litongjava
Date: Sun, 2 Feb 2025 16:04:59 -1000
Subject: [PATCH 5/6] crrect userId
---
ui/components/ChatWindow.tsx | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx
index 1be4ec5..8e18b03 100644
--- a/ui/components/ChatWindow.tsx
+++ b/ui/components/ChatWindow.tsx
@@ -515,7 +515,7 @@ const ChatWindow = ({id}: { id?: string }) => {
ws.send(
JSON.stringify({
type: 'message',
- useerId:userId,
+ userId:userId,
message: {
messageId: messageId,
chatId: chatId!,
@@ -523,6 +523,7 @@ const ChatWindow = ({id}: { id?: string }) => {
},
files: fileIds,
focusMode: focusMode,
+ copilotEnabled: copilotEnabled,
optimizationMode: optimizationMode,
history: [...chatHistory, ['human', message]],
}),
From 5f672eaa66f006dd903823c82c1758399391af72 Mon Sep 17 00:00:00 2001
From: litongjava
Date: Sun, 2 Feb 2025 16:09:59 -1000
Subject: [PATCH 6/6] remove userId
---
ui/components/ChatWindow.tsx | 2 +-
ui/lib/mcid.ts | 4 +---
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx
index 8e18b03..95a81e8 100644
--- a/ui/components/ChatWindow.tsx
+++ b/ui/components/ChatWindow.tsx
@@ -429,7 +429,7 @@ const ChatWindow = ({id}: { id?: string }) => {
setUserId(storedUserId);
console.debug('Using existing user ID:', storedUserId);
} else {
- const newUserId = new Mcid().generate().toString(); // 转换为字符串
+ const newUserId = new Mcid().generate().toString();
localStorage.setItem('userId', newUserId);
setUserId(newUserId);
diff --git a/ui/lib/mcid.ts b/ui/lib/mcid.ts
index 1a0d0d3..77d7f65 100644
--- a/ui/lib/mcid.ts
+++ b/ui/lib/mcid.ts
@@ -30,9 +30,7 @@ export class Mcid {
this.lastTimestamp = now;
return (
- (now * 0x1000) + // 时间戳左移 12 位
- (this.machineId * 16) + // 机器 ID 左移 4 位
- this.sequence // 序列号
+ (now * 0x1000) + (this.machineId * 16) + this.sequence
);
}
}