Skip to content

Commit a5eb023

Browse files
efecnccursoragent
andauthored
feat(agent-ui): insert session after active tab (A6.192) (#593)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 977de0d commit a5eb023

5 files changed

Lines changed: 47 additions & 10 deletions

File tree

packages/agent-ui/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ consumption of shared panel shells:
307307
| A6.189 | Desktop New chat uses DEFAULT_SESSION_TITLE |
308308
| A6.190 | Deleted session id blocklist ops |
309309
| A6.191 | Hydrate active session resolve |
310+
| A6.192 | Insert session after active tab |
310311

311312
### A5 complete enough
312313

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
import { insertSessionAfterActive } from "../lib/insertSessionAfterActive.js";
3+
4+
describe("insertSessionAfterActive", () => {
5+
it("inserts after active", () => {
6+
expect(
7+
insertSessionAfterActive(
8+
[{ id: "a" }, { id: "b" }],
9+
"a",
10+
{ id: "n" },
11+
),
12+
).toEqual([{ id: "a" }, { id: "n" }, { id: "b" }]);
13+
});
14+
it("appends when missing active", () => {
15+
expect(insertSessionAfterActive([{ id: "a" }], "z", { id: "n" })).toEqual([
16+
{ id: "a" },
17+
{ id: "n" },
18+
]);
19+
});
20+
});

packages/agent-ui/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ export {
892892
resolveActiveSessionOnHydrate,
893893
createUntitledSessionMeta,
894894
} from "./lib/resolveActiveSessionOnHydrate.js";
895+
export { insertSessionAfterActive } from "./lib/insertSessionAfterActive.js";
895896
export type { SessionIdTitle } from "./lib/resolveActiveSessionOnHydrate.js";
896897
export type {
897898
UntitledSessionMeta,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Pure session-list insert after the active tab (A6.192).
3+
*/
4+
5+
/** Insert `meta` immediately after the active session (or append). */
6+
export function insertSessionAfterActive<T extends { id: string }>(
7+
sessions: readonly T[],
8+
activeId: string | null | undefined,
9+
meta: T,
10+
): T[] {
11+
const activeIdx = activeId
12+
? sessions.findIndex((s) => s.id === activeId)
13+
: -1;
14+
if (activeIdx === -1) return [...sessions, meta];
15+
return [
16+
...sessions.slice(0, activeIdx + 1),
17+
meta,
18+
...sessions.slice(activeIdx + 1),
19+
];
20+
}

src/modules/ai/store/chatStore.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
appendDeletedSessionId,
55
resolveActiveSessionOnHydrate,
66
createUntitledSessionMeta,
7+
insertSessionAfterActive,
78
} from "@altai/agent-ui";
89
import type { UIMessage } from "ai";
910
import { native } from "../lib/native";
@@ -900,17 +901,11 @@ export const useChatStore = create<StoreState>((set, get) => ({
900901
updatedAt: Date.now(),
901902
};
902903
const current = get().sessions;
903-
const activeIdx = current.findIndex(
904-
(s) => s.id === get().activeSessionId,
904+
const next = insertSessionAfterActive(
905+
current,
906+
get().activeSessionId,
907+
meta,
905908
);
906-
const next =
907-
activeIdx === -1
908-
? [...current, meta]
909-
: [
910-
...current.slice(0, activeIdx + 1),
911-
meta,
912-
...current.slice(activeIdx + 1),
913-
];
914909
set({
915910
sessions: next,
916911
activeSessionId: id,

0 commit comments

Comments
 (0)