Add archived threads and mobile file viewer (#3155)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
93 files changed
+6792
-1137
No files matched your search
@@ -1,13 +1,22 @@
|
||||
import { EnvironmentId, type OrchestrationShellSnapshot } from "@t3tools/contracts";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Cause from "effect/Cause";
|
||||
import { pipe } from "effect/Function";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Order from "effect/Order";
|
||||
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
||||
|
||||
export interface ArchivedSnapshotEntry {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly snapshot: OrchestrationShellSnapshot;
|
||||
}
|
||||
|
||||
export interface ArchivedThreadSnapshotsState {
|
||||
readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
|
||||
readonly error: string | null;
|
||||
readonly isLoading: boolean;
|
||||
}
|
||||
|
||||
const ARCHIVED_THREADS_ENVIRONMENT_KEY_SEPARATOR = "\u001f";
|
||||
const environmentIdOrder = Order.String as Order.Order<EnvironmentId>;
|
||||
|
||||
@@ -28,3 +37,38 @@ export function parseArchivedThreadsEnvironmentKey(key: string): ReadonlyArray<E
|
||||
Arr.map((environmentId) => EnvironmentId.make(environmentId)),
|
||||
);
|
||||
}
|
||||
|
||||
export function createArchivedThreadSnapshotsAtomFamily<E>(options: {
|
||||
readonly getSnapshotAtom: (
|
||||
environmentId: EnvironmentId,
|
||||
) => Atom.Atom<AsyncResult.AsyncResult<OrchestrationShellSnapshot, E>>;
|
||||
readonly labelPrefix: string;
|
||||
}) {
|
||||
return Atom.family((environmentKey: string) =>
|
||||
Atom.make((get): ArchivedThreadSnapshotsState => {
|
||||
const snapshots: ArchivedSnapshotEntry[] = [];
|
||||
let error: string | null = null;
|
||||
let isLoading = false;
|
||||
|
||||
for (const environmentId of parseArchivedThreadsEnvironmentKey(environmentKey)) {
|
||||
const result = get(options.getSnapshotAtom(environmentId));
|
||||
isLoading ||= result.waiting;
|
||||
|
||||
const snapshot = Option.getOrNull(AsyncResult.value(result));
|
||||
if (snapshot !== null) {
|
||||
snapshots.push({ environmentId, snapshot });
|
||||
}
|
||||
|
||||
if (error === null && result._tag === "Failure") {
|
||||
const cause = Cause.squash(result.cause);
|
||||
error =
|
||||
cause instanceof Error && cause.message.trim().length > 0
|
||||
? cause.message
|
||||
: "Failed to load archived threads.";
|
||||
}
|
||||
}
|
||||
|
||||
return { snapshots, error, isLoading };
|
||||
}).pipe(Atom.withLabel(`${options.labelPrefix}:${environmentKey}`)),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import { scopedProjectKey, scopeProjectRef } from "../environment/scoped.ts";
|
||||
import type { ScopedProjectRef, SidebarProjectGroupingMode } from "@t3tools/contracts";
|
||||
import type { UnifiedSettings } from "@t3tools/contracts/settings";
|
||||
|
||||
import type { EnvironmentProject } from "./models.ts";
|
||||
import { normalizeProjectPathForComparison } from "./projects.ts";
|
||||
|
||||
export interface ProjectGroupingSettings {
|
||||
readonly sidebarProjectGroupingMode: SidebarProjectGroupingMode;
|
||||
readonly sidebarProjectGroupingOverrides: Record<string, SidebarProjectGroupingMode>;
|
||||
}
|
||||
|
||||
export type ProjectGroupingMode = SidebarProjectGroupingMode;
|
||||
|
||||
export function selectProjectGroupingSettings(settings: UnifiedSettings): ProjectGroupingSettings {
|
||||
return {
|
||||
sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
|
||||
sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
|
||||
};
|
||||
}
|
||||
|
||||
function uniqueNonEmptyValues(values: ReadonlyArray<string | null | undefined>): string[] {
|
||||
const seen = new Set<string>();
|
||||
const unique: string[] = [];
|
||||
for (const value of values) {
|
||||
const trimmed = value?.trim();
|
||||
if (!trimmed || seen.has(trimmed)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(trimmed);
|
||||
unique.push(trimmed);
|
||||
}
|
||||
return unique;
|
||||
}
|
||||
|
||||
function deriveRepositoryRelativeProjectPath(
|
||||
project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">,
|
||||
): string | null {
|
||||
const rootPath = project.repositoryIdentity?.rootPath?.trim();
|
||||
if (!rootPath) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalizedProjectPath = normalizeProjectPathForComparison(project.workspaceRoot);
|
||||
const normalizedRootPath = normalizeProjectPathForComparison(rootPath);
|
||||
if (normalizedProjectPath.length === 0 || normalizedRootPath.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (normalizedProjectPath === normalizedRootPath) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const separator = normalizedRootPath.includes("\\") ? "\\" : "/";
|
||||
const rootPrefix = `${normalizedRootPath}${separator}`;
|
||||
if (!normalizedProjectPath.startsWith(rootPrefix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/");
|
||||
}
|
||||
|
||||
export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string {
|
||||
return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`;
|
||||
}
|
||||
|
||||
export function derivePhysicalProjectKey(
|
||||
project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
|
||||
): string {
|
||||
return derivePhysicalProjectKeyFromPath(project.environmentId, project.workspaceRoot);
|
||||
}
|
||||
|
||||
export function deriveProjectGroupingOverrideKey(
|
||||
project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
|
||||
): string {
|
||||
return derivePhysicalProjectKey(project);
|
||||
}
|
||||
|
||||
export function getProjectOrderKey(
|
||||
project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
|
||||
): string {
|
||||
return derivePhysicalProjectKey(project);
|
||||
}
|
||||
|
||||
export function resolveProjectGroupingMode(
|
||||
project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">,
|
||||
settings: ProjectGroupingSettings,
|
||||
): SidebarProjectGroupingMode {
|
||||
return (
|
||||
settings.sidebarProjectGroupingOverrides?.[deriveProjectGroupingOverrideKey(project)] ??
|
||||
settings.sidebarProjectGroupingMode
|
||||
);
|
||||
}
|
||||
|
||||
function deriveRepositoryScopedKey(
|
||||
project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">,
|
||||
groupingMode: SidebarProjectGroupingMode,
|
||||
): string | null {
|
||||
const canonicalKey = project.repositoryIdentity?.canonicalKey;
|
||||
if (!canonicalKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (groupingMode === "repository") {
|
||||
return canonicalKey;
|
||||
}
|
||||
|
||||
const relativeProjectPath = deriveRepositoryRelativeProjectPath(project);
|
||||
if (relativeProjectPath === null) {
|
||||
return canonicalKey;
|
||||
}
|
||||
|
||||
return relativeProjectPath.length === 0
|
||||
? canonicalKey
|
||||
: `${canonicalKey}::${relativeProjectPath}`;
|
||||
}
|
||||
|
||||
export function deriveLogicalProjectKey(
|
||||
project: Pick<
|
||||
EnvironmentProject,
|
||||
"environmentId" | "id" | "workspaceRoot" | "repositoryIdentity"
|
||||
>,
|
||||
options?: {
|
||||
readonly groupingMode?: SidebarProjectGroupingMode;
|
||||
},
|
||||
): string {
|
||||
const groupingMode = options?.groupingMode ?? "repository";
|
||||
if (groupingMode === "separate") {
|
||||
return derivePhysicalProjectKey(project);
|
||||
}
|
||||
|
||||
return (
|
||||
deriveRepositoryScopedKey(project, groupingMode) ??
|
||||
derivePhysicalProjectKey(project) ??
|
||||
scopedProjectKey(scopeProjectRef(project.environmentId, project.id))
|
||||
);
|
||||
}
|
||||
|
||||
export function deriveLogicalProjectKeyFromSettings(
|
||||
project: Pick<
|
||||
EnvironmentProject,
|
||||
"environmentId" | "id" | "workspaceRoot" | "repositoryIdentity"
|
||||
>,
|
||||
settings: ProjectGroupingSettings,
|
||||
): string {
|
||||
return deriveLogicalProjectKey(project, {
|
||||
groupingMode: resolveProjectGroupingMode(project, settings),
|
||||
});
|
||||
}
|
||||
|
||||
export function deriveLogicalProjectKeyFromRef(
|
||||
projectRef: ScopedProjectRef,
|
||||
project:
|
||||
| Pick<EnvironmentProject, "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity">
|
||||
| null
|
||||
| undefined,
|
||||
options?: {
|
||||
readonly groupingMode?: SidebarProjectGroupingMode;
|
||||
},
|
||||
): string {
|
||||
return project ? deriveLogicalProjectKey(project, options) : scopedProjectKey(projectRef);
|
||||
}
|
||||
|
||||
export function deriveProjectGroupLabel(input: {
|
||||
readonly representative: Pick<EnvironmentProject, "title" | "repositoryIdentity">;
|
||||
readonly members: ReadonlyArray<Pick<EnvironmentProject, "title" | "repositoryIdentity">>;
|
||||
}): string {
|
||||
const sharedDisplayNames = uniqueNonEmptyValues(
|
||||
input.members.map((member) => member.repositoryIdentity?.displayName),
|
||||
);
|
||||
if (sharedDisplayNames.length === 1) {
|
||||
return sharedDisplayNames[0]!;
|
||||
}
|
||||
|
||||
const sharedRepositoryNames = uniqueNonEmptyValues(
|
||||
input.members.map((member) => member.repositoryIdentity?.name),
|
||||
);
|
||||
if (sharedRepositoryNames.length === 1) {
|
||||
return sharedRepositoryNames[0]!;
|
||||
}
|
||||
|
||||
return input.representative.title;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import type { ProjectId } from "@t3tools/contracts";
|
||||
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Order from "effect/Order";
|
||||
|
||||
export interface ThreadSortInput {
|
||||
readonly createdAt: string;
|
||||
readonly updatedAt: string;
|
||||
readonly latestUserMessageAt?: string | null;
|
||||
readonly messages?: ReadonlyArray<{
|
||||
readonly createdAt: string;
|
||||
readonly role: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export function toSortableTimestamp(iso: string | undefined): number | null {
|
||||
if (!iso) return null;
|
||||
const ms = Date.parse(iso);
|
||||
return Number.isFinite(ms) ? ms : null;
|
||||
}
|
||||
|
||||
function getFirstSortableTimestamp(...values: Array<string | null | undefined>): number | null {
|
||||
for (const value of values) {
|
||||
const timestamp = toSortableTimestamp(value ?? undefined);
|
||||
if (timestamp !== null) {
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
|
||||
if (thread.latestUserMessageAt) {
|
||||
return toSortableTimestamp(thread.latestUserMessageAt) ?? Number.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
let latestUserMessageTimestamp: number | null = null;
|
||||
|
||||
for (const message of thread.messages ?? []) {
|
||||
if (message.role !== "user") continue;
|
||||
const messageTimestamp = toSortableTimestamp(message.createdAt);
|
||||
if (messageTimestamp === null) continue;
|
||||
latestUserMessageTimestamp =
|
||||
latestUserMessageTimestamp === null
|
||||
? messageTimestamp
|
||||
: Math.max(latestUserMessageTimestamp, messageTimestamp);
|
||||
}
|
||||
|
||||
if (latestUserMessageTimestamp !== null) {
|
||||
return latestUserMessageTimestamp;
|
||||
}
|
||||
|
||||
return getFirstSortableTimestamp(thread.updatedAt, thread.createdAt) ?? Number.NEGATIVE_INFINITY;
|
||||
}
|
||||
|
||||
export function getThreadSortTimestamp(
|
||||
thread: ThreadSortInput,
|
||||
sortOrder: SidebarThreadSortOrder | Exclude<SidebarProjectSortOrder, "manual">,
|
||||
): number {
|
||||
if (sortOrder === "created_at") {
|
||||
return (
|
||||
getFirstSortableTimestamp(thread.createdAt, thread.updatedAt) ?? Number.NEGATIVE_INFINITY
|
||||
);
|
||||
}
|
||||
return getLatestUserMessageTimestamp(thread);
|
||||
}
|
||||
|
||||
export function sortThreads<T extends { readonly id: string } & ThreadSortInput>(
|
||||
threads: readonly T[],
|
||||
sortOrder: SidebarThreadSortOrder,
|
||||
): T[] {
|
||||
return Arr.sort(
|
||||
threads,
|
||||
Order.mapInput(
|
||||
Order.Struct({
|
||||
timestamp: Order.flip(Order.Number),
|
||||
id: Order.flip(Order.String),
|
||||
}),
|
||||
(thread: T) => ({
|
||||
timestamp: getThreadSortTimestamp(thread, sortOrder),
|
||||
id: thread.id,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function getLatestThreadForProject<
|
||||
T extends {
|
||||
readonly id: string;
|
||||
readonly projectId: ProjectId;
|
||||
readonly archivedAt: string | null;
|
||||
} & ThreadSortInput,
|
||||
>(threads: readonly T[], projectId: ProjectId, sortOrder: SidebarThreadSortOrder): T | null {
|
||||
return (
|
||||
sortThreads(
|
||||
threads.filter((thread) => thread.projectId === projectId && thread.archivedAt === null),
|
||||
sortOrder,
|
||||
)[0] ?? null
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user