@equinor/fusion-framework-cli-plugin-ai-index 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/LICENSE +21 -0
  3. package/README.md +114 -0
  4. package/dist/esm/bin/apply-metadata.js +63 -0
  5. package/dist/esm/bin/apply-metadata.js.map +1 -0
  6. package/dist/esm/bin/delete-removed-files.js +36 -0
  7. package/dist/esm/bin/delete-removed-files.js.map +1 -0
  8. package/dist/esm/bin/embed.js +196 -0
  9. package/dist/esm/bin/embed.js.map +1 -0
  10. package/dist/esm/bin/execute-pipeline.js +40 -0
  11. package/dist/esm/bin/execute-pipeline.js.map +1 -0
  12. package/dist/esm/bin/file-stream.js +22 -0
  13. package/dist/esm/bin/file-stream.js.map +1 -0
  14. package/dist/esm/bin/get-diff.js +29 -0
  15. package/dist/esm/bin/get-diff.js.map +1 -0
  16. package/dist/esm/bin/index.js +2 -0
  17. package/dist/esm/bin/index.js.map +1 -0
  18. package/dist/esm/bin/types.js +2 -0
  19. package/dist/esm/bin/types.js.map +1 -0
  20. package/dist/esm/command.js +82 -0
  21. package/dist/esm/command.js.map +1 -0
  22. package/dist/esm/command.options.js +48 -0
  23. package/dist/esm/command.options.js.map +1 -0
  24. package/dist/esm/config.js +2 -0
  25. package/dist/esm/config.js.map +1 -0
  26. package/dist/esm/index.js +13 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/utils/generate-chunk-id.js +18 -0
  29. package/dist/esm/utils/generate-chunk-id.js.map +1 -0
  30. package/dist/esm/utils/git/file-changes.js +196 -0
  31. package/dist/esm/utils/git/file-changes.js.map +1 -0
  32. package/dist/esm/utils/git/git-client.js +39 -0
  33. package/dist/esm/utils/git/git-client.js.map +1 -0
  34. package/dist/esm/utils/git/index.js +9 -0
  35. package/dist/esm/utils/git/index.js.map +1 -0
  36. package/dist/esm/utils/git/metadata.js +41 -0
  37. package/dist/esm/utils/git/metadata.js.map +1 -0
  38. package/dist/esm/utils/git/status.js +34 -0
  39. package/dist/esm/utils/git/status.js.map +1 -0
  40. package/dist/esm/utils/git/types.js +2 -0
  41. package/dist/esm/utils/git/types.js.map +1 -0
  42. package/dist/esm/utils/markdown/index.js +3 -0
  43. package/dist/esm/utils/markdown/index.js.map +1 -0
  44. package/dist/esm/utils/markdown/parser.js +72 -0
  45. package/dist/esm/utils/markdown/parser.js.map +1 -0
  46. package/dist/esm/utils/markdown/types.js +2 -0
  47. package/dist/esm/utils/markdown/types.js.map +1 -0
  48. package/dist/esm/utils/package-resolver.js +40 -0
  49. package/dist/esm/utils/package-resolver.js.map +1 -0
  50. package/dist/esm/utils/ts-doc/constants.js +13 -0
  51. package/dist/esm/utils/ts-doc/constants.js.map +1 -0
  52. package/dist/esm/utils/ts-doc/extractors.js +175 -0
  53. package/dist/esm/utils/ts-doc/extractors.js.map +1 -0
  54. package/dist/esm/utils/ts-doc/index.js +3 -0
  55. package/dist/esm/utils/ts-doc/index.js.map +1 -0
  56. package/dist/esm/utils/ts-doc/parser.js +37 -0
  57. package/dist/esm/utils/ts-doc/parser.js.map +1 -0
  58. package/dist/esm/utils/ts-doc/types.js +2 -0
  59. package/dist/esm/utils/ts-doc/types.js.map +1 -0
  60. package/dist/esm/utils/types.js +2 -0
  61. package/dist/esm/utils/types.js.map +1 -0
  62. package/dist/esm/version.js +3 -0
  63. package/dist/esm/version.js.map +1 -0
  64. package/dist/tsconfig.tsbuildinfo +1 -0
  65. package/dist/types/bin/apply-metadata.d.ts +1 -0
  66. package/dist/types/bin/delete-removed-files.d.ts +1 -0
  67. package/dist/types/bin/embed.d.ts +1 -0
  68. package/dist/types/bin/execute-pipeline.d.ts +1 -0
  69. package/dist/types/bin/file-stream.d.ts +1 -0
  70. package/dist/types/bin/get-diff.d.ts +1 -0
  71. package/dist/types/bin/index.d.ts +1 -0
  72. package/dist/types/bin/types.d.ts +1 -0
  73. package/dist/types/command.d.ts +2 -0
  74. package/dist/types/command.options.d.ts +62 -0
  75. package/dist/types/config.d.ts +33 -0
  76. package/dist/types/index.d.ts +8 -0
  77. package/dist/types/utils/generate-chunk-id.d.ts +8 -0
  78. package/dist/types/utils/git/file-changes.d.ts +21 -0
  79. package/dist/types/utils/git/git-client.d.ts +17 -0
  80. package/dist/types/utils/git/index.d.ts +5 -0
  81. package/dist/types/utils/git/metadata.d.ts +7 -0
  82. package/dist/types/utils/git/status.d.ts +12 -0
  83. package/dist/types/utils/git/types.d.ts +33 -0
  84. package/dist/types/utils/markdown/index.d.ts +2 -0
  85. package/dist/types/utils/markdown/parser.d.ts +21 -0
  86. package/dist/types/utils/markdown/types.d.ts +11 -0
  87. package/dist/types/utils/package-resolver.d.ts +14 -0
  88. package/dist/types/utils/ts-doc/constants.d.ts +5 -0
  89. package/dist/types/utils/ts-doc/extractors.d.ts +28 -0
  90. package/dist/types/utils/ts-doc/index.d.ts +2 -0
  91. package/dist/types/utils/ts-doc/parser.d.ts +23 -0
  92. package/dist/types/utils/ts-doc/types.d.ts +20 -0
  93. package/dist/types/utils/types.d.ts +17 -0
  94. package/dist/types/version.d.ts +1 -0
  95. package/package.json +72 -0
  96. package/src/bin/apply-metadata.ts +77 -0
  97. package/src/bin/delete-removed-files.ts +49 -0
  98. package/src/bin/embed.ts +262 -0
  99. package/src/bin/execute-pipeline.ts +48 -0
  100. package/src/bin/file-stream.ts +34 -0
  101. package/src/bin/get-diff.ts +33 -0
  102. package/src/bin/index.ts +1 -0
  103. package/src/bin/types.ts +48 -0
  104. package/src/command.options.ts +58 -0
  105. package/src/command.ts +100 -0
  106. package/src/config.ts +39 -0
  107. package/src/index.ts +19 -0
  108. package/src/utils/generate-chunk-id.ts +17 -0
  109. package/src/utils/git/file-changes.ts +213 -0
  110. package/src/utils/git/git-client.ts +43 -0
  111. package/src/utils/git/index.ts +19 -0
  112. package/src/utils/git/metadata.ts +47 -0
  113. package/src/utils/git/status.ts +48 -0
  114. package/src/utils/git/types.ts +36 -0
  115. package/src/utils/markdown/index.ts +5 -0
  116. package/src/utils/markdown/parser.ts +92 -0
  117. package/src/utils/markdown/types.ts +20 -0
  118. package/src/utils/package-resolver.ts +44 -0
  119. package/src/utils/ts-doc/constants.ts +13 -0
  120. package/src/utils/ts-doc/extractors.ts +246 -0
  121. package/src/utils/ts-doc/index.ts +5 -0
  122. package/src/utils/ts-doc/parser.ts +51 -0
  123. package/src/utils/ts-doc/types.ts +26 -0
  124. package/src/utils/types.ts +18 -0
  125. package/src/version.ts +2 -0
  126. package/tsconfig.json +27 -0
  127. package/vitest.config.ts +14 -0
