agenticSessionSearch.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import type { LogOption, SerializedMessage } from '../types/logs.js'
  2. import { count } from './array.js'
  3. import { logForDebugging } from './debug.js'
  4. import { getLogDisplayTitle, logError } from './log.js'
  5. import { getSmallFastModel } from './model/model.js'
  6. import { isLiteLog, loadFullLog } from './sessionStorage.js'
  7. import { sideQuery } from './sideQuery.js'
  8. import { jsonParse } from './slowOperations.js'
  9. // Limits for transcript extraction
  10. const MAX_TRANSCRIPT_CHARS = 2000 // Max chars of transcript per session
  11. const MAX_MESSAGES_TO_SCAN = 100 // Max messages to scan from start/end
  12. const MAX_SESSIONS_TO_SEARCH = 100 // Max sessions to send to the API
  13. const SESSION_SEARCH_SYSTEM_PROMPT = `Your goal is to find relevant sessions based on a user's search query.
  14. You will be given a list of sessions with their metadata and a search query. Identify which sessions are most relevant to the query.
  15. Each session may include:
  16. - Title (display name or custom title)
  17. - Tag (user-assigned category, shown as [tag: name] - users tag sessions with /tag command to categorize them)
  18. - Branch (git branch name, shown as [branch: name])
  19. - Summary (AI-generated summary)
  20. - First message (beginning of the conversation)
  21. - Transcript (excerpt of conversation content)
  22. IMPORTANT: Tags are user-assigned labels that indicate the session's topic or category. If the query matches a tag exactly or partially, those sessions should be highly prioritized.
  23. For each session, consider (in order of priority):
  24. 1. Exact tag matches (highest priority - user explicitly categorized this session)
  25. 2. Partial tag matches or tag-related terms
  26. 3. Title matches (custom titles or first message content)
  27. 4. Branch name matches
  28. 5. Summary and transcript content matches
  29. 6. Semantic similarity and related concepts
  30. CRITICAL: Be VERY inclusive in your matching. Include sessions that:
  31. - Contain the query term anywhere in any field
  32. - Are semantically related to the query (e.g., "testing" matches sessions about "tests", "unit tests", "QA", etc.)
  33. - Discuss topics that could be related to the query
  34. - Have transcripts that mention the concept even in passing
  35. When in doubt, INCLUDE the session. It's better to return too many results than too few. The user can easily scan through results, but missing relevant sessions is frustrating.
  36. Return sessions ordered by relevance (most relevant first). If truly no sessions have ANY connection to the query, return an empty array - but this should be rare.
  37. Respond with ONLY the JSON object, no markdown formatting:
  38. {"relevant_indices": [2, 5, 0]}`
  39. type AgenticSearchResult = {
  40. relevant_indices: number[]
  41. }
  42. /**
  43. * Extracts searchable text content from a message.
  44. */
  45. function extractMessageText(message: SerializedMessage): string {
  46. if (message.type !== 'user' && message.type !== 'assistant') {
  47. return ''
  48. }
  49. const content = 'message' in message ? message.message?.content : undefined
  50. if (!content) return ''
  51. if (typeof content === 'string') {
  52. return content
  53. }
  54. if (Array.isArray(content)) {
  55. return content
  56. .map(block => {
  57. if (typeof block === 'string') return block
  58. if ('text' in block && typeof block.text === 'string') return block.text
  59. return ''
  60. })
  61. .filter(Boolean)
  62. .join(' ')
  63. }
  64. return ''
  65. }
  66. /**
  67. * Extracts a truncated transcript from session messages.
  68. */
  69. function extractTranscript(messages: SerializedMessage[]): string {
  70. if (messages.length === 0) return ''
  71. // Take messages from start and end to get context
  72. const messagesToScan =
  73. messages.length <= MAX_MESSAGES_TO_SCAN
  74. ? messages
  75. : [
  76. ...messages.slice(0, MAX_MESSAGES_TO_SCAN / 2),
  77. ...messages.slice(-MAX_MESSAGES_TO_SCAN / 2),
  78. ]
  79. const text = messagesToScan
  80. .map(extractMessageText)
  81. .filter(Boolean)
  82. .join(' ')
  83. .replace(/\s+/g, ' ')
  84. .trim()
  85. return text.length > MAX_TRANSCRIPT_CHARS
  86. ? text.slice(0, MAX_TRANSCRIPT_CHARS) + '…'
  87. : text
  88. }
  89. /**
  90. * Checks if a log contains the query term in any searchable field.
  91. */
  92. function logContainsQuery(log: LogOption, queryLower: string): boolean {
  93. // Check title
  94. const title = getLogDisplayTitle(log).toLowerCase()
  95. if (title.includes(queryLower)) return true
  96. // Check custom title
  97. if (log.customTitle?.toLowerCase().includes(queryLower)) return true
  98. // Check tag
  99. if (log.tag?.toLowerCase().includes(queryLower)) return true
  100. // Check branch
  101. if (log.gitBranch?.toLowerCase().includes(queryLower)) return true
  102. // Check summary
  103. if (log.summary?.toLowerCase().includes(queryLower)) return true
  104. // Check first prompt
  105. if (log.firstPrompt?.toLowerCase().includes(queryLower)) return true
  106. // Check transcript (more expensive, do last)
  107. if (log.messages && log.messages.length > 0) {
  108. const transcript = extractTranscript(log.messages).toLowerCase()
  109. if (transcript.includes(queryLower)) return true
  110. }
  111. return false
  112. }
  113. /**
  114. * Performs an agentic search using Claude to find relevant sessions
  115. * based on semantic understanding of the query.
  116. */
  117. export async function agenticSessionSearch(
  118. query: string,
  119. logs: LogOption[],
  120. signal?: AbortSignal,
  121. ): Promise<LogOption[]> {
  122. if (!query.trim() || logs.length === 0) {
  123. return []
  124. }
  125. const queryLower = query.toLowerCase()
  126. // Pre-filter: find sessions that contain the query term
  127. // This ensures we search relevant sessions, not just recent ones
  128. const matchingLogs = logs.filter(log => logContainsQuery(log, queryLower))
  129. // Take up to MAX_SESSIONS_TO_SEARCH matching logs
  130. // If fewer matches, fill remaining slots with recent non-matching logs for context
  131. let logsToSearch: LogOption[]
  132. if (matchingLogs.length >= MAX_SESSIONS_TO_SEARCH) {
  133. logsToSearch = matchingLogs.slice(0, MAX_SESSIONS_TO_SEARCH)
  134. } else {
  135. const nonMatchingLogs = logs.filter(
  136. log => !logContainsQuery(log, queryLower),
  137. )
  138. const remainingSlots = MAX_SESSIONS_TO_SEARCH - matchingLogs.length
  139. logsToSearch = [
  140. ...matchingLogs,
  141. ...nonMatchingLogs.slice(0, remainingSlots),
  142. ]
  143. }
  144. // Debug: log what data we have
  145. logForDebugging(
  146. `Agentic search: ${logsToSearch.length}/${logs.length} logs, query="${query}", ` +
  147. `matching: ${matchingLogs.length}, with messages: ${count(logsToSearch, l => l.messages?.length > 0)}`,
  148. )
  149. // Load full logs for lite logs to get transcript content
  150. const logsWithTranscriptsPromises = logsToSearch.map(async log => {
  151. if (isLiteLog(log)) {
  152. try {
  153. return await loadFullLog(log)
  154. } catch (error) {
  155. logError(error as Error)
  156. // If loading fails, use the lite log (no transcript)
  157. return log
  158. }
  159. }
  160. return log
  161. })
  162. const logsWithTranscripts = await Promise.all(logsWithTranscriptsPromises)
  163. logForDebugging(
  164. `Agentic search: loaded ${count(logsWithTranscripts, l => l.messages?.length > 0)}/${logsToSearch.length} logs with transcripts`,
  165. )
  166. // Build session list for the prompt with all searchable metadata
  167. const sessionList = logsWithTranscripts
  168. .map((log, index) => {
  169. const parts: string[] = [`${index}:`]
  170. // Title (display title, may be custom or from first prompt)
  171. const displayTitle = getLogDisplayTitle(log)
  172. parts.push(displayTitle)
  173. // Custom title if different from display title
  174. if (log.customTitle && log.customTitle !== displayTitle) {
  175. parts.push(`[custom title: ${log.customTitle}]`)
  176. }
  177. // Tag
  178. if (log.tag) {
  179. parts.push(`[tag: ${log.tag}]`)
  180. }
  181. // Git branch
  182. if (log.gitBranch) {
  183. parts.push(`[branch: ${log.gitBranch}]`)
  184. }
  185. // Summary
  186. if (log.summary) {
  187. parts.push(`- Summary: ${log.summary}`)
  188. }
  189. // First prompt content (truncated)
  190. if (log.firstPrompt && log.firstPrompt !== 'No prompt') {
  191. parts.push(`- First message: ${log.firstPrompt.slice(0, 300)}`)
  192. }
  193. // Transcript excerpt (if messages are available)
  194. if (log.messages && log.messages.length > 0) {
  195. const transcript = extractTranscript(log.messages)
  196. if (transcript) {
  197. parts.push(`- Transcript: ${transcript}`)
  198. }
  199. }
  200. return parts.join(' ')
  201. })
  202. .join('\n')
  203. const userMessage = `Sessions:
  204. ${sessionList}
  205. Search query: "${query}"
  206. Find the sessions that are most relevant to this query.`
  207. // Debug: log first part of the session list
  208. logForDebugging(
  209. `Agentic search prompt (first 500 chars): ${userMessage.slice(0, 500)}...`,
  210. )
  211. try {
  212. const model = getSmallFastModel()
  213. logForDebugging(`Agentic search using model: ${model}`)
  214. const response = await sideQuery({
  215. model,
  216. system: SESSION_SEARCH_SYSTEM_PROMPT,
  217. messages: [{ role: 'user', content: userMessage }],
  218. signal,
  219. querySource: 'session_search',
  220. })
  221. // Extract the text content from the response
  222. const textContent = response.content.find(block => block.type === 'text')
  223. if (!textContent || textContent.type !== 'text') {
  224. logForDebugging('No text content in agentic search response')
  225. return []
  226. }
  227. // Debug: log the response
  228. logForDebugging(`Agentic search response: ${textContent.text}`)
  229. // Parse the JSON response
  230. const jsonMatch = textContent.text.match(/\{[\s\S]*\}/)
  231. if (!jsonMatch) {
  232. logForDebugging('Could not find JSON in agentic search response')
  233. return []
  234. }
  235. const result: AgenticSearchResult = jsonParse(jsonMatch[0])
  236. const relevantIndices = result.relevant_indices || []
  237. // Map indices back to logs (indices are relative to logsWithTranscripts)
  238. const relevantLogs = relevantIndices
  239. .filter(index => index >= 0 && index < logsWithTranscripts.length)
  240. .map(index => logsWithTranscripts[index]!)
  241. logForDebugging(
  242. `Agentic search found ${relevantLogs.length} relevant sessions`,
  243. )
  244. return relevantLogs
  245. } catch (error) {
  246. logError(error as Error)
  247. logForDebugging(`Agentic search error: ${error}`)
  248. return []
  249. }
  250. }