types.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /**
  2. * CodeGraph Type Definitions
  3. *
  4. * Core types for the semantic knowledge graph system.
  5. */
  6. // =============================================================================
  7. // Union Types
  8. // =============================================================================
  9. /**
  10. * Types of nodes in the knowledge graph.
  11. *
  12. * Defined as a runtime-iterable `as const` array so the same source
  13. * of truth backs both the TS type and any runtime validation
  14. * (e.g. the search query parser).
  15. */
  16. export const NODE_KINDS = [
  17. 'file',
  18. 'module',
  19. 'class',
  20. 'struct',
  21. 'interface',
  22. 'trait',
  23. 'protocol',
  24. 'function',
  25. 'method',
  26. 'property',
  27. 'field',
  28. 'variable',
  29. 'constant',
  30. 'enum',
  31. 'enum_member',
  32. 'type_alias',
  33. 'namespace',
  34. 'parameter',
  35. 'import',
  36. 'export',
  37. 'route',
  38. 'component',
  39. ] as const;
  40. export type NodeKind = (typeof NODE_KINDS)[number];
  41. /**
  42. * Types of edges (relationships) between nodes
  43. */
  44. export type EdgeKind =
  45. | 'contains' // Parent contains child (file→class, class→method)
  46. | 'calls' // Function/method calls another
  47. | 'imports' // File imports from another
  48. | 'exports' // File exports a symbol
  49. | 'extends' // Class/interface extends another
  50. | 'implements' // Class implements interface
  51. | 'references' // Generic reference to another symbol
  52. | 'type_of' // Variable/parameter has type
  53. | 'returns' // Function returns type
  54. | 'instantiates' // Creates instance of class
  55. | 'overrides' // Method overrides parent method
  56. | 'decorates'; // Decorator applied to symbol
  57. /**
  58. * Supported programming languages. See NODE_KINDS for why this is a
  59. * runtime-iterable const array.
  60. */
  61. export const LANGUAGES = [
  62. 'typescript',
  63. 'javascript',
  64. 'tsx',
  65. 'jsx',
  66. 'python',
  67. 'go',
  68. 'rust',
  69. 'java',
  70. 'c',
  71. 'cpp',
  72. 'csharp',
  73. 'razor',
  74. 'php',
  75. 'ruby',
  76. 'swift',
  77. 'kotlin',
  78. 'dart',
  79. 'svelte',
  80. 'vue',
  81. 'astro',
  82. 'liquid',
  83. 'pascal',
  84. 'scala',
  85. 'lua',
  86. 'luau',
  87. 'objc',
  88. 'yaml',
  89. 'twig',
  90. 'xml',
  91. 'properties',
  92. 'unknown',
  93. ] as const;
  94. export type Language = (typeof LANGUAGES)[number];
  95. // =============================================================================
  96. // Core Graph Types
  97. // =============================================================================
  98. /**
  99. * A node in the knowledge graph representing a code symbol
  100. */
  101. export interface Node {
  102. /** Unique identifier (hash of file path + qualified name) */
  103. id: string;
  104. /** Type of code element */
  105. kind: NodeKind;
  106. /** Simple name (e.g., "calculateTotal") */
  107. name: string;
  108. /** Fully qualified name (e.g., "src/utils.ts::MathHelper.calculateTotal") */
  109. qualifiedName: string;
  110. /** File path relative to project root */
  111. filePath: string;
  112. /** Programming language */
  113. language: Language;
  114. /** Starting line number (1-indexed) */
  115. startLine: number;
  116. /** Ending line number (1-indexed) */
  117. endLine: number;
  118. /** Starting column (0-indexed) */
  119. startColumn: number;
  120. /** Ending column (0-indexed) */
  121. endColumn: number;
  122. /** Documentation string if present */
  123. docstring?: string;
  124. /** Function/method signature */
  125. signature?: string;
  126. /** Visibility modifier */
  127. visibility?: 'public' | 'private' | 'protected' | 'internal';
  128. /** Whether symbol is exported */
  129. isExported?: boolean;
  130. /** Whether symbol is async */
  131. isAsync?: boolean;
  132. /** Whether symbol is static */
  133. isStatic?: boolean;
  134. /** Whether symbol is abstract */
  135. isAbstract?: boolean;
  136. /** Decorators/annotations applied */
  137. decorators?: string[];
  138. /** Generic type parameters */
  139. typeParameters?: string[];
  140. /**
  141. * Normalized return/result type name for a function/method (the bare class
  142. * name, smart-pointer pointee unwrapped). Captured for C/C++ so resolution
  143. * can infer a chained receiver's type from what the inner call returns —
  144. * `Foo::instance().bar()` resolves `bar` on `Foo` (issue #645). Undefined for
  145. * languages/symbols where it isn't captured.
  146. */
  147. returnType?: string;
  148. /** When the node was last updated */
  149. updatedAt: number;
  150. }
  151. /**
  152. * An edge representing a relationship between two nodes
  153. */
  154. export interface Edge {
  155. /** Source node ID */
  156. source: string;
  157. /** Target node ID */
  158. target: string;
  159. /** Type of relationship */
  160. kind: EdgeKind;
  161. /** Additional context about the relationship */
  162. metadata?: Record<string, unknown>;
  163. /** Line number where relationship occurs (e.g., call site) */
  164. line?: number;
  165. /** Column number where relationship occurs */
  166. column?: number;
  167. /** How this edge was created */
  168. provenance?: 'tree-sitter' | 'scip' | 'heuristic';
  169. }
  170. /**
  171. * Metadata about a tracked file
  172. */
  173. export interface FileRecord {
  174. /** File path relative to project root */
  175. path: string;
  176. /** Content hash for change detection */
  177. contentHash: string;
  178. /** Detected language */
  179. language: Language;
  180. /** File size in bytes */
  181. size: number;
  182. /** Last modification timestamp */
  183. modifiedAt: number;
  184. /** When last indexed */
  185. indexedAt: number;
  186. /** Number of nodes extracted */
  187. nodeCount: number;
  188. /** Any extraction errors */
  189. errors?: ExtractionError[];
  190. }
  191. // =============================================================================
  192. // Extraction Types
  193. // =============================================================================
  194. /**
  195. * Result from parsing a source file
  196. */
  197. export interface ExtractionResult {
  198. /** Extracted nodes */
  199. nodes: Node[];
  200. /** Extracted edges */
  201. edges: Edge[];
  202. /** References that couldn't be resolved yet */
  203. unresolvedReferences: UnresolvedReference[];
  204. /** Any errors during extraction */
  205. errors: ExtractionError[];
  206. /** Extraction duration in milliseconds */
  207. durationMs: number;
  208. }
  209. /**
  210. * Error during code extraction
  211. */
  212. export interface ExtractionError {
  213. /** Error message */
  214. message: string;
  215. /** File path where the error occurred */
  216. filePath?: string;
  217. /** Line number if available */
  218. line?: number;
  219. /** Column number if available */
  220. column?: number;
  221. /** Error severity */
  222. severity: 'error' | 'warning';
  223. /** Error code for categorization */
  224. code?: string;
  225. }
  226. /**
  227. * Kinds an unresolved reference can carry. `function_ref` is internal-only —
  228. * a function name used as a VALUE (callback registration, #756). It never
  229. * becomes an edge kind: resolution maps it to a `references` edge targeting
  230. * function/method nodes only (see `matchFunctionRef`).
  231. */
  232. export type ReferenceKind = EdgeKind | 'function_ref';
  233. /**
  234. * A reference that couldn't be resolved during extraction
  235. */
  236. export interface UnresolvedReference {
  237. /** ID of the node containing the reference */
  238. fromNodeId: string;
  239. /** Name being referenced */
  240. referenceName: string;
  241. /** Type of reference (call, type, import, etc.) */
  242. referenceKind: ReferenceKind;
  243. /** Location of the reference */
  244. line: number;
  245. column: number;
  246. /** File path where reference occurs (denormalized for performance) */
  247. filePath?: string;
  248. /** Language of the source file (denormalized for performance) */
  249. language?: Language;
  250. /** Possible qualified names it might resolve to */
  251. candidates?: string[];
  252. }
  253. // =============================================================================
  254. // Query Types
  255. // =============================================================================
  256. /**
  257. * A subgraph containing a subset of the knowledge graph
  258. */
  259. export interface Subgraph {
  260. /** Nodes in this subgraph */
  261. nodes: Map<string, Node>;
  262. /** Edges in this subgraph */
  263. edges: Edge[];
  264. /** Root node IDs (entry points) */
  265. roots: string[];
  266. /**
  267. * Retrieval confidence for context-style queries. `'low'` means the query
  268. * resolved only to isolated common-word matches (no entry point corroborated
  269. * by 2+ distinct query terms) — callers should surface an honest handoff to
  270. * explore/trace rather than present the results as comprehensive. Undefined
  271. * for graph traversals that don't run the search-ranking path.
  272. */
  273. confidence?: 'high' | 'low';
  274. }
  275. /**
  276. * Options for graph traversal
  277. */
  278. export interface TraversalOptions {
  279. /** Maximum depth to traverse (default: Infinity) */
  280. maxDepth?: number;
  281. /** Edge types to follow (default: all) */
  282. edgeKinds?: EdgeKind[];
  283. /** Node types to include (default: all) */
  284. nodeKinds?: NodeKind[];
  285. /** Direction of traversal */
  286. direction?: 'outgoing' | 'incoming' | 'both';
  287. /** Maximum nodes to return */
  288. limit?: number;
  289. /** Whether to include the starting node */
  290. includeStart?: boolean;
  291. }
  292. /**
  293. * Options for searching the graph
  294. */
  295. export interface SearchOptions {
  296. /** Node types to search */
  297. kinds?: NodeKind[];
  298. /** Languages to include */
  299. languages?: Language[];
  300. /** File path patterns to include */
  301. includePatterns?: string[];
  302. /** File path patterns to exclude */
  303. excludePatterns?: string[];
  304. /** Maximum results to return */
  305. limit?: number;
  306. /** Offset for pagination */
  307. offset?: number;
  308. /** Whether search is case-sensitive */
  309. caseSensitive?: boolean;
  310. }
  311. /**
  312. * A search result with relevance scoring
  313. */
  314. export interface SearchResult {
  315. /** Matching node */
  316. node: Node;
  317. /** Relevance score (0-1) */
  318. score: number;
  319. /** Matched text snippets for highlighting */
  320. highlights?: string[];
  321. }
  322. // =============================================================================
  323. // Context Types
  324. // =============================================================================
  325. /**
  326. * Context information for code understanding
  327. */
  328. export interface Context {
  329. /** Primary node being examined */
  330. focal: Node;
  331. /** Nodes containing the focal node (file, class, etc.) */
  332. ancestors: Node[];
  333. /** Nodes directly contained by focal node */
  334. children: Node[];
  335. /** Incoming references (who calls/uses this) */
  336. incomingRefs: Array<{ node: Node; edge: Edge }>;
  337. /** Outgoing references (what this calls/uses) */
  338. outgoingRefs: Array<{ node: Node; edge: Edge }>;
  339. /** Related type information */
  340. types: Node[];
  341. /** Relevant imports */
  342. imports: Node[];
  343. }
  344. /**
  345. * A block of code with context
  346. */
  347. export interface CodeBlock {
  348. /** The code content */
  349. content: string;
  350. /** File path */
  351. filePath: string;
  352. /** Starting line */
  353. startLine: number;
  354. /** Ending line */
  355. endLine: number;
  356. /** Language for syntax highlighting */
  357. language: Language;
  358. /** Associated node if extracted */
  359. node?: Node;
  360. }
  361. // =============================================================================
  362. // Database Types
  363. // =============================================================================
  364. /**
  365. * Database schema version info
  366. */
  367. export interface SchemaVersion {
  368. /** Current schema version */
  369. version: number;
  370. /** When schema was created/updated */
  371. appliedAt: number;
  372. /** Description of this version */
  373. description?: string;
  374. }
  375. /**
  376. * Statistics about the knowledge graph
  377. */
  378. export interface GraphStats {
  379. /** Total number of nodes */
  380. nodeCount: number;
  381. /** Total number of edges */
  382. edgeCount: number;
  383. /** Number of tracked files */
  384. fileCount: number;
  385. /** Node counts by kind */
  386. nodesByKind: Record<NodeKind, number>;
  387. /** Edge counts by kind */
  388. edgesByKind: Record<EdgeKind, number>;
  389. /** File counts by language */
  390. filesByLanguage: Record<Language, number>;
  391. /** Database size in bytes */
  392. dbSizeBytes: number;
  393. /** Last update timestamp */
  394. lastUpdated: number;
  395. }
  396. // =============================================================================
  397. // Task Context Types (for buildContext)
  398. // =============================================================================
  399. /**
  400. * Input for building task context
  401. */
  402. export type TaskInput = string | { title: string; description?: string };
  403. /**
  404. * Options for building task context
  405. */
  406. export interface BuildContextOptions {
  407. /** Maximum number of nodes to include (default: 50) */
  408. maxNodes?: number;
  409. /** Maximum number of code blocks to include (default: 10) */
  410. maxCodeBlocks?: number;
  411. /** Maximum characters per code block (default: 2000) */
  412. maxCodeBlockSize?: number;
  413. /** Whether to include code blocks (default: true) */
  414. includeCode?: boolean;
  415. /** Output format (default: 'markdown') */
  416. format?: 'markdown' | 'json';
  417. /** Number of semantic search results (default: 5) */
  418. searchLimit?: number;
  419. /** Graph traversal depth from entry points (default: 2) */
  420. traversalDepth?: number;
  421. /** Minimum semantic similarity score (default: 0.3) */
  422. minScore?: number;
  423. }
  424. /**
  425. * Full context for a task, ready for Claude
  426. */
  427. export interface TaskContext {
  428. /** The original query/task */
  429. query: string;
  430. /** Subgraph of relevant nodes and edges */
  431. subgraph: Subgraph;
  432. /** Entry point nodes (from semantic search) */
  433. entryPoints: Node[];
  434. /** Code blocks extracted from key nodes */
  435. codeBlocks: CodeBlock[];
  436. /** Files involved in this context */
  437. relatedFiles: string[];
  438. /** Brief summary of the context */
  439. summary: string;
  440. /** Statistics about the context */
  441. stats: {
  442. /** Number of nodes included */
  443. nodeCount: number;
  444. /** Number of edges included */
  445. edgeCount: number;
  446. /** Number of files touched */
  447. fileCount: number;
  448. /** Number of code blocks included */
  449. codeBlockCount: number;
  450. /** Total characters in code blocks */
  451. totalCodeSize: number;
  452. };
  453. }
  454. /**
  455. * Options for finding relevant context
  456. */
  457. export interface FindRelevantContextOptions {
  458. /** Number of semantic search results (default: 5) */
  459. searchLimit?: number;
  460. /** Graph traversal depth (default: 2) */
  461. traversalDepth?: number;
  462. /** Maximum nodes in result (default: 50) */
  463. maxNodes?: number;
  464. /** Minimum semantic similarity score (default: 0.3) */
  465. minScore?: number;
  466. /** Edge types to follow in traversal */
  467. edgeKinds?: EdgeKind[];
  468. /** Node types to include */
  469. nodeKinds?: NodeKind[];
  470. }