fix(tui): use canonical session handles (#6455)

This commit is contained in:
chengyongru authored and GitHub committed 2026-10-25 21:52:58 +08:00
1 parent 424d2debc2
commit b048cf44f7
6 files changed
+146 -38

No files matched your search

+19 -26
View File
@@ -25,6 +25,7 @@ import {
} from "./validation"
const SKILL_REFERENCE_NAME = /^[A-Za-z0-9_-]+$/u
const SESSION_HANDLE_NAME = /^[a-z]{4,16}$/u
const SLASH_COMMAND_LIFECYCLES = new Set([
"side_channel",
"finalize_active_turn",
@@ -415,8 +416,13 @@ export async function fetchSessions(
const chatId = value.key.slice("websocket:".length)
if (!chatId) return []
const titleOverride = titles[value.key]
const handle = value.handle
return [{
key: value.key,
chatId,
...(isRecord(handle) && typeof handle.id === "string" && handle.id
&& typeof handle.name === "string" && SESSION_HANDLE_NAME.test(handle.name)
? { handle: { id: handle.id, name: handle.name } } : {}),
title: typeof titleOverride === "string"
? titleOverride
: typeof value.title === "string" ? value.title : "",
@@ -437,16 +443,6 @@ export async function fetchSessions(
})
}
function sessionMentionName(session: SessionSummary): string {
const label = (session.title || session.preview || "session")
.normalize("NFKC")
.replace(/\s+/gu, "-")
.replace(/[^\p{L}\p{N}_-]+/gu, "")
.replace(/-+/gu, "-")
.replace(/^-|-$/gu, "")
return Array.from(label || "session").slice(0, 40).join("")
}
/** Installed capabilities and saved chats share one mention namespace. */
export async function fetchMentionCandidates(
apiUrl: string,
@@ -464,7 +460,18 @@ export async function fetchMentionCandidates(
).catch(() => null),
fetchApi(apiUrl, apiToken, "/api/settings/mcp-presets", reauthenticate).catch(() => null),
])
const used = new Set<string>()
const sessionCandidates = sessions.flatMap<MentionCandidate>((session) => session.handle ? [{
kind: "session",
name: session.handle.name,
displayName: sessionLabelForMention(session),
description: session.preview || "Saved session",
session: {
...session.handle,
session_key: session.key,
title: session.title || undefined,
},
}] : [])
const used = new Set(sessionCandidates.map((candidate) => candidate.name.toLocaleLowerCase()))
const uniqueName = (raw: string) => {
const base = raw || "session"
let name = base
@@ -507,21 +514,7 @@ export async function fetchMentionCandidates(
})
}
}
for (const session of sessions) {
const name = uniqueName(sessionMentionName(session))
candidates.push({
kind: "session",
name,
displayName: sessionLabelForMention(session),
description: session.preview || "Saved session",
session: {
name,
session_key: `websocket:${session.chatId}`,
title: session.title || undefined,
},
})
}
return candidates
return [...candidates, ...sessionCandidates]
}
function sessionLabelForMention(session: SessionSummary): string {
+82 -3
View File
@@ -1319,6 +1319,7 @@ describe("gateway protocol", () => {
sessions: [
{
key: "websocket:chat-1",
handle: { id: "handle_plan", name: "luma" },
title: "Release plan",
preview: "Prepare the release",
created_at: "2026-08-12T10:00:00Z",
@@ -1334,6 +1335,8 @@ describe("gateway protocol", () => {
try {
expect(await fetchSessions("http://nanobot.test", "secret")).toEqual([{
key: "websocket:chat-1",
handle: { id: "handle_plan", name: "luma" },
chatId: "chat-1",
title: "Pinned release",
preview: "Prepare the release",
@@ -1371,7 +1374,12 @@ describe("gateway protocol", () => {
}
if (url.includes("sidebar-state")) return Promise.resolve(new Response("{}"))
return Promise.resolve(new Response(JSON.stringify({
sessions: [{ key: "websocket:plan", title: "Release plan", preview: "Ship it" }],
sessions: [{
key: "websocket:plan",
handle: { id: "handle_plan", name: "luma" },
title: "Release plan",
preview: "Ship it",
}],
})))
}) as typeof fetch
@@ -1391,11 +1399,12 @@ describe("gateway protocol", () => {
},
{
kind: "session",
name: "Release-plan",
name: "luma",
displayName: "Release plan",
description: "Ship it",
session: {
name: "Release-plan",
id: "handle_plan",
name: "luma",
session_key: "websocket:plan",
title: "Release plan",
},
@@ -1405,6 +1414,76 @@ describe("gateway protocol", () => {
globalThis.fetch = original
}
})
test("reserves canonical session names across tools, renames, and list reordering", async () => {
const original = globalThis.fetch
let renamed = false
const sessions = [
{ key: "websocket:plan", handle: { id: "handle_plan", name: "luma" }, title: "Same title" },
{ key: "websocket:notes", handle: { id: "handle_notes", name: "bime" }, title: "Same title" },
]
globalThis.fetch = ((input: string | URL | Request) => {
const url = String(input)
if (url.includes("cli-apps")) return Promise.resolve(Response.json({
apps: [{ name: "luma", installed: true }],
}))
if (url.includes("mcp-presets")) return Promise.resolve(Response.json({
presets: [{ name: "LUMA", installed: true, configured: true }],
}))
if (url.includes("sidebar-state")) return Promise.resolve(Response.json({
title_overrides: renamed ? { "websocket:plan": "Renamed chat" } : {},
}))
return Promise.resolve(Response.json({ sessions: renamed ? [...sessions].reverse() : sessions }))
}) as typeof fetch
try {
const before = await fetchMentionCandidates("http://nanobot.test", "secret")
expect(before.map(({ kind, name, targetName }) => ({ kind, name, targetName }))).toEqual([
{ kind: "cli", name: "luma-2", targetName: "luma" },
{ kind: "mcp", name: "LUMA-3", targetName: "LUMA" },
{ kind: "session", name: "luma", targetName: undefined },
{ kind: "session", name: "bime", targetName: undefined },
])
expect(before.filter((item) => item.kind === "session").map((item) => item.displayName))
.toEqual(["Same title", "Same title"])
renamed = true
const after = await fetchMentionCandidates("http://nanobot.test", "secret")
expect(after.filter((item) => item.kind === "session").map((item) => item.session)).toEqual([
{ id: "handle_notes", name: "bime", session_key: "websocket:notes", title: "Same title" },
{ id: "handle_plan", name: "luma", session_key: "websocket:plan", title: "Renamed chat" },
])
expect(after.find((item) => item.name === "luma")?.displayName).toBe("Renamed chat")
expect(after.slice(0, 2)).toEqual(before.slice(0, 2))
} finally {
globalThis.fetch = original
}
})
test("keeps sessions navigable without inventing handles for missing or malformed API identities", async () => {
const original = globalThis.fetch
globalThis.fetch = ((input: string | URL | Request) => Promise.resolve(Response.json(
String(input).endsWith("/api/sessions") ? {
sessions: [
{ key: "websocket:missing", title: "Missing handle" },
{ key: "websocket:invalid", handle: { id: 42, name: "luma" }, title: "Invalid handle" },
{ key: "websocket:alias", handle: { id: "handle_alias", name: "Release-plan" }, title: "Old alias" },
],
} : {},
))) as typeof fetch
try {
const sessions = await fetchSessions("http://nanobot.test", "secret")
expect(sessions.map((session) => [session.key, session.title, session.handle])).toEqual([
["websocket:missing", "Missing handle", undefined],
["websocket:invalid", "Invalid handle", undefined],
["websocket:alias", "Old alias", undefined],
])
expect(await fetchMentionCandidates("http://nanobot.test", "secret")).toEqual([])
} finally {
globalThis.fetch = original
}
})
})
+3
View File
@@ -279,6 +279,7 @@ export interface SessionContextSnapshot {
}
interface SessionMention {
id?: string
name: string
session_key: string
title?: string
@@ -324,7 +325,9 @@ export type SlashCommandLifecycle =
| "agent_turn_with_args"
export interface SessionSummary {
key: string
chatId: string
handle?: { id: string; name: string }
title: string
preview: string
createdAt: string | null