From 32b749d07eaa67feed8a2d64f44afda33e88ff29 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 20 Jul 2026 18:35:12 +0200 Subject: [PATCH] fix(mobile): support Hermes collection sorting Reuses the focused compatibility approach from closed PR #3923 and covers the remaining hardware-keyboard reverse iteration. Co-authored-by: codex --- .../keyboard/hardwareKeyboardCommands.ts | 5 +++- .../threads/ThreadRelationshipsBanner.tsx | 6 +++-- apps/mobile/src/state/use-thread-selection.ts | 18 +++++++++++-- .../src/state/threadWorkflows.ts | 25 +++++++++---------- packages/shared/package.json | 4 +++ packages/shared/src/Array.test.ts | 12 +++++++++ packages/shared/src/Array.ts | 6 +++++ packages/shared/src/model.ts | 18 ++++++++++--- 8 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 packages/shared/src/Array.test.ts create mode 100644 packages/shared/src/Array.ts diff --git a/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts b/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts index 300434eb73..28a8d6c723 100644 --- a/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts +++ b/apps/mobile/src/features/keyboard/hardwareKeyboardCommands.ts @@ -55,7 +55,10 @@ export function subscribeToHardwareKeyboardCommandRegistrations(listener: () => export function dispatchHardwareKeyboardCommand(command: HardwareKeyboardCommand): boolean { const commandHandlers = handlers.get(command); if (!commandHandlers) return false; - for (const handler of [...commandHandlers].toReversed()) { + const handlersInRegistrationOrder = Array.from(commandHandlers); + for (let index = handlersInRegistrationOrder.length - 1; index >= 0; index -= 1) { + const handler = handlersInRegistrationOrder[index]; + if (!handler) continue; if (handler() !== false) return true; } return false; diff --git a/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx b/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx index bf970456b6..0e0915fb7b 100644 --- a/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx +++ b/apps/mobile/src/features/threads/ThreadRelationshipsBanner.tsx @@ -8,7 +8,8 @@ import { } from "@t3tools/client-runtime/state/thread-relationships"; import { canDetachThreadProviderSession } from "@t3tools/client-runtime/state/thread-workflows"; import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts"; -import { useRouter } from "expo-router"; +import { copySorted } from "@t3tools/shared/Array"; +import { useNavigation } from "@react-navigation/native"; import { useMemo, useState } from "react"; import { ActivityIndicator, Modal, Pressable, ScrollView, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; @@ -79,7 +80,8 @@ export function ThreadRelationshipsBanner(props: { const mergeTargetThreadId = resolveMergeBackTargetThreadId(projection); const rows = useMemo( () => - immediateThreadRelationships(graph, props.threadId).toSorted( + copySorted( + immediateThreadRelationships(graph, props.threadId), (left, right) => Number(right.threadId === mergeTargetThreadId) - Number(left.threadId === mergeTargetThreadId), diff --git a/apps/mobile/src/state/use-thread-selection.ts b/apps/mobile/src/state/use-thread-selection.ts index 8e340ef33b..d4c955ea31 100644 --- a/apps/mobile/src/state/use-thread-selection.ts +++ b/apps/mobile/src/state/use-thread-selection.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import * as Option from "effect/Option"; +import { copySorted } from "@t3tools/shared/Array"; import { useProject, useThreadShell } from "../state/entities"; import { useEnvironmentThread } from "../state/threads"; @@ -44,8 +45,21 @@ function threadDetailToShell( environmentId: EnvironmentId, thread: OrchestrationThread, ): EnvironmentThreadShell { - return { - environmentId, + const thread = projection.thread; + const runsByOrdinal = copySorted(projection.runs, (left, right) => right.ordinal - left.ordinal); + const latestRun = runsByOrdinal[0] ?? null; + const activeRun = + runsByOrdinal.find( + (run) => + run.status === "preparing" || + run.status === "queued" || + run.status === "starting" || + run.status === "running" || + run.status === "waiting", + ) ?? null; + const pendingRequest = + projection.runtimeRequests.find((request) => request.status === "pending") ?? null; + return presentThreadShell(environmentId, { id: thread.id, projectId: thread.projectId, title: thread.title, diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index 4fbceeb8c2..ae2830209f 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -3,6 +3,7 @@ import type { OrchestrationV2ProviderCapabilities, OrchestrationV2ThreadProjection, } from "@t3tools/contracts"; +import { copySorted } from "@t3tools/shared/Array"; type Projection = OrchestrationV2ThreadProjection; type Run = Projection["runs"][number]; @@ -54,19 +55,17 @@ export function deriveThreadQueueWorkflowState(projection: Projection): ThreadQu const activeRun = resolveActiveThreadRun(projection); const session = resolveThreadProviderSession(projection); const capabilities = session?.capabilities.turns; - const queuedRuns = projection.runs - .filter((run) => run.status === "queued") - .toSorted( - (left, right) => - (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal) || - left.ordinal - right.ordinal, - ) - .map((run) => ({ - run, - text: - projection.messages.find((message) => message.id === run.userMessageId)?.text ?? - "Queued message", - })); + const queuedRuns = copySorted( + projection.runs.filter((run) => run.status === "queued"), + (left, right) => + (left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal) || + left.ordinal - right.ordinal, + ).map((run) => ({ + run, + text: + projection.messages.find((message) => message.id === run.userMessageId)?.text ?? + "Queued message", + })); return { activeRun, diff --git a/packages/shared/package.json b/packages/shared/package.json index 6191f4db90..e9ff5b210a 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -3,6 +3,10 @@ "private": true, "type": "module", "exports": { + "./Array": { + "types": "./src/Array.ts", + "import": "./src/Array.ts" + }, "./projectFavicon": { "types": "./src/projectFavicon.ts", "import": "./src/projectFavicon.ts" diff --git a/packages/shared/src/Array.test.ts b/packages/shared/src/Array.test.ts new file mode 100644 index 0000000000..ab4440ca6e --- /dev/null +++ b/packages/shared/src/Array.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { copySorted } from "./Array.ts"; + +describe("array copies", () => { + it("sorts a copy without mutating the source", () => { + const source = [3, 1, 2]; + + expect(copySorted(source, (left: number, right: number) => left - right)).toEqual([1, 2, 3]); + expect(source).toEqual([3, 1, 2]); + }); +}); diff --git a/packages/shared/src/Array.ts b/packages/shared/src/Array.ts new file mode 100644 index 0000000000..dea902cfc4 --- /dev/null +++ b/packages/shared/src/Array.ts @@ -0,0 +1,6 @@ +export function copySorted( + values: ReadonlyArray, + compareFn?: (left: T, right: T) => number, +): T[] { + return [...values].sort(compareFn); +} diff --git a/packages/shared/src/model.ts b/packages/shared/src/model.ts index 7a2c7973c4..48f1934b20 100644 --- a/packages/shared/src/model.ts +++ b/packages/shared/src/model.ts @@ -9,6 +9,7 @@ import { type ProviderOptionDescriptor, type ProviderOptionSelection, } from "@t3tools/contracts"; +import { copySorted } from "./Array.ts"; const DEFAULT_PROVIDER_DRIVER_KIND = ProviderDriverKind.make("codex"); @@ -80,12 +81,21 @@ export function getModelSelectionBooleanOptionValue( function canonicalModelSelectionOptions( modelSelection: ModelSelection, ): ReadonlyArray { - return (modelSelection.options ?? []) - .map((selection) => [selection.id, selection.value] as const) - .toSorted(([leftId, leftValue], [rightId, rightValue]) => { + return copySorted( + (modelSelection.options ?? []).map( + (selection): readonly [id: string, value: string | boolean] => [ + selection.id, + selection.value, + ], + ), + ( + [leftId, leftValue]: readonly [id: string, value: string | boolean], + [rightId, rightValue]: readonly [id: string, value: string | boolean], + ) => { const idOrder = leftId.localeCompare(rightId); return idOrder !== 0 ? idOrder : String(leftValue).localeCompare(String(rightValue)); - }); + }, + ); } /**