formatter.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * formatter.ts - Output formatting utilities for QMD
  3. *
  4. * Provides methods to format search results and documents into various output formats:
  5. * JSON, CSV, XML, Markdown, files list, and CLI (colored terminal output).
  6. */
  7. import type { SearchResult, MultiGetResult, DocumentResult } from "../store.js";
  8. export type { SearchResult, MultiGetResult, DocumentResult };
  9. export type MultiGetFile = {
  10. filepath: string;
  11. displayPath: string;
  12. title: string;
  13. body: string;
  14. context?: string | null;
  15. skipped: false;
  16. } | {
  17. filepath: string;
  18. displayPath: string;
  19. title: string;
  20. body: string;
  21. context?: string | null;
  22. skipped: true;
  23. skipReason: string;
  24. };
  25. export type OutputFormat = "cli" | "csv" | "md" | "xml" | "files" | "json";
  26. export type FormatOptions = {
  27. full?: boolean;
  28. query?: string;
  29. useColor?: boolean;
  30. lineNumbers?: boolean;
  31. intent?: string;
  32. };
  33. /**
  34. * Add line numbers to text content.
  35. * Each line becomes: "{lineNum}: {content}"
  36. * @param text The text to add line numbers to
  37. * @param startLine Optional starting line number (default: 1)
  38. */
  39. export declare function addLineNumbers(text: string, startLine?: number): string;
  40. /**
  41. * Extract short docid from a full hash (first 6 characters).
  42. */
  43. export declare function getDocid(hash: string): string;
  44. export declare function escapeCSV(value: string | null | number): string;
  45. export declare function escapeXml(str: string): string;
  46. /**
  47. * Format search results as JSON
  48. */
  49. export declare function searchResultsToJson(results: SearchResult[], opts?: FormatOptions): string;
  50. /**
  51. * Format search results as CSV
  52. */
  53. export declare function searchResultsToCsv(results: SearchResult[], opts?: FormatOptions): string;
  54. /**
  55. * Format search results as simple files list (docid,score,filepath,context)
  56. */
  57. export declare function searchResultsToFiles(results: SearchResult[]): string;
  58. /**
  59. * Format search results as Markdown
  60. */
  61. export declare function searchResultsToMarkdown(results: SearchResult[], opts?: FormatOptions): string;
  62. /**
  63. * Format search results as XML
  64. */
  65. export declare function searchResultsToXml(results: SearchResult[], opts?: FormatOptions): string;
  66. /**
  67. * Format search results for MCP (simpler CSV format with pre-extracted snippets)
  68. */
  69. export declare function searchResultsToMcpCsv(results: {
  70. docid: string;
  71. file: string;
  72. title: string;
  73. score: number;
  74. context: string | null;
  75. snippet: string;
  76. }[]): string;
  77. /**
  78. * Format documents as JSON
  79. */
  80. export declare function documentsToJson(results: MultiGetFile[]): string;
  81. /**
  82. * Format documents as CSV
  83. */
  84. export declare function documentsToCsv(results: MultiGetFile[]): string;
  85. /**
  86. * Format documents as files list
  87. */
  88. export declare function documentsToFiles(results: MultiGetFile[]): string;
  89. /**
  90. * Format documents as Markdown
  91. */
  92. export declare function documentsToMarkdown(results: MultiGetFile[]): string;
  93. /**
  94. * Format documents as XML
  95. */
  96. export declare function documentsToXml(results: MultiGetFile[]): string;
  97. /**
  98. * Format a single DocumentResult as JSON
  99. */
  100. export declare function documentToJson(doc: DocumentResult): string;
  101. /**
  102. * Format a single DocumentResult as Markdown
  103. */
  104. export declare function documentToMarkdown(doc: DocumentResult): string;
  105. /**
  106. * Format a single DocumentResult as XML
  107. */
  108. export declare function documentToXml(doc: DocumentResult): string;
  109. /**
  110. * Format a single document to the specified format
  111. */
  112. export declare function formatDocument(doc: DocumentResult, format: OutputFormat): string;
  113. /**
  114. * Format search results to the specified output format
  115. */
  116. export declare function formatSearchResults(results: SearchResult[], format: OutputFormat, opts?: FormatOptions): string;
  117. /**
  118. * Format documents to the specified output format
  119. */
  120. export declare function formatDocuments(results: MultiGetFile[], format: OutputFormat): string;