@@ -0,0 +1,246 @@
1
+ import {
2
+ type SourceFile as ProjectSourceFile,
3
+ SyntaxKind,
4
+ Node,
5
+ type VariableStatement,
6
+ type ClassDeclaration,
7
+ type MethodDeclaration,
8
+ type PropertyDeclaration,
9
+ } from 'ts-morph';
10
+
11
+ import { generateChunkId } from '../generate-chunk-id.js';
12
+ import type { TypescriptDocument, ParseTsDocOptions } from './types.js';
13
+ import { nodeKinds } from './constants.js';
14
+
15
+ /**
16
+ * Creates a TypescriptDocument from the given parameters.
17
+ * @param sourceFile - The source file containing the node.
18
+ * @param pageContent - The content of the document.
19
+ * @param tsKind - The TypeScript node kind name.
20
+ * @param tsName - The name of the TypeScript node.
21
+ * @param options - Optional parsing configuration.
22
+ * @param sourceOverride - Optional source path override (for cases where source needs transformation).
23
+ * @returns A TypeScript document with metadata.
24
+ */
25
+ const createTypescriptDocument = (
26
+ sourceFile: ProjectSourceFile,
27
+ pageContent: string,
28
+ tsKind: string,
29
+ tsName: string,
30
+ options?: ParseTsDocOptions,
31
+ ): TypescriptDocument => {
32
+ const source = sourceFile.getFilePath().replace(/^\/+/, '');
33
+ return {
34
+ id: generateChunkId(source),
35
+ pageContent,
36
+ metadata: {
37
+ source,
38
+ ...(options?.projectRoot && { rootPath: options.projectRoot }),
39
+ attributes: {
40
+ type: 'tsdoc',
41
+ ts_kind: tsKind,
42
+ ts_name: tsName,
43
+ },
44
+ },
45
+ };
46
+ };
47
+
48
+ /**
49
+ * Extracts a TSDoc document from a class node, including TSDoc, constructor, and public member signatures.
50
+ * @param classNode - The ClassDeclaration node to process.
51
+ * @param sourceFile - The source file containing the node.
52
+ * @param options - Optional parsing configuration.
53
+ * @returns A TypeScript document with TSDoc and class interface, or null if no TSDoc is found.
54
+ */
55
+ export const extractDocumentFromClassNode = (
56
+ classNode: ClassDeclaration,
57
+ sourceFile: ProjectSourceFile,
58
+ _options?: ParseTsDocOptions,
59
+ ): TypescriptDocument | null => {
60
+ // Find TSDoc comment (/** ... */)
61
+ const docCommentRange = classNode
62
+ .getLeadingCommentRanges()
63
+ .find((range) => range.getText().startsWith('/**'));
64
+
65
+ if (!docCommentRange) {
66
+ return null;
67
+ }
68
+
69
+ const docComment = docCommentRange.getText();
70
+ const className = classNode.getName() || 'AnonymousClass';
71
+
72
+ // Start with class TSDoc and declaration (without body)
73
+ const classSignature = `${classNode.getText({ includeJsDocComments: false }).split('{')[0].trim()} {`;
74
+ let pageContent = `${docComment}\n${classSignature}`;
75
+
76
+ // Add constructor with TSDoc if present
77
+ const constructorNode = classNode.getConstructors()[0]; // Get the first constructor (if any)
78
+ if (constructorNode) {
79
+ const constructorDocCommentRange = constructorNode
80
+ .getLeadingCommentRanges()
81
+ .find((range) => range.getText().startsWith('/**'));
82
+ if (constructorDocCommentRange) {
83
+ const constructorDocComment = constructorDocCommentRange.getText();
84
+ const constructorSignature = constructorNode
85
+ .getText({ includeJsDocComments: false })
86
+ .split('{')[0]
87
+ .trim();
88
+ pageContent += `\n ${constructorDocComment}\n ${constructorSignature}`;
89
+ }
90
+ }
91
+
92
+ // Add public methods and properties with TSDoc
93
+ const publicMembers = [
94
+ ...classNode.getMethods().filter((m) => m.hasModifier(SyntaxKind.PublicKeyword)),
95
+ ...classNode.getProperties().filter((p) => p.hasModifier(SyntaxKind.PublicKeyword)),
96
+ ];
97
+
98
+ publicMembers.forEach((member) => {
99
+ const memberDocCommentRange = member
100
+ .getLeadingCommentRanges()
101
+ .find((range) => range.getText().startsWith('/**'));
102
+ if (memberDocCommentRange) {
103
+ const memberDocComment = memberDocCommentRange.getText();
104
+ let memberSignature = '';
105
+ if (member.getKind() === SyntaxKind.MethodDeclaration) {
106
+ memberSignature = (member as MethodDeclaration)
107
+ .getText({ includeJsDocComments: false })
108
+ .split('{')[0]
109
+ .trim();
110
+ } else if (member.getKind() === SyntaxKind.PropertyDeclaration) {
111
+ memberSignature = (member as PropertyDeclaration)
112
+ .getText({ includeJsDocComments: false })
113
+ .trim();
114
+ }
115
+ pageContent += `\n ${memberDocComment}\n ${memberSignature}`;
116
+ }
117
+ });
118
+
119
+ pageContent += '\n}';
120
+
121
+ return createTypescriptDocument(
122
+ sourceFile,
123
+ pageContent,
124
+ classNode.getKindName(),
125
+ className,
126
+ _options,
127
+ );
128
+ };
129
+
130
+ /**
131
+ * Extracts a TSDoc document from a single node.
132
+ * @param node - The TypeScript node to process.
133
+ * @param sourceFile - The source file containing the node.
134
+ * @param options - Optional parsing configuration.
135
+ * @param nodeOptions - Optional node-specific configuration (e.g., skipKindCheck for VariableStatement).
136
+ * @returns A TypeScript document with TSDoc metadata, or null if no TSDoc is found.
137
+ */
138
+ export const extractDocumentFromNode = (
139
+ node: Node,
140
+ sourceFile: ProjectSourceFile,
141
+ options?: ParseTsDocOptions,
142
+ nodeOptions?: { skipKindCheck?: boolean },
143
+ ): TypescriptDocument | null => {
144
+ const kind = node.getKind();
145
+
146
+ // Skip if not in our target kinds unless skipKindCheck is true
147
+ if (!nodeOptions?.skipKindCheck && !nodeKinds.includes(kind)) {
148
+ return null;
149
+ }
150
+
151
+ // Handle ClassDeclaration separately
152
+ if (kind === SyntaxKind.ClassDeclaration) {
153
+ return extractDocumentFromClassNode(node as ClassDeclaration, sourceFile, options);
154
+ }
155
+
156
+ // Handle VariableStatement (e.g., `export const bundleApp = ...`)
157
+ if (kind === SyntaxKind.VariableStatement) {
158
+ const declaration = (node as VariableStatement)
159
+ .getDeclarations()
160
+ .find((d) => d.getKind() === SyntaxKind.VariableDeclaration);
161
+
162
+ if (!declaration) {
163
+ return null;
164
+ }
165
+
166
+ const initializer = declaration.getInitializer();
167
+ if (
168
+ !initializer ||
169
+ !(
170
+ initializer.getKind() === SyntaxKind.ArrowFunction ||
171
+ initializer.getKind() === SyntaxKind.FunctionExpression
172
+ )
173
+ ) {
174
+ return null;
175
+ }
176
+
177
+ // Get the TSDoc comment from the VariableStatement
178
+ const docCommentRange = node
179
+ .getLeadingCommentRanges()
180
+ .find((range) => range.getText().startsWith('/**'));
181
+
182
+ if (!docCommentRange) {
183
+ return null;
184
+ }
185
+
186
+ const nodeName = declaration.getName() || 'missing_name_of_node';
187
+ const docComment = docCommentRange.getText();
188
+
189
+ // Only include the TSDoc comment for functions/lambdas
190
+ return createTypescriptDocument(
191
+ sourceFile,
192
+ docComment,
193
+ initializer.getKindName(),
194
+ nodeName,
195
+ options,
196
+ );
197
+ }
198
+
199
+ // Get name if node has one
200
+ const nodeName = Node.hasName(node) ? node.getName() : 'missing_name_of_node';
201
+
202
+ // Find TSDoc comment (/** ... */)
203
+ const docCommentRange = node
204
+ .getLeadingCommentRanges()
205
+ .find((range) => range.getText().startsWith('/**'));
206
+
207
+ if (!docCommentRange) {
208
+ return null;
209
+ }
210
+
211
+ const docComment = docCommentRange.getText();
212
+ let pageContent = docComment;
213
+
214
+ // For interfaces and type aliases, include the full code
215
+ if (kind === SyntaxKind.InterfaceDeclaration || kind === SyntaxKind.TypeAliasDeclaration) {
216
+ pageContent = `${docComment}\n${node.getText()}`;
217
+ }
218
+ // For other nodes (e.g., FunctionDeclaration), include only TSDoc
219
+ else if (kind !== SyntaxKind.FunctionDeclaration) {
220
+ return null; // Skip unsupported kinds
221
+ }
222
+
223
+ return createTypescriptDocument(sourceFile, pageContent, node.getKindName(), nodeName, options);
224
+ };
225
+
226
+ /**
227
+ * Processes a TypeScript source file to extract TSDoc documents.
228
+ * @param sourceFile - The source file to process.
229
+ * @param options - Optional parsing configuration.
230
+ * @returns An array of TypeScript documents with TSDoc metadata.
231
+ */
232
+ export const processSourceFile = (
233
+ sourceFile: ProjectSourceFile,
234
+ options?: ParseTsDocOptions,
235
+ ): TypescriptDocument[] => {
236
+ const documents: TypescriptDocument[] = [];
237
+
238
+ sourceFile.forEachDescendant((node) => {
239
+ const document = extractDocumentFromNode(node, sourceFile, options);
240
+ if (document) {
241
+ documents.push(document);
242
+ }
243
+ });
244
+
245
+ return documents;
246
+ };
@@ -0,0 +1,5 @@
1
+ // Re-export all types
2
+ export type { TypescriptMetadata, TypescriptDocument, ParseTsDocOptions } from './types.js';
3
+
4
+ // Re-export parser functions
5
+ export { isTypescriptFile, parseTsDocSync, parseTsDocFromFileSync } from './parser.js';
@@ -0,0 +1,51 @@
1
+ import { assert } from 'node:console';
2
+ import { readFileSync } from 'node:fs';
3
+ import { Project } from 'ts-morph';
4
+
5
+ import type { SourceFile } from '../types.js';
6
+ import type { TypescriptDocument, ParseTsDocOptions } from './types.js';
7
+ import { processSourceFile } from './extractors.js';
8
+
9
+ /**
10
+ * Checks if a file is a TypeScript or TSX file based on its extension.
11
+ * @param filePath - The path to the file.
12
+ * @returns True if the file ends with .ts or .tsx, false otherwise.
13
+ */
14
+ export const isTypescriptFile = (filePath: string): boolean => {
15
+ return filePath.endsWith('.ts') || filePath.endsWith('.tsx');
16
+ };
17
+
18
+ /**
19
+ * Parses TSDoc from a string of TypeScript code.
20
+ * @param content - The TypeScript code content.
21
+ * @param options - Optional parsing configuration.
22
+ * @returns An array of TypeScript documents with TSDoc metadata.
23
+ */
24
+ export const parseTsDocSync = (
25
+ content: string,
26
+ options?: ParseTsDocOptions,
27
+ ): TypescriptDocument[] => {
28
+ const project = new Project({ useInMemoryFileSystem: true });
29
+ const sourceFile = project.createSourceFile('temp.ts', content);
30
+ return processSourceFile(sourceFile, options);
31
+ };
32
+
33
+ /**
34
+ * Parses TSDoc from a TypeScript file by path.
35
+ * @param file - The source file object.
36
+ * @param options - Optional parsing configuration.
37
+ * @returns An array of TypeScript documents with TSDoc metadata.
38
+ * @throws If the file is not a TypeScript file.
39
+ */
40
+ export const parseTsDocFromFileSync = (
41
+ file: SourceFile,
42
+ options?: ParseTsDocOptions,
43
+ ): TypescriptDocument[] => {
44
+ assert(isTypescriptFile(file.path), `File ${file.path} is not a TypeScript file`);
45
+ const project = new Project({ useInMemoryFileSystem: true });
46
+ const sourceFile = project.createSourceFile(
47
+ file.relativePath ?? file.path,
48
+ readFileSync(file.path, 'utf8'),
49
+ );
50
+ return processSourceFile(sourceFile, options);
51
+ };
@@ -0,0 +1,26 @@
1
+ import type {
2
+ VectorStoreDocument,
3
+ VectorStoreDocumentMetadata,
4
+ } from '@equinor/fusion-framework-module-ai/lib';
5
+
6
+ /**
7
+ * TypeScript document metadata
8
+ */
9
+ export type TypescriptMetadata = VectorStoreDocumentMetadata<{
10
+ type: 'tsdoc';
11
+ ts_kind: string;
12
+ ts_name: string;
13
+ }>;
14
+
15
+ /**
16
+ * TypeScript document with TSDoc metadata
17
+ */
18
+ export type TypescriptDocument = VectorStoreDocument<TypescriptMetadata>;
19
+
20
+ /**
21
+ * Options for parsing TypeScript documents
22
+ */
23
+ export interface ParseTsDocOptions {
24
+ /** The project root path for generating relative paths */
25
+ projectRoot?: string;
26
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * File change status in git
3
+ */
4
+ export type FileStatus = 'new' | 'modified' | 'removed';
5
+
6
+ /**
7
+ * Source file information for processing
8
+ */
9
+ export type SourceFile = {
10
+ /** Absolute file path */
11
+ path: string;
12
+ /** Project root directory (git repository root) */
13
+ projectRoot?: string;
14
+ /** Relative path from project root */
15
+ relativePath?: string;
16
+ /** Git change status */
17
+ status: FileStatus;
18
+ };
package/src/version.ts ADDED
@@ -0,0 +1,2 @@
1
+ // Generated by genversion.
2
+ export const version = '1.0.0';
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "outDir": "dist/esm",
7
+ "rootDir": "src",
8
+ "declarationDir": "./dist/types",
9
+ "baseUrl": "."
10
+ },
11
+ "references": [
12
+ {
13
+ "path": "../ai-base"
14
+ },
15
+ {
16
+ "path": "../../modules/ai"
17
+ },
18
+ {
19
+ "path": "../../modules/module"
20
+ },
21
+ {
22
+ "path": "../../utils/imports"
23
+ }
24
+ ],
25
+ "include": ["src/**/*"],
26
+ "exclude": ["node_modules"]
27
+ }
@@ -0,0 +1,14 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ include: ['src/**/*.test.ts'],
8
+ coverage: {
9
+ provider: 'v8',
10
+ reporter: ['text', 'json', 'html'],
11
+ exclude: ['**/*.test.ts', '**/dist/**', '**/node_modules/**'],
12
+ },
13
+ },
14
+ });