teamMemoryOps.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { isTeamMemFile } from '../memdir/teamMemPaths.js'
  2. import { FILE_EDIT_TOOL_NAME } from '../tools/FileEditTool/constants.js'
  3. import { FILE_WRITE_TOOL_NAME } from '../tools/FileWriteTool/prompt.js'
  4. export { isTeamMemFile }
  5. /**
  6. * Check if a search tool use targets team memory files by examining its path.
  7. */
  8. export function isTeamMemorySearch(toolInput: unknown): boolean {
  9. const input = toolInput as
  10. | { path?: string; pattern?: string; glob?: string }
  11. | undefined
  12. if (!input) {
  13. return false
  14. }
  15. if (input.path && isTeamMemFile(input.path)) {
  16. return true
  17. }
  18. return false
  19. }
  20. /**
  21. * Check if a Write or Edit tool use targets a team memory file.
  22. */
  23. export function isTeamMemoryWriteOrEdit(
  24. toolName: string,
  25. toolInput: unknown,
  26. ): boolean {
  27. if (toolName !== FILE_WRITE_TOOL_NAME && toolName !== FILE_EDIT_TOOL_NAME) {
  28. return false
  29. }
  30. const input = toolInput as { file_path?: string; path?: string } | undefined
  31. const filePath = input?.file_path ?? input?.path
  32. return filePath !== undefined && isTeamMemFile(filePath)
  33. }
  34. /**
  35. * Append team memory summary parts to the parts array.
  36. * Encapsulates all team memory verb/string logic for getSearchReadSummaryText.
  37. */
  38. export function appendTeamMemorySummaryParts(
  39. memoryCounts: {
  40. teamMemoryReadCount?: number
  41. teamMemorySearchCount?: number
  42. teamMemoryWriteCount?: number
  43. },
  44. isActive: boolean,
  45. parts: string[],
  46. ): void {
  47. const teamReadCount = memoryCounts.teamMemoryReadCount ?? 0
  48. const teamSearchCount = memoryCounts.teamMemorySearchCount ?? 0
  49. const teamWriteCount = memoryCounts.teamMemoryWriteCount ?? 0
  50. if (teamReadCount > 0) {
  51. const verb = isActive
  52. ? parts.length === 0
  53. ? 'Recalling'
  54. : 'recalling'
  55. : parts.length === 0
  56. ? 'Recalled'
  57. : 'recalled'
  58. parts.push(
  59. `${verb} ${teamReadCount} team ${teamReadCount === 1 ? 'memory' : 'memories'}`,
  60. )
  61. }
  62. if (teamSearchCount > 0) {
  63. const verb = isActive
  64. ? parts.length === 0
  65. ? 'Searching'
  66. : 'searching'
  67. : parts.length === 0
  68. ? 'Searched'
  69. : 'searched'
  70. parts.push(`${verb} team memories`)
  71. }
  72. if (teamWriteCount > 0) {
  73. const verb = isActive
  74. ? parts.length === 0
  75. ? 'Writing'
  76. : 'writing'
  77. : parts.length === 0
  78. ? 'Wrote'
  79. : 'wrote'
  80. parts.push(
  81. `${verb} ${teamWriteCount} team ${teamWriteCount === 1 ? 'memory' : 'memories'}`,
  82. )
  83. }
  84. }