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
1 change: 1 addition & 0 deletions packages/agent-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ consumption of shared panel shells:
| A6.193 | Rename session title in list |
| A6.194 | Apply session workspace target |
| A6.195 | Remove session + next active id |
| A6.196 | Maybe auto-derive untitled session title |

### A5 complete enough

Expand Down
23 changes: 23 additions & 0 deletions packages/agent-ui/src/__tests__/maybeDeriveSessionTitle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from "vitest";
import { DEFAULT_SESSION_TITLE } from "../lib/backendSessionTitle.js";
import { maybeDeriveSessionTitleList } from "../lib/maybeDeriveSessionTitle.js";

describe("maybeDeriveSessionTitleList", () => {
it("renames untitled only when changed", () => {
const sessions = [
{ id: "a", title: DEFAULT_SESSION_TITLE, updatedAt: 1 },
];
expect(maybeDeriveSessionTitleList(sessions, "a", "Hello", 2)?.[0]).toMatchObject({
title: "Hello",
updatedAt: 2,
});
expect(maybeDeriveSessionTitleList(sessions, "a", DEFAULT_SESSION_TITLE)).toBeNull();
expect(
maybeDeriveSessionTitleList(
[{ id: "a", title: "Fixed", updatedAt: 1 }],
"a",
"Hello",
),
).toBeNull();
});
});
2 changes: 2 additions & 0 deletions packages/agent-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ export {
removeSessionFromList,
nextActiveIdAfterDelete,
} from "./lib/removeSessionFromList.js";
export { maybeDeriveSessionTitleList } from "./lib/maybeDeriveSessionTitle.js";
export type { DerivableSession } from "./lib/maybeDeriveSessionTitle.js";
export type {
SessionWorkspaceTarget,
SessionWorkspaceFields,
Expand Down
25 changes: 25 additions & 0 deletions packages/agent-ui/src/lib/maybeDeriveSessionTitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Pure auto-title update decision for untitled sessions (A6.196).
*/

import { isUntitledSessionTitle } from "./isUntitledSessionTitle.js";
import { renameSessionInList } from "./renameSessionInList.js";

export type DerivableSession = { id: string; title: string; updatedAt: number };

/**
* When a session is still untitled and `nextTitle` differs, return a renamed
* list; otherwise `null` so the host can skip store writes.
*/
export function maybeDeriveSessionTitleList<T extends DerivableSession>(
sessions: readonly T[],
id: string,
nextTitle: string,
now: number = Date.now(),
): T[] | null {
const meta = sessions.find((s) => s.id === id);
if (!meta) return null;
if (!isUntitledSessionTitle(meta.title)) return null;
if (nextTitle === meta.title) return null;
return renameSessionInList(sessions, id, nextTitle, now);
}
12 changes: 3 additions & 9 deletions src/modules/ai/store/chatStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
DEFAULT_SESSION_TITLE,
isUntitledSessionTitle,
appendDeletedSessionId,
resolveActiveSessionOnHydrate,
createUntitledSessionMeta,
Expand All @@ -9,6 +8,7 @@ import {
applySessionWorkspaceTarget,
removeSessionFromList,
nextActiveIdAfterDelete,
maybeDeriveSessionTitleList,
} from "@altai/agent-ui";
import type { UIMessage } from "ai";
import { native } from "../lib/native";
Expand Down Expand Up @@ -410,15 +410,9 @@ function persistNativeMessages(id: string, messages: UIMessage[]): void {
// otherwise we'd rewrite the sessions array (and trigger re-renders + a
// store write) on every streamed event.
const state = useChatStore.getState();
const meta = state.sessions.find((s) => s.id === id);
if (!meta) return;
const isUntitled = isUntitledSessionTitle(meta.title);
if (!isUntitled) return;
const nextTitle = deriveTitle(messages);
if (nextTitle === meta.title) return;
const next = state.sessions.map((s) =>
s.id === id ? { ...s, title: nextTitle, updatedAt: Date.now() } : s,
);
const next = maybeDeriveSessionTitleList(state.sessions, id, nextTitle);
if (!next) return;
useChatStore.setState({ sessions: next });
void saveSessionsList(next);
}
Expand Down
Loading