| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- import type {
- AssistantTextRow,
- UserInputRow,
- } from "@zcode/shared/zcode-protocol-v4";
- import type { ChatSearchResultHighlightRequest } from "@/v4/legacyChatViewTypes.js";
- import type { ConversationTurnRenderUnit } from "@/v4/conversationTurnRenderUnits.js";
- import { projectAssistantCodeComments } from "@/lib/assistantCodeComment.js";
- export type ConversationFindRowKind = "userInput" | "assistantText";
- export interface ConversationFindMatch {
- globalIndex: number;
- unitIndex: number;
- rowId: number;
- rowKind: ConversationFindRowKind;
- rowMatchIndex: number;
- start: number;
- end: number;
- sourceText: string;
- }
- export interface ConversationFindIndex {
- query: string;
- matches: ConversationFindMatch[];
- matchCount: number;
- loadedRowCount: number;
- }
- export interface ConversationFindMatchKey {
- rowId: number;
- rowKind: ConversationFindRowKind;
- rowMatchIndex: number;
- }
- interface ConversationFindTarget {
- unitIndex: number;
- rowId: number;
- rowKind: ConversationFindRowKind;
- text: string;
- }
- export function normalizeConversationFindQuery(query: string): string {
- return query.trim().toLocaleLowerCase();
- }
- function addTargetsForUnit(
- targets: ConversationFindTarget[],
- unit: ConversationTurnRenderUnit,
- unitIndex: number,
- projectCodeComments: boolean,
- ) {
- const latestAssistantState = unit.latestAssistantTextRow?.state;
- const projectUnitCodeComments =
- projectCodeComments &&
- (unit.isRunning ||
- latestAssistantState === "complete" ||
- latestAssistantState === "interrupted");
- for (const row of unit.visibleUserInputs) {
- targets.push(createTarget(unitIndex, row));
- }
- for (const row of unit.assistantTextRows) {
- targets.push({
- ...createTarget(unitIndex, row),
- text:
- projectUnitCodeComments
- ? projectAssistantCodeComments(row.text, {
- streaming: row.state === "streaming",
- }).visibleText
- : row.text,
- });
- }
- }
- function createTarget(
- unitIndex: number,
- row: UserInputRow | AssistantTextRow,
- ): ConversationFindTarget {
- return {
- unitIndex,
- rowId: row.rowId,
- rowKind: row.kind,
- text: row.text,
- };
- }
- export function buildConversationFindIndex(
- units: readonly ConversationTurnRenderUnit[],
- query: string,
- options: { projectAssistantCodeComments?: boolean } = {},
- ): ConversationFindIndex {
- const normalizedQuery = normalizeConversationFindQuery(query);
- const targets: ConversationFindTarget[] = [];
- units.forEach((unit, unitIndex) =>
- addTargetsForUnit(
- targets,
- unit,
- unitIndex,
- options.projectAssistantCodeComments === true,
- ),
- );
- if (!normalizedQuery) {
- return {
- query: normalizedQuery,
- matches: [],
- matchCount: 0,
- loadedRowCount: targets.length,
- };
- }
- const matches: ConversationFindMatch[] = [];
- for (const target of targets) {
- const normalizedText = target.text.toLocaleLowerCase();
- let searchStart = 0;
- let rowMatchIndex = 0;
- while (searchStart < normalizedText.length) {
- const matchIndex = normalizedText.indexOf(normalizedQuery, searchStart);
- if (matchIndex === -1) {
- break;
- }
- matches.push({
- globalIndex: matches.length,
- unitIndex: target.unitIndex,
- rowId: target.rowId,
- rowKind: target.rowKind,
- rowMatchIndex,
- start: matchIndex,
- end: matchIndex + normalizedQuery.length,
- sourceText: target.text,
- });
- rowMatchIndex += 1;
- searchStart = matchIndex + normalizedQuery.length;
- }
- }
- return {
- query: normalizedQuery,
- matches,
- matchCount: matches.length,
- loadedRowCount: targets.length,
- };
- }
- export function getConversationFindMatchKey(
- match: ConversationFindMatch | null | undefined,
- ): ConversationFindMatchKey | null {
- if (!match) {
- return null;
- }
- return {
- rowId: match.rowId,
- rowKind: match.rowKind,
- rowMatchIndex: match.rowMatchIndex,
- };
- }
- export function findConversationMatchIndexByKey(
- index: ConversationFindIndex,
- key: ConversationFindMatchKey | null | undefined,
- ): number {
- if (!key) {
- return -1;
- }
- return index.matches.findIndex(
- (match) =>
- match.rowId === key.rowId &&
- match.rowKind === key.rowKind &&
- match.rowMatchIndex === key.rowMatchIndex,
- );
- }
- export function resolveConversationFindActiveIndex(
- index: ConversationFindIndex,
- preferredIndex: number,
- ): number {
- if (index.matchCount === 0) {
- return -1;
- }
- if (preferredIndex >= 0 && preferredIndex < index.matchCount) {
- return preferredIndex;
- }
- return 0;
- }
- export function resolveConversationFindActiveMatch(
- index: ConversationFindIndex,
- preferredIndex: number,
- ): ConversationFindMatch | null {
- const activeIndex = resolveConversationFindActiveIndex(index, preferredIndex);
- return activeIndex >= 0 ? (index.matches[activeIndex] ?? null) : null;
- }
- function normalizeSearchResultProbeText(text: string): string {
- return text.replace(/^\.{3}/, "").replace(/\.{3}$/, "").replace(/\s+/g, " ").trim();
- }
- function includesProbe(sourceText: string, probe: string): boolean {
- const normalizedSource = normalizeSearchResultProbeText(sourceText).toLocaleLowerCase();
- const normalizedProbe = normalizeSearchResultProbeText(probe).toLocaleLowerCase();
- return normalizedProbe.length > 0 && normalizedSource.includes(normalizedProbe);
- }
- export function resolveSearchResultHighlightMatch(
- index: ConversationFindIndex,
- request: ChatSearchResultHighlightRequest,
- ): ConversationFindMatch | null {
- if (index.matchCount === 0) {
- return null;
- }
- const snippet = request.snippet?.trim();
- if (snippet) {
- const snippetMatch = index.matches.find((match) =>
- includesProbe(match.sourceText, snippet),
- );
- if (snippetMatch) {
- return snippetMatch;
- }
- }
- const preferredIndex = request.snippetIndex ?? 0;
- return index.matches[preferredIndex] ?? index.matches[0] ?? null;
- }
|