History was a date-grouped list you scrolled. Finding a session from last week meant eyeballing every row. This adds a search field to the existing filter rail, next to the host picker. Search runs on the daemon, not the client. The daemon already holds every persisted agent in memory, so it ranks the whole history and the client never has to warn that it only looked at the pages it had loaded. A searched response is one ranked page with no cursor. The global top K is always contained in the union of each host's own top K, so merging per-host pages and slicing back to the limit is the exact global answer — no k-way merge, and no offset into a ranking that is recomputed per request, which would duplicate and skip rows as history changes underneath it. Past result 200 of a relevance order the answer is a narrower query, so the footer says that instead of offering a "Load more" that cannot deliver. Matching is the app's existing picker matcher, moved to the protocol package so app and daemon share one implementation instead of drifting into two "why didn't this show up?" rule sets. Typo tolerance is opt-in, so the comboboxes keep exact narrowing; the budget is per token, and four-character tokens get transpositions only, because at that length a free substitution turns "main" into "mail", "maid" and "rain". Ranked rows carry the spans they matched on, so results can mark them. The subsequence and typo tiers match characters the eye would never find unaided, and an unexplained row reads as a bug. The ranker computes the spans anyway; sending them keeps the client from deriving a second opinion that could disagree with the ranking it is explaining. Hosts that fail or are offline are now named above the list. Without that, "No sessions match" is a claim the app has no basis for. Gated on server_info.features.agentHistorySearch, checked once against the connected hosts. Reachability is reported by the banner instead, so one offline host in the registry no longer hides the field.
125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
import {
|
|
compareMatchScores,
|
|
type MatchScore,
|
|
scoreTextFields,
|
|
} from "@getpaseo/protocol/search/text-match";
|
|
|
|
interface CommandAutocompleteEntry {
|
|
command: {
|
|
name: string;
|
|
aliases?: readonly string[];
|
|
kind?: string;
|
|
};
|
|
}
|
|
|
|
interface InlineSkillCommandEntry extends CommandAutocompleteEntry {
|
|
source: "provider" | "client";
|
|
}
|
|
|
|
export type SlashCommandPosition = "start" | "inline";
|
|
|
|
export interface SlashCommandRange {
|
|
start: number;
|
|
end: number;
|
|
query: string;
|
|
position: SlashCommandPosition;
|
|
}
|
|
|
|
interface FindActiveSlashCommandInput {
|
|
text: string;
|
|
cursorIndex: number;
|
|
}
|
|
|
|
interface ApplySlashCommandReplacementInput {
|
|
text: string;
|
|
command: SlashCommandRange;
|
|
commandName: string;
|
|
}
|
|
|
|
interface ScoredCommandAutocompleteEntry<TEntry> {
|
|
entry: TEntry;
|
|
score: MatchScore;
|
|
}
|
|
|
|
function scoreCommandAutocompleteEntry(
|
|
entry: CommandAutocompleteEntry,
|
|
query: string,
|
|
): MatchScore | null {
|
|
return scoreTextFields(query, [entry.command.name, ...(entry.command.aliases ?? [])]);
|
|
}
|
|
|
|
export function filterAndRankCommandAutocompleteEntries<TEntry extends CommandAutocompleteEntry>(
|
|
entries: readonly TEntry[],
|
|
query: string,
|
|
): TEntry[] {
|
|
const normalizedQuery = query.trim().toLowerCase();
|
|
if (normalizedQuery.length === 0) {
|
|
return [...entries];
|
|
}
|
|
|
|
const scoredEntries: ScoredCommandAutocompleteEntry<TEntry>[] = [];
|
|
for (const entry of entries) {
|
|
const score = scoreCommandAutocompleteEntry(entry, normalizedQuery);
|
|
if (score) {
|
|
scoredEntries.push({ entry, score });
|
|
}
|
|
}
|
|
|
|
scoredEntries.sort((a, b) => {
|
|
const scoreComparison = compareMatchScores(a.score, b.score);
|
|
if (scoreComparison !== 0) {
|
|
return scoreComparison;
|
|
}
|
|
return a.entry.command.name.localeCompare(b.entry.command.name);
|
|
});
|
|
|
|
return scoredEntries.map((scored) => scored.entry);
|
|
}
|
|
|
|
export function filterInlineSkillCommandEntries<TEntry extends InlineSkillCommandEntry>(
|
|
entries: readonly TEntry[],
|
|
): TEntry[] {
|
|
return entries.filter((entry) => entry.source === "provider" && entry.command.kind === "skill");
|
|
}
|
|
|
|
const INVALID_SLASH_COMMAND_QUERY_CHARS = /[/\s\n\r\t"']/;
|
|
|
|
export function findActiveSlashCommand(
|
|
input: FindActiveSlashCommandInput,
|
|
): SlashCommandRange | null {
|
|
const clampedCursor = Math.max(0, Math.min(input.cursorIndex, input.text.length));
|
|
const beforeCursor = input.text.slice(0, clampedCursor);
|
|
|
|
for (
|
|
let slashIndex = beforeCursor.lastIndexOf("/");
|
|
slashIndex >= 0;
|
|
slashIndex = slashIndex === 0 ? -1 : beforeCursor.lastIndexOf("/", slashIndex - 1)
|
|
) {
|
|
const previousCharacter = slashIndex > 0 ? input.text[slashIndex - 1] : "";
|
|
if (previousCharacter && !/\s/.test(previousCharacter)) {
|
|
continue;
|
|
}
|
|
|
|
const query = beforeCursor.slice(slashIndex + 1);
|
|
if (INVALID_SLASH_COMMAND_QUERY_CHARS.test(query)) {
|
|
continue;
|
|
}
|
|
|
|
return {
|
|
start: slashIndex,
|
|
end: clampedCursor,
|
|
query,
|
|
position: slashIndex === 0 ? "start" : "inline",
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function applySlashCommandReplacement(input: ApplySlashCommandReplacementInput): string {
|
|
const before = input.text.slice(0, input.command.start);
|
|
const after = input.text.slice(input.command.end);
|
|
const replacement = `${before}/${input.commandName}${after}`;
|
|
return input.command.end === input.text.length ? `${replacement} ` : replacement;
|
|
}
|