types.ts 14 KB

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