DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
follow-up to #1308: one command-id generator, and not from Math.random
Bingxi Zhao (Frank)82621bd2f60d
diff --git a/web/contracts/parse/turn-command.ts b/web/contracts/parse/turn-command.tsindex 9c1d1c20c..80e9688f4 100644--- a/web/contracts/parse/turn-command.ts+++ b/web/contracts/parse/turn-command.ts@@ -28,9 +28,43 @@ function sequence(value: number): number {return value;}+/**+ * A v4 UUID for a command the server acknowledges by id.+ *+ * ``crypto.randomUUID`` is defined only in a secure context, so on a+ * plain-HTTP deployment — a LAN box, a VPS without TLS — it is undefined and+ * reaching for it *threw*: before the command was enqueued, so the answer+ * never left the browser and the card reported the reply as not delivered.+ * ``crypto.getRandomValues`` carries no such gate, which is why it is the+ * fallback rather than ``Math.random``: same shape, still unguessable. The+ * ``Math.random`` tail is only for a runtime with no Web Crypto at all.+ */+export function newCommandId(): string {+ const webCrypto = globalThis.crypto;+ if (typeof webCrypto?.randomUUID === "function") return webCrypto.randomUUID();+ const bytes = new Uint8Array(16);+ if (typeof webCrypto?.getRandomValues === "function") {+ webCrypto.getRandomValues(bytes);+ } else {+ for (let i = 0; i < bytes.length; i += 1) {+ bytes[i] = Math.floor(Math.random() * 256);+ }+ }+ bytes[6] = (bytes[6] & 0x0f) | 0x40;+ bytes[8] = (bytes[8] & 0x3f) | 0x80;+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"));+ return [+ hex.slice(0, 4).join(""),+ hex.slice(4, 6).join(""),+ hex.slice(6, 8).join(""),+ hex.slice(8, 10).join(""),+ hex.slice(10, 16).join(""),+ ].join("-");+}+function commandId(value?: string): string {if (value !== undefined) return id(value, "command_id");- return globalThis.crypto.randomUUID();+ return newCommandId();}export function buildStartTurn(diff --git a/web/features/chat/transport/TurnRuntimeClient.ts b/web/features/chat/transport/TurnRuntimeClient.tsindex 35d9fc18f..89bbef932 100644--- a/web/features/chat/transport/TurnRuntimeClient.ts+++ b/web/features/chat/transport/TurnRuntimeClient.ts@@ -3,7 +3,11 @@ import type {ServerEvent,StreamEvent,} from "@/contracts/generated/turn-protocol";-import { buildPing, buildResumeTurn } from "@/contracts/parse/turn-command";+import {+ buildPing,+ buildResumeTurn,+ newCommandId,+} from "@/contracts/parse/turn-command";import { parseTurnEvent } from "@/contracts/parse/turn-event";import {@@ -56,28 +60,6 @@ const ACKNOWLEDGED_COMMAND_TYPES = new Set(["user_input",]);-/**- * Generates a command id for acknowledged commands.- *- * `crypto.randomUUID()` is only available in secure contexts (HTTPS). When- * DeepTutor is served over plain HTTP (e.g. a LAN or VPS deployment) it is- * undefined, and submitting an acknowledged command (`submit_user_reply`,- * `cancel_turn`, `user_input`) would throw inside `prepareCommand` before the- * command is ever enqueued, so the answer never reaches the server and the UI- * reports the reply as "not delivered". Fall back to an RFC 4122 v4-style- * UUID in that case.- */-function createCommandId(): string {- if (typeof globalThis.crypto?.randomUUID === "function") {- return globalThis.crypto.randomUUID();- }- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {- const r = (Math.random() * 16) | 0;- const v = c === "x" ? r : (r & 0x3) | 0x8;- return v.toString(16);- });-}-function prepareCommand(command: ClientCommand): {command: ClientCommand;commandId: string | null;@@ -90,7 +72,7 @@ function prepareCommand(command: ClientCommand): {const record = command as unknown as Record<string, unknown>;const existing =typeof record.command_id === "string" ? record.command_id.trim() : "";- const commandId = existing || createCommandId();+ const commandId = existing || newCommandId();return {command: { ...command, command_id: commandId } as ClientCommand,commandId,