conversationFindIndex.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import type {
  2. AssistantTextRow,
  3. UserInputRow,
  4. } from "@zcode/shared/zcode-protocol-v4";
  5. import type { ChatSearchResultHighlightRequest } from "@/v4/legacyChatViewTypes.js";
  6. import type { ConversationTurnRenderUnit } from "@/v4/conversationTurnRenderUnits.js";
  7. import { projectAssistantCodeComments } from "@/lib/assistantCodeComment.js";
  8. export type ConversationFindRowKind = "userInput" | "assistantText";
  9. export interface ConversationFindMatch {
  10. globalIndex: number;
  11. unitIndex: number;
  12. rowId: number;
  13. rowKind: ConversationFindRowKind;
  14. rowMatchIndex: number;
  15. start: number;
  16. end: number;
  17. sourceText: string;
  18. }
  19. export interface ConversationFindIndex {
  20. query: string;
  21. matches: ConversationFindMatch[];
  22. matchCount: number;
  23. loadedRowCount: number;
  24. }
  25. export interface ConversationFindMatchKey {
  26. rowId: number;
  27. rowKind: ConversationFindRowKind;
  28. rowMatchIndex: number;
  29. }
  30. interface ConversationFindTarget {
  31. unitIndex: number;
  32. rowId: number;
  33. rowKind: ConversationFindRowKind;
  34. text: string;
  35. }
  36. export function normalizeConversationFindQuery(query: string): string {
  37. return query.trim().toLocaleLowerCase();
  38. }
  39. function addTargetsForUnit(
  40. targets: ConversationFindTarget[],
  41. unit: ConversationTurnRenderUnit,
  42. unitIndex: number,
  43. projectCodeComments: boolean,
  44. ) {
  45. const latestAssistantState = unit.latestAssistantTextRow?.state;
  46. const projectUnitCodeComments =
  47. projectCodeComments &&
  48. (unit.isRunning ||
  49. latestAssistantState === "complete" ||
  50. latestAssistantState === "interrupted");
  51. for (const row of unit.visibleUserInputs) {
  52. targets.push(createTarget(unitIndex, row));
  53. }
  54. for (const row of unit.assistantTextRows) {
  55. targets.push({
  56. ...createTarget(unitIndex, row),
  57. text:
  58. projectUnitCodeComments
  59. ? projectAssistantCodeComments(row.text, {
  60. streaming: row.state === "streaming",
  61. }).visibleText
  62. : row.text,
  63. });
  64. }
  65. }
  66. function createTarget(
  67. unitIndex: number,
  68. row: UserInputRow | AssistantTextRow,
  69. ): ConversationFindTarget {
  70. return {
  71. unitIndex,
  72. rowId: row.rowId,
  73. rowKind: row.kind,
  74. text: row.text,
  75. };
  76. }
  77. export function buildConversationFindIndex(
  78. units: readonly ConversationTurnRenderUnit[],
  79. query: string,
  80. options: { projectAssistantCodeComments?: boolean } = {},
  81. ): ConversationFindIndex {
  82. const normalizedQuery = normalizeConversationFindQuery(query);
  83. const targets: ConversationFindTarget[] = [];
  84. units.forEach((unit, unitIndex) =>
  85. addTargetsForUnit(
  86. targets,
  87. unit,
  88. unitIndex,
  89. options.projectAssistantCodeComments === true,
  90. ),
  91. );
  92. if (!normalizedQuery) {
  93. return {
  94. query: normalizedQuery,
  95. matches: [],
  96. matchCount: 0,
  97. loadedRowCount: targets.length,
  98. };
  99. }
  100. const matches: ConversationFindMatch[] = [];
  101. for (const target of targets) {
  102. const normalizedText = target.text.toLocaleLowerCase();
  103. let searchStart = 0;
  104. let rowMatchIndex = 0;
  105. while (searchStart < normalizedText.length) {
  106. const matchIndex = normalizedText.indexOf(normalizedQuery, searchStart);
  107. if (matchIndex === -1) {
  108. break;
  109. }
  110. matches.push({
  111. globalIndex: matches.length,
  112. unitIndex: target.unitIndex,
  113. rowId: target.rowId,
  114. rowKind: target.rowKind,
  115. rowMatchIndex,
  116. start: matchIndex,
  117. end: matchIndex + normalizedQuery.length,
  118. sourceText: target.text,
  119. });
  120. rowMatchIndex += 1;
  121. searchStart = matchIndex + normalizedQuery.length;
  122. }
  123. }
  124. return {
  125. query: normalizedQuery,
  126. matches,
  127. matchCount: matches.length,
  128. loadedRowCount: targets.length,
  129. };
  130. }
  131. export function getConversationFindMatchKey(
  132. match: ConversationFindMatch | null | undefined,
  133. ): ConversationFindMatchKey | null {
  134. if (!match) {
  135. return null;
  136. }
  137. return {
  138. rowId: match.rowId,
  139. rowKind: match.rowKind,
  140. rowMatchIndex: match.rowMatchIndex,
  141. };
  142. }
  143. export function findConversationMatchIndexByKey(
  144. index: ConversationFindIndex,
  145. key: ConversationFindMatchKey | null | undefined,
  146. ): number {
  147. if (!key) {
  148. return -1;
  149. }
  150. return index.matches.findIndex(
  151. (match) =>
  152. match.rowId === key.rowId &&
  153. match.rowKind === key.rowKind &&
  154. match.rowMatchIndex === key.rowMatchIndex,
  155. );
  156. }
  157. export function resolveConversationFindActiveIndex(
  158. index: ConversationFindIndex,
  159. preferredIndex: number,
  160. ): number {
  161. if (index.matchCount === 0) {
  162. return -1;
  163. }
  164. if (preferredIndex >= 0 && preferredIndex < index.matchCount) {
  165. return preferredIndex;
  166. }
  167. return 0;
  168. }
  169. export function resolveConversationFindActiveMatch(
  170. index: ConversationFindIndex,
  171. preferredIndex: number,
  172. ): ConversationFindMatch | null {
  173. const activeIndex = resolveConversationFindActiveIndex(index, preferredIndex);
  174. return activeIndex >= 0 ? (index.matches[activeIndex] ?? null) : null;
  175. }
  176. function normalizeSearchResultProbeText(text: string): string {
  177. return text.replace(/^\.{3}/, "").replace(/\.{3}$/, "").replace(/\s+/g, " ").trim();
  178. }
  179. function includesProbe(sourceText: string, probe: string): boolean {
  180. const normalizedSource = normalizeSearchResultProbeText(sourceText).toLocaleLowerCase();
  181. const normalizedProbe = normalizeSearchResultProbeText(probe).toLocaleLowerCase();
  182. return normalizedProbe.length > 0 && normalizedSource.includes(normalizedProbe);
  183. }
  184. export function resolveSearchResultHighlightMatch(
  185. index: ConversationFindIndex,
  186. request: ChatSearchResultHighlightRequest,
  187. ): ConversationFindMatch | null {
  188. if (index.matchCount === 0) {
  189. return null;
  190. }
  191. const snippet = request.snippet?.trim();
  192. if (snippet) {
  193. const snippetMatch = index.matches.find((match) =>
  194. includesProbe(match.sourceText, snippet),
  195. );
  196. if (snippetMatch) {
  197. return snippetMatch;
  198. }
  199. }
  200. const preferredIndex = request.snippetIndex ?? 0;
  201. return index.matches[preferredIndex] ?? index.matches[0] ?? null;
  202. }