Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1cb4e4bd5 | ||
|
|
a8a5f7cfbc |
No files matched your search
+677
-323
File diff suppressed because it is too large.
Load diff
+99
-16
@@ -1,8 +1,29 @@
|
||||
import { ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
|
||||
import {
|
||||
ProjectId,
|
||||
ThreadId,
|
||||
TurnId,
|
||||
type OrchestrationReadModel,
|
||||
} from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { markThreadUnread, type AppState } from "./store";
|
||||
import type { Thread } from "./types";
|
||||
import {
|
||||
markThreadUnread,
|
||||
reorderProjects,
|
||||
syncServerReadModel,
|
||||
type AppState,
|
||||
} from "./store";
|
||||
import type { Project, Thread } from "./types";
|
||||
|
||||
function makeProject(id: string, cwd = `/tmp/${id}`): Project {
|
||||
return {
|
||||
id: ProjectId.makeUnsafe(id),
|
||||
name: id,
|
||||
cwd,
|
||||
model: "gpt-5-codex",
|
||||
expanded: true,
|
||||
scripts: [],
|
||||
};
|
||||
}
|
||||
|
||||
function makeThread(overrides: Partial<Thread> = {}): Thread {
|
||||
return {
|
||||
@@ -24,25 +45,81 @@ function makeThread(overrides: Partial<Thread> = {}): Thread {
|
||||
};
|
||||
}
|
||||
|
||||
function makeState(thread: Thread): AppState {
|
||||
function makeState(
|
||||
thread: Thread,
|
||||
projects: Project[] = [makeProject("project-1")],
|
||||
): AppState {
|
||||
return {
|
||||
projects: [
|
||||
{
|
||||
id: ProjectId.makeUnsafe("project-1"),
|
||||
name: "Project",
|
||||
cwd: "/tmp/project",
|
||||
model: "gpt-5-codex",
|
||||
expanded: true,
|
||||
scripts: [],
|
||||
},
|
||||
],
|
||||
projects,
|
||||
threads: [thread],
|
||||
threadsHydrated: true,
|
||||
runtimeMode: "full-access",
|
||||
};
|
||||
}
|
||||
|
||||
function makeReadModel(
|
||||
projects: Array<{ id: string; cwd?: string }>,
|
||||
): OrchestrationReadModel {
|
||||
return {
|
||||
snapshotSequence: 1,
|
||||
projects: projects.map((project) => ({
|
||||
id: ProjectId.makeUnsafe(project.id),
|
||||
title: project.id,
|
||||
workspaceRoot: project.cwd ?? `/tmp/${project.id}`,
|
||||
defaultModel: "gpt-5-codex",
|
||||
scripts: [],
|
||||
createdAt: "2026-02-25T12:00:00.000Z",
|
||||
updatedAt: "2026-02-25T12:00:00.000Z",
|
||||
deletedAt: null,
|
||||
})),
|
||||
threads: [],
|
||||
updatedAt: "2026-02-25T12:00:00.000Z",
|
||||
};
|
||||
}
|
||||
|
||||
describe("store pure functions", () => {
|
||||
it("reorderProjects moves a project to the end insertion slot", () => {
|
||||
const initialState = makeState(makeThread(), [
|
||||
makeProject("project-1"),
|
||||
makeProject("project-2"),
|
||||
makeProject("project-3"),
|
||||
]);
|
||||
|
||||
const next = reorderProjects(
|
||||
initialState,
|
||||
ProjectId.makeUnsafe("project-1"),
|
||||
3,
|
||||
);
|
||||
|
||||
expect(next.projects.map((project) => project.id)).toEqual([
|
||||
ProjectId.makeUnsafe("project-2"),
|
||||
ProjectId.makeUnsafe("project-3"),
|
||||
ProjectId.makeUnsafe("project-1"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("syncServerReadModel keeps the existing local project order and appends new projects", () => {
|
||||
const initialState = makeState(makeThread(), [
|
||||
makeProject("project-b", "/tmp/project-b"),
|
||||
makeProject("project-a", "/tmp/project-a"),
|
||||
]);
|
||||
|
||||
const next = syncServerReadModel(
|
||||
initialState,
|
||||
makeReadModel([
|
||||
{ id: "project-a", cwd: "/tmp/project-a" },
|
||||
{ id: "project-b", cwd: "/tmp/project-b" },
|
||||
{ id: "project-c", cwd: "/tmp/project-c" },
|
||||
]),
|
||||
);
|
||||
|
||||
expect(next.projects.map((project) => project.cwd)).toEqual([
|
||||
"/tmp/project-b",
|
||||
"/tmp/project-a",
|
||||
"/tmp/project-c",
|
||||
]);
|
||||
});
|
||||
|
||||
it("markThreadUnread moves lastVisitedAt before completion for a completed thread", () => {
|
||||
const latestTurnCompletedAt = "2026-02-25T12:30:00.000Z";
|
||||
const initialState = makeState(
|
||||
@@ -59,7 +136,10 @@ describe("store pure functions", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
|
||||
const next = markThreadUnread(
|
||||
initialState,
|
||||
ThreadId.makeUnsafe("thread-1"),
|
||||
);
|
||||
|
||||
const updatedThread = next.threads[0];
|
||||
expect(updatedThread).toBeDefined();
|
||||
@@ -77,7 +157,10 @@ describe("store pure functions", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
const next = markThreadUnread(initialState, ThreadId.makeUnsafe("thread-1"));
|
||||
const next = markThreadUnread(
|
||||
initialState,
|
||||
ThreadId.makeUnsafe("thread-1"),
|
||||
);
|
||||
|
||||
expect(next).toEqual(initialState);
|
||||
});
|
||||
|
||||
+95
-15
@@ -44,28 +44,37 @@ const initialState: AppState = {
|
||||
runtimeMode: DEFAULT_RUNTIME_MODE,
|
||||
};
|
||||
const persistedExpandedProjectCwds = new Set<string>();
|
||||
const persistedProjectOrderCwds: string[] = [];
|
||||
|
||||
// ── Persist helpers ──────────────────────────────────────────────────
|
||||
|
||||
function readPersistedState(): AppState {
|
||||
if (typeof window === "undefined") return initialState;
|
||||
try {
|
||||
persistedExpandedProjectCwds.clear();
|
||||
persistedProjectOrderCwds.length = 0;
|
||||
const raw = window.localStorage.getItem(PERSISTED_STATE_KEY);
|
||||
if (!raw) return initialState;
|
||||
const parsed = JSON.parse(raw) as {
|
||||
runtimeMode?: RuntimeMode;
|
||||
expandedProjectCwds?: string[];
|
||||
projectOrderCwds?: string[];
|
||||
};
|
||||
persistedExpandedProjectCwds.clear();
|
||||
for (const cwd of parsed.expandedProjectCwds ?? []) {
|
||||
if (typeof cwd === "string" && cwd.length > 0) {
|
||||
persistedExpandedProjectCwds.add(cwd);
|
||||
}
|
||||
}
|
||||
for (const cwd of parsed.projectOrderCwds ?? []) {
|
||||
if (typeof cwd === "string" && cwd.length > 0) {
|
||||
persistedProjectOrderCwds.push(cwd);
|
||||
}
|
||||
}
|
||||
return {
|
||||
...initialState,
|
||||
runtimeMode:
|
||||
parsed.runtimeMode === "approval-required" || parsed.runtimeMode === "full-access"
|
||||
parsed.runtimeMode === "approval-required" ||
|
||||
parsed.runtimeMode === "full-access"
|
||||
? parsed.runtimeMode
|
||||
: DEFAULT_RUNTIME_MODE,
|
||||
};
|
||||
@@ -84,6 +93,7 @@ function persistState(state: AppState): void {
|
||||
expandedProjectCwds: state.projects
|
||||
.filter((project) => project.expanded)
|
||||
.map((project) => project.cwd),
|
||||
projectOrderCwds: state.projects.map((project) => project.cwd),
|
||||
}),
|
||||
);
|
||||
for (const legacyKey of LEGACY_PERSISTED_STATE_KEYS) {
|
||||
@@ -115,7 +125,7 @@ function mapProjectsFromReadModel(
|
||||
incoming: OrchestrationReadModel["projects"],
|
||||
previous: Project[],
|
||||
): Project[] {
|
||||
return incoming.map((project) => {
|
||||
const mappedProjects = incoming.map((project) => {
|
||||
const existing =
|
||||
previous.find((entry) => entry.id === project.id) ??
|
||||
previous.find((entry) => entry.cwd === project.workspaceRoot);
|
||||
@@ -123,7 +133,9 @@ function mapProjectsFromReadModel(
|
||||
id: project.id,
|
||||
name: project.title,
|
||||
cwd: project.workspaceRoot,
|
||||
model: existing?.model ?? resolveModelSlug(project.defaultModel ?? DEFAULT_MODEL),
|
||||
model:
|
||||
existing?.model ??
|
||||
resolveModelSlug(project.defaultModel ?? DEFAULT_MODEL),
|
||||
expanded:
|
||||
existing?.expanded ??
|
||||
(persistedExpandedProjectCwds.size > 0
|
||||
@@ -132,6 +144,29 @@ function mapProjectsFromReadModel(
|
||||
scripts: project.scripts.map((script) => ({ ...script })),
|
||||
};
|
||||
});
|
||||
const preferredProjectOrderCwds =
|
||||
previous.length > 0
|
||||
? previous.map((project) => project.cwd)
|
||||
: persistedProjectOrderCwds;
|
||||
if (preferredProjectOrderCwds.length === 0 || mappedProjects.length <= 1) {
|
||||
return mappedProjects;
|
||||
}
|
||||
const orderByCwd = new Map(
|
||||
preferredProjectOrderCwds.map((cwd, index) => [cwd, index] as const),
|
||||
);
|
||||
const orderedProjects = mappedProjects.toSorted((left, right) => {
|
||||
const leftIndex = orderByCwd.get(left.cwd);
|
||||
const rightIndex = orderByCwd.get(right.cwd);
|
||||
if (leftIndex === undefined && rightIndex === undefined) return 0;
|
||||
if (leftIndex === undefined) return 1;
|
||||
if (rightIndex === undefined) return -1;
|
||||
return leftIndex - rightIndex;
|
||||
});
|
||||
return orderedProjects.every(
|
||||
(project, index) => project === mappedProjects[index],
|
||||
)
|
||||
? mappedProjects
|
||||
: orderedProjects;
|
||||
}
|
||||
|
||||
function toLegacySessionStatus(
|
||||
@@ -228,7 +263,9 @@ export function syncServerReadModel(
|
||||
activeTurnId: thread.session.activeTurnId ?? undefined,
|
||||
createdAt: thread.session.updatedAt,
|
||||
updatedAt: thread.session.updatedAt,
|
||||
...(thread.session.lastError ? { lastError: thread.session.lastError } : {}),
|
||||
...(thread.session.lastError
|
||||
? { lastError: thread.session.lastError }
|
||||
: {}),
|
||||
}
|
||||
: null,
|
||||
messages: thread.messages.map((message) => {
|
||||
@@ -238,7 +275,9 @@ export function syncServerReadModel(
|
||||
name: attachment.name,
|
||||
mimeType: attachment.mimeType,
|
||||
sizeBytes: attachment.sizeBytes,
|
||||
previewUrl: toAttachmentPreviewUrl(attachmentPreviewRoutePath(attachment.id)),
|
||||
previewUrl: toAttachmentPreviewUrl(
|
||||
attachmentPreviewRoutePath(attachment.id),
|
||||
),
|
||||
}));
|
||||
const normalizedMessage: ChatMessage = {
|
||||
id: message.id,
|
||||
@@ -285,7 +324,9 @@ export function markThreadVisited(
|
||||
const at = visitedAt ?? new Date().toISOString();
|
||||
const visitedAtMs = Date.parse(at);
|
||||
const threads = updateThread(state.threads, threadId, (thread) => {
|
||||
const previousVisitedAtMs = thread.lastVisitedAt ? Date.parse(thread.lastVisitedAt) : NaN;
|
||||
const previousVisitedAtMs = thread.lastVisitedAt
|
||||
? Date.parse(thread.lastVisitedAt)
|
||||
: NaN;
|
||||
if (
|
||||
Number.isFinite(previousVisitedAtMs) &&
|
||||
Number.isFinite(visitedAtMs) &&
|
||||
@@ -298,7 +339,10 @@ export function markThreadVisited(
|
||||
return threads === state.threads ? state : { ...state, threads };
|
||||
}
|
||||
|
||||
export function markThreadUnread(state: AppState, threadId: ThreadId): AppState {
|
||||
export function markThreadUnread(
|
||||
state: AppState,
|
||||
threadId: ThreadId,
|
||||
): AppState {
|
||||
const threads = updateThread(state.threads, threadId, (thread) => {
|
||||
if (!thread.latestTurn?.completedAt) return thread;
|
||||
const latestTurnCompletedAtMs = Date.parse(thread.latestTurn.completedAt);
|
||||
@@ -310,7 +354,10 @@ export function markThreadUnread(state: AppState, threadId: ThreadId): AppState
|
||||
return threads === state.threads ? state : { ...state, threads };
|
||||
}
|
||||
|
||||
export function toggleProject(state: AppState, projectId: Project["id"]): AppState {
|
||||
export function toggleProject(
|
||||
state: AppState,
|
||||
projectId: Project["id"],
|
||||
): AppState {
|
||||
return {
|
||||
...state,
|
||||
projects: state.projects.map((p) =>
|
||||
@@ -333,7 +380,36 @@ export function setProjectExpanded(
|
||||
return changed ? { ...state, projects } : state;
|
||||
}
|
||||
|
||||
export function setError(state: AppState, threadId: ThreadId, error: string | null): AppState {
|
||||
export function reorderProjects(
|
||||
state: AppState,
|
||||
sourceProjectId: Project["id"],
|
||||
destinationIndex: number,
|
||||
): AppState {
|
||||
const sourceIndex = state.projects.findIndex(
|
||||
(project) => project.id === sourceProjectId,
|
||||
);
|
||||
if (sourceIndex === -1) return state;
|
||||
const boundedDestinationIndex = Math.max(
|
||||
0,
|
||||
Math.min(destinationIndex, state.projects.length),
|
||||
);
|
||||
const normalizedDestinationIndex =
|
||||
sourceIndex < boundedDestinationIndex
|
||||
? boundedDestinationIndex - 1
|
||||
: boundedDestinationIndex;
|
||||
if (normalizedDestinationIndex === sourceIndex) return state;
|
||||
const projects = [...state.projects];
|
||||
const [movedProject] = projects.splice(sourceIndex, 1);
|
||||
if (!movedProject) return state;
|
||||
projects.splice(normalizedDestinationIndex, 0, movedProject);
|
||||
return { ...state, projects };
|
||||
}
|
||||
|
||||
export function setError(
|
||||
state: AppState,
|
||||
threadId: ThreadId,
|
||||
error: string | null,
|
||||
): AppState {
|
||||
const threads = updateThread(state.threads, threadId, (t) => {
|
||||
if (t.error === error) return t;
|
||||
return { ...t, error };
|
||||
@@ -373,6 +449,10 @@ interface AppStore extends AppState {
|
||||
markThreadUnread: (threadId: ThreadId) => void;
|
||||
toggleProject: (projectId: Project["id"]) => void;
|
||||
setProjectExpanded: (projectId: Project["id"], expanded: boolean) => void;
|
||||
reorderProjects: (
|
||||
sourceProjectId: Project["id"],
|
||||
destinationIndex: number,
|
||||
) => void;
|
||||
setError: (threadId: ThreadId, error: string | null) => void;
|
||||
setThreadBranch: (
|
||||
threadId: ThreadId,
|
||||
@@ -390,19 +470,19 @@ export const useStore = create<AppStore>((set) => ({
|
||||
set((state) => markThreadVisited(state, threadId, visitedAt)),
|
||||
markThreadUnread: (threadId) =>
|
||||
set((state) => markThreadUnread(state, threadId)),
|
||||
toggleProject: (projectId) =>
|
||||
set((state) => toggleProject(state, projectId)),
|
||||
toggleProject: (projectId) => set((state) => toggleProject(state, projectId)),
|
||||
setProjectExpanded: (projectId, expanded) =>
|
||||
set((state) => setProjectExpanded(state, projectId, expanded)),
|
||||
reorderProjects: (sourceProjectId, destinationIndex) =>
|
||||
set((state) => reorderProjects(state, sourceProjectId, destinationIndex)),
|
||||
setError: (threadId, error) =>
|
||||
set((state) => setError(state, threadId, error)),
|
||||
setThreadBranch: (threadId, branch, worktreePath) =>
|
||||
set((state) => setThreadBranch(state, threadId, branch, worktreePath)),
|
||||
setRuntimeMode: (mode) =>
|
||||
set((state) => setRuntimeMode(state, mode)),
|
||||
setRuntimeMode: (mode) => set((state) => setRuntimeMode(state, mode)),
|
||||
}));
|
||||
|
||||
// Persist on every state change (only runtimeMode + expandedProjectCwds)
|
||||
// Persist on every state change (runtimeMode + sidebar project UI state)
|
||||
useStore.subscribe((state) => persistState(state));
|
||||
|
||||
export function StoreProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
Reference in new issue
Block a user