Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): fall back when crypto.randomUUID is unavailable for command ids (#1308)

LuLia599445b02b1
diff --git a/web/features/chat/transport/TurnRuntimeClient.ts b/web/features/chat/transport/TurnRuntimeClient.ts
index 03367b937..35d9fc18f 100644
--- a/web/features/chat/transport/TurnRuntimeClient.ts
+++ b/web/features/chat/transport/TurnRuntimeClient.ts
@@ -56,6 +56,28 @@ 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;
@@ -68,7 +90,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 || globalThis.crypto.randomUUID();
+ const commandId = existing || createCommandId();
return {
command: { ...command, command_id: commandId } as ClientCommand,
commandId,
diff --git a/web/tests/turn-runtime-client.test.ts b/web/tests/turn-runtime-client.test.ts
index 8cdf92413..15779da3c 100644
--- a/web/tests/turn-runtime-client.test.ts
+++ b/web/tests/turn-runtime-client.test.ts
@@ -387,3 +387,34 @@ test("stopping releases waiters that will never be acknowledged", async () => {
assert.equal(await verdict, false);
});
+
+
+test("acknowledged commands get a generated command_id when crypto.randomUUID is unavailable", () => {
+ const original = globalThis.crypto?.randomUUID;
+ const cryptoObject = globalThis.crypto as {
+ randomUUID?: typeof globalThis.crypto.randomUUID;
+ };
+ if (cryptoObject) cryptoObject.randomUUID = undefined;
+ try {
+ const { client, sockets } = harness();
+ client.connect();
+ sockets[0].open();
+ client.send(
+ buildSubmitUserReply({
+ turnId: "turn-1",
+ text: "A",
+ answers: [{ questionId: "q", text: "B" }],
+ }),
+ );
+ const sent = sockets[0].sent.find(
+ (item) => item.type === "submit_user_reply",
+ ) as Record<string, unknown>;
+ assert.ok(sent, "submit_user_reply should be sent");
+ assert.match(
+ String(sent.command_id),
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
+ );
+ } finally {
+ if (cryptoObject) cryptoObject.randomUUID = original;
+ }
+});