types.ts 15 KB

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