Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions BetterSDK/src/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdir, readdir, readFile, unlink, writeFile } from "node:fs/promises";
import { mkdir, readdir, readFile, stat, unlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { homedir } from "node:os";
import { createHash, randomUUID } from "node:crypto";
Expand Down Expand Up @@ -62,6 +62,7 @@ export async function getSessionInfo(
sessionId: string,
opts: { dir?: string } = {},
): Promise<SessionInfo | null> {
const file = sessionFile(sessionId, opts.dir);
const messages = await getSessionMessages(sessionId, {
dir: opts.dir,
includeSystemMessages: true,
Expand All @@ -71,15 +72,18 @@ export async function getSessionInfo(
last = messages[messages.length - 1] as any;
const firstUser = messages.find((m: any) => m.type === "user") as any;
const firstPrompt = contentText(firstUser?.message?.content);
const fileMtime = await stat(file)
.then((s) => s.mtimeMs)
.catch(() => undefined);
return {
sessionId,
summary: first?.summary ?? firstPrompt?.slice(0, 80),
firstPrompt,
customTitle: first?.customTitle,
cwd: first?.cwd ?? opts.dir,
model: last?.message?.model ?? first?.model,
createdAt: timestamp(first) ?? Date.now(),
lastModified: timestamp(last) ?? Date.now(),
createdAt: firstTimestampInMessages(messages) ?? fileMtime ?? Date.now(),
lastModified: lastTimestampInMessages(messages) ?? fileMtime ?? Date.now(),
};
}

Expand Down Expand Up @@ -127,6 +131,22 @@ export async function forkSession(
return { sessionId: newId };
}

function firstTimestampInMessages(messages: any[]): number | undefined {
for (const m of messages) {
const t = timestamp(m);
if (t !== undefined) return t;
}
return undefined;
}

function lastTimestampInMessages(messages: any[]): number | undefined {
for (let i = messages.length - 1; i >= 0; i--) {
const t = timestamp(messages[i]);
if (t !== undefined) return t;
}
return undefined;
}

function timestamp(m: any): number | undefined {
const t = m?.timestamp ?? m?.created_at ?? m?.time;
const n = typeof t === "string" ? Date.parse(t) : typeof t === "number" ? t : NaN;
Expand Down
4 changes: 2 additions & 2 deletions src/components/sidebar/use-sidebar-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ function session(id: string, harnessId: HarnessId, updated: number): Session {
}

describe("sortSessionsForSidebar", () => {
test("keeps preferred Harness sessions above newer sessions from other Harnesses", () => {
test("sorts by newest update regardless of Harness", () => {
const sorted = sortSessionsForSidebar(
[session("claude-new", "claude-code", 20), session("open-old", "opencode", 10)],
{},
"opencode",
);

expect(sorted.map((item) => item.id)).toEqual(["open-old", "claude-new"]);
expect(sorted.map((item) => item.id)).toEqual(["claude-new", "open-old"]);
});

test("sorts by newest update inside the same Harness", () => {
Expand Down
10 changes: 2 additions & 8 deletions src/components/sidebar/use-sidebar-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@/lib/worktree-placement";
import { getProjectName, normalizeProjectPath } from "@/lib/utils";
import type { ConnectionStatus, Workspace } from "@/types/electron";
import { getSessionHarnessId, parseProjectKey } from "@/hooks/agent-session-utils";
import { parseProjectKey } from "@/hooks/agent-session-utils";
import type { HarnessId } from "@/agents";

function getSidebarSessionSortTime(session: Session, sessionMeta: SessionMetaMap) {
Expand Down Expand Up @@ -46,15 +46,9 @@ export function shouldShowSessionInChatList({
export function sortSessionsForSidebar(
items: Session[],
sessionMeta: SessionMetaMap,
preferredHarnessId?: HarnessId | null,
_preferredHarnessId?: HarnessId | null,
) {
return [...items].sort((a, b) => {
if (preferredHarnessId) {
const aPreferred = getSessionHarnessId(a) === preferredHarnessId;
const bPreferred = getSessionHarnessId(b) === preferredHarnessId;
if (aPreferred !== bPreferred) return aPreferred ? -1 : 1;
}

const byUpdated =
getSidebarSessionSortTime(b, sessionMeta) - getSidebarSessionSortTime(a, sessionMeta);
if (byUpdated !== 0) return byUpdated;
Expand Down
Loading