Sessions no longer take their title from the first message: once the first exchange is sent, the text model writes the title and summary, which the rail and sessions list show. The summary records how many messages it covers so a stale one is rewritten before it is carried into the next session. The sessions hook now reloads on live message and session events, so a session started by the first message, a rollover or another tab appears without a reload. New sessions are pushed as server:bot-session, both from the process that changed them and through the live relay.
36 lines
965 B
TypeScript
36 lines
965 B
TypeScript
type Params = {
|
|
text?: string | null;
|
|
};
|
|
|
|
export type SessionName = {
|
|
title: string;
|
|
summary: string;
|
|
};
|
|
|
|
/**
|
|
* Reads the JSON a naming model answers with: its title and summary.
|
|
* Tolerates a surrounding code fence or a sentence around the object.
|
|
* Null when either is missing.
|
|
*/
|
|
export default function parseSessionName({ text }: Params): SessionName | null {
|
|
if (!text) return null;
|
|
|
|
const start = text.indexOf("{");
|
|
const end = text.lastIndexOf("}");
|
|
|
|
if (start < 0 || end <= start) return null;
|
|
|
|
try {
|
|
const value = JSON.parse(text.slice(start, end + 1));
|
|
|
|
if (!value || typeof value != "object" || Array.isArray(value)) return null;
|
|
|
|
const title = typeof value.title == "string" ? value.title.trim() : "";
|
|
const summary = typeof value.summary == "string" ? value.summary.trim() : "";
|
|
|
|
return title && summary ? { title, summary } : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|