All files / src/utils session.ts

100% Statements 16/16
100% Branches 10/10
100% Functions 3/3
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45            3x             3x   3x       97x         27x   20x   2x   2x   3x           97x 97x 95x 95x 94x    
import { MESSAGES } from "../locales/en";
import { SEARCH_CHUNK_TYPE, type SearchChunkType } from "../types";
 
/** The server's `SearchScope` vocabulary. It says "both" where the plugin says "all". */
export type SessionScope = "raw" | "wiki" | "both";
 
export const SESSION_SCOPE = {
    RAW: "raw",
    WIKI: "wiki",
    BOTH: "both",
} as const satisfies Record<string, SessionScope>;
 
/** Longest auto-derived title before it gets an ellipsis. Matches the server's TITLE_MAX_LEN. */
export const SESSION_TITLE_MAX_LEN = 60;
 
const TITLE_ELLIPSIS = "…";
 
/** Translate the plugin's chunk-type selection into a scope the server can parse. */
export function scopeFromChunkType(chunk: SearchChunkType): SessionScope {
    return chunk === SEARCH_CHUNK_TYPE.ALL ? SESSION_SCOPE.BOTH : chunk;
}
 
/** Inverse of `scopeFromChunkType`. Null for a scope this build doesn't know. */
export function chunkTypeFromScope(scope: string): SearchChunkType | null {
    switch (scope) {
        case SESSION_SCOPE.BOTH:
            return SEARCH_CHUNK_TYPE.ALL;
        case SESSION_SCOPE.WIKI:
            return SEARCH_CHUNK_TYPE.WIKI;
        case SESSION_SCOPE.RAW:
            return SEARCH_CHUNK_TYPE.RAW;
        default:
            return null;
    }
}
 
/** Title a session from its first user message: first line, truncated. Mirrors `derive_title`. */
export function deriveSessionTitle(text: string): string {
    const stripped = text.trim();
    if (!stripped) return MESSAGES.SESSIONS_UNTITLED;
    const first = stripped.split(/\r\n|[\r\n]/)[0];
    if (first.length > SESSION_TITLE_MAX_LEN) return first.slice(0, SESSION_TITLE_MAX_LEN) + TITLE_ELLIPSIS;
    return first;
}