@skmtc/core 0.0.924 → 0.0.926

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 (39) hide show
  1. package/esm/context/CoreContext.d.ts +1 -1
  2. package/esm/context/CoreContext.d.ts.map +1 -1
  3. package/esm/context/CoreContext.js +3 -3
  4. package/esm/context/RenderContext.d.ts +2 -2
  5. package/esm/context/RenderContext.d.ts.map +1 -1
  6. package/esm/context/RenderContext.js +10 -86
  7. package/esm/helpers/naming.d.ts.map +1 -1
  8. package/esm/helpers/naming.js +1 -1
  9. package/esm/mod.d.ts +0 -1
  10. package/esm/mod.d.ts.map +1 -1
  11. package/esm/mod.js +0 -1
  12. package/esm/run/toArtifacts.d.ts +2 -2
  13. package/esm/run/toArtifacts.d.ts.map +1 -1
  14. package/esm/run/toArtifacts.js +2 -2
  15. package/esm/typescript/FunctionParameter.d.ts.map +1 -1
  16. package/esm/typescript/FunctionParameter.js +1 -1
  17. package/package.json +1 -7
  18. package/script/context/CoreContext.d.ts +1 -1
  19. package/script/context/CoreContext.d.ts.map +1 -1
  20. package/script/context/CoreContext.js +3 -3
  21. package/script/context/RenderContext.d.ts +2 -2
  22. package/script/context/RenderContext.d.ts.map +1 -1
  23. package/script/context/RenderContext.js +10 -86
  24. package/script/helpers/naming.d.ts.map +1 -1
  25. package/script/helpers/naming.js +2 -2
  26. package/script/mod.d.ts +0 -1
  27. package/script/mod.d.ts.map +1 -1
  28. package/script/mod.js +0 -1
  29. package/script/run/toArtifacts.d.ts +2 -2
  30. package/script/run/toArtifacts.d.ts.map +1 -1
  31. package/script/run/toArtifacts.js +2 -2
  32. package/script/typescript/FunctionParameter.d.ts.map +1 -1
  33. package/script/typescript/FunctionParameter.js +2 -2
  34. package/esm/helpers/generationStats.d.ts +0 -312
  35. package/esm/helpers/generationStats.d.ts.map +0 -1
  36. package/esm/helpers/generationStats.js +0 -330
  37. package/script/helpers/generationStats.d.ts +0 -312
  38. package/script/helpers/generationStats.d.ts.map +0 -1
  39. package/script/helpers/generationStats.js +0 -339
@@ -1,339 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkResult = exports.toManifestErrors = exports.toTotalTime = exports.toManifestLines = exports.toManifestTokens = exports.toGenerationStats = void 0;
4
- require("../_dnt.polyfills.js");
5
- const ts_pattern_1 = require("ts-pattern");
6
- const gpt_tokenizer_1 = require("gpt-tokenizer");
7
- /**
8
- * Generates comprehensive statistics about the code generation process.
9
- *
10
- * This function analyzes the generation manifest and produced artifacts to create
11
- * detailed statistics including token counts, lines of code, timing information,
12
- * error tracking, and file counts. It's used for performance monitoring, debugging,
13
- * and reporting on generation runs.
14
- *
15
- * The function combines multiple analysis methods to provide a complete picture
16
- * of the generation process, including tokenization analysis for cost estimation,
17
- * error path extraction for debugging, and timing analysis for performance optimization.
18
- *
19
- * @param args - Generation analysis configuration
20
- * @param args.manifest - The generation manifest with metadata and results
21
- * @param args.artifacts - Map of file paths to generated content
22
- * @returns Comprehensive generation statistics object
23
- *
24
- * @example Basic usage
25
- * ```typescript
26
- * import { toGenerationStats } from '@skmtc/core';
27
- *
28
- * const stats = toGenerationStats({
29
- * manifest: {
30
- * startAt: 1672531200000,
31
- * endAt: 1672531245000,
32
- * files: {
33
- * './models.ts': { lines: 150, characters: 4500 },
34
- * './types.ts': { lines: 80, characters: 2100 }
35
- * },
36
- * results: { }
37
- * },
38
- * artifacts: {
39
- * './models.ts': 'export interface User { id: string; name: string; }',
40
- * './types.ts': 'export type Status = "active" | "inactive";'
41
- * }
42
- * });
43
- *
44
- * console.log(stats);
45
- * // {
46
- * // tokens: 250,
47
- * // lines: 230,
48
- * // totalTime: 45000,
49
- * // errors: [],
50
- * // files: 2
51
- * // }
52
- * ```
53
- *
54
- * @example Performance monitoring
55
- * ```typescript
56
- * class GenerationMonitor {
57
- * analyzeGeneration(manifest: ManifestContent, artifacts: Record<string, string>) {
58
- * const stats = toGenerationStats({ manifest, artifacts });
59
- *
60
- * console.log(`Generation completed in ${stats.totalTime}ms`);
61
- * console.log(`Generated ${stats.files} files with ${stats.lines} lines`);
62
- * console.log(`Token usage: ${stats.tokens} tokens`);
63
- *
64
- * if (stats.errors.length > 0) {
65
- * console.warn(`Found ${stats.errors.length} errors:`);
66
- * stats.errors.forEach((errorPath, index) => {
67
- * console.warn(` ${index + 1}. ${errorPath.join(' -> ')}`);
68
- * });
69
- * }
70
- *
71
- * return stats;
72
- * }
73
- * }
74
- * ```
75
- *
76
- * @example Cost estimation
77
- * ```typescript
78
- * function estimateGenerationCost(stats: GenerationStats): number {
79
- * const tokensPerDollar = 1000000; // Example rate
80
- * const estimatedCost = stats.tokens / tokensPerDollar;
81
- *
82
- * console.log(`Estimated cost: $${estimatedCost.toFixed(4)}`);
83
- * console.log(`Efficiency: ${stats.lines / stats.tokens} lines per token`);
84
- *
85
- * return estimatedCost;
86
- * }
87
- * ```
88
- *
89
- * @example Build reporting
90
- * ```typescript
91
- * function generateBuildReport(stats: GenerationStats): string {
92
- * const efficiency = stats.totalTime / stats.files;
93
- * const successRate = ((stats.files - stats.errors.length) / stats.files * 100).toFixed(1);
94
- *
95
- * return `
96
- * Build Report:
97
- * -----------
98
- * Files Generated: ${stats.files}
99
- * Total Lines: ${stats.lines.toLocaleString()}
100
- * Token Count: ${stats.tokens.toLocaleString()}
101
- * Generation Time: ${(stats.totalTime / 1000).toFixed(2)}s
102
- * Average per File: ${efficiency.toFixed(0)}ms
103
- * Success Rate: ${successRate}%
104
- * Errors: ${stats.errors.length}
105
- * `.trim();
106
- * }
107
- * ```
108
- */
109
- const toGenerationStats = ({ manifest, artifacts }) => {
110
- const tokens = (0, exports.toManifestTokens)(artifacts);
111
- const lines = (0, exports.toManifestLines)(manifest);
112
- const totalTime = (0, exports.toTotalTime)(manifest);
113
- const errors = (0, exports.toManifestErrors)(manifest.results);
114
- const files = Object.keys(artifacts).length;
115
- return {
116
- tokens,
117
- lines,
118
- totalTime,
119
- errors,
120
- files
121
- };
122
- };
123
- exports.toGenerationStats = toGenerationStats;
124
- /**
125
- * Calculates the total number of GPT tokens in all generated artifacts.
126
- *
127
- * This function uses GPT tokenization to count the total number of tokens
128
- * across all generated code artifacts. This is useful for cost estimation
129
- * when using token-based pricing models and for understanding the size
130
- * and complexity of generated content.
131
- *
132
- * @param artifacts - Map of file paths to generated content strings
133
- * @returns Total number of GPT tokens across all artifacts
134
- *
135
- * @example
136
- * ```typescript
137
- * const artifacts = {
138
- * './models.ts': 'export interface User { id: string; name: string; }',
139
- * './types.ts': 'export type Status = "active" | "inactive";'
140
- * };
141
- *
142
- * const tokens = toManifestTokens(artifacts);
143
- * console.log(tokens); // e.g., 24 tokens
144
- * ```
145
- */
146
- const toManifestTokens = (artifacts) => {
147
- const { tokens } = Object.values(artifacts).reduce((acc, artifact) => ({ tokens: acc.tokens + (0, gpt_tokenizer_1.countTokens)(artifact) }), { tokens: 0 });
148
- return tokens;
149
- };
150
- exports.toManifestTokens = toManifestTokens;
151
- /**
152
- * Calculates the total number of lines across all generated files.
153
- *
154
- * This function sums up the line counts from the manifest's file metadata
155
- * to provide the total lines of code generated. This metric is useful
156
- * for understanding the scale of code generation and tracking productivity.
157
- *
158
- * @param manifest - The generation manifest containing file metadata
159
- * @returns Total number of lines across all generated files
160
- *
161
- * @example
162
- * ```typescript
163
- * const manifest = {
164
- * files: {
165
- * './models.ts': { lines: 150, characters: 4500 },
166
- * './types.ts': { lines: 80, characters: 2100 }
167
- * },
168
- * // ... other manifest properties
169
- * };
170
- *
171
- * const lines = toManifestLines(manifest);
172
- * console.log(lines); // 230
173
- * ```
174
- */
175
- const toManifestLines = (manifest) => {
176
- const { lines } = Object.values(manifest.files).reduce((acc, file) => ({ lines: acc.lines + file.lines }), { lines: 0 });
177
- return lines;
178
- };
179
- exports.toManifestLines = toManifestLines;
180
- /**
181
- * Calculates the total generation time from the manifest timestamps.
182
- *
183
- * This function computes the duration of the generation process by
184
- * calculating the difference between the end and start timestamps
185
- * recorded in the manifest. The result is in milliseconds.
186
- *
187
- * @param manifest - The generation manifest with timing information
188
- * @returns Total generation time in milliseconds
189
- *
190
- * @example
191
- * ```typescript
192
- * const manifest = {
193
- * startAt: 1672531200000, // January 1, 2023 00:00:00 GMT
194
- * endAt: 1672531245000, // January 1, 2023 00:00:45 GMT
195
- * // ... other manifest properties
196
- * };
197
- *
198
- * const duration = toTotalTime(manifest);
199
- * console.log(duration); // 45000 (45 seconds)
200
- * console.log(`${duration / 1000}s`); // "45s"
201
- * ```
202
- */
203
- const toTotalTime = (manifest) => {
204
- const totalTime = manifest.endAt - manifest.startAt;
205
- return totalTime;
206
- };
207
- exports.toTotalTime = toTotalTime;
208
- /**
209
- * Extracts and organizes error paths from the manifest results.
210
- *
211
- * This function recursively traverses the results structure in the manifest
212
- * to identify all error conditions and their associated file paths. Errors
213
- * are returned as arrays of path segments, making it easy to understand
214
- * the location and context of each error.
215
- *
216
- * @param results - The results object from the generation manifest
217
- * @returns Array of error paths, each represented as an array of path segments
218
- *
219
- * @example
220
- * ```typescript
221
- * const results = {
222
- * 'models.ts': {
223
- * 'User': 'success',
224
- * 'Product': 'error'
225
- * },
226
- * 'types.ts': 'success'
227
- * };
228
- *
229
- * const errors = toManifestErrors(results);
230
- * console.log(errors); // [['models.ts', 'Product']]
231
- *
232
- * // Display errors in readable format
233
- * errors.forEach(errorPath => {
234
- * console.log(`Error at: ${errorPath.join(' -> ')}`);
235
- * });
236
- * // Output: "Error at: models.ts -> Product"
237
- * ```
238
- */
239
- const toManifestErrors = (results) => {
240
- const errors = [];
241
- Object.entries(results).map(([path, result]) => {
242
- return (0, exports.checkResult)({ path: [path], result, errors });
243
- });
244
- return errors;
245
- };
246
- exports.toManifestErrors = toManifestErrors;
247
- /**
248
- * Recursively checks results structure for error conditions and collects error paths.
249
- *
250
- * This function traverses the complex nested structure of generation results,
251
- * identifying error conditions at any level and recording their full path context.
252
- * It handles arrays, objects, strings, and null values, building a comprehensive
253
- * map of all errors that occurred during generation.
254
- *
255
- * The function mutates the errors array parameter, accumulating error paths
256
- * as it traverses the results structure. This approach is used for performance
257
- * when processing large result sets.
258
- *
259
- * @param args - Arguments for result checking
260
- * @param args.path - Current path segments being traversed
261
- * @param args.result - The result value to check for errors
262
- * @param args.errors - Array to accumulate error paths (mutated by function)
263
- *
264
- * @example Simple error detection
265
- * ```typescript
266
- * const errors: string[][] = [];
267
- *
268
- * checkResult({
269
- * path: ['models.ts'],
270
- * result: 'error',
271
- * errors
272
- * });
273
- *
274
- * console.log(errors); // [['models.ts']]
275
- * ```
276
- *
277
- * @example Nested structure error detection
278
- * ```typescript
279
- * const errors: string[][] = [];
280
- * const nestedResult = {
281
- * 'User': 'success',
282
- * 'Product': {
283
- * 'validation': 'error',
284
- * 'generation': 'success'
285
- * }
286
- * };
287
- *
288
- * checkResult({
289
- * path: ['models.ts'],
290
- * result: nestedResult,
291
- * errors
292
- * });
293
- *
294
- * console.log(errors); // [['models.ts', 'Product', 'validation']]
295
- * ```
296
- *
297
- * @example Array handling
298
- * ```typescript
299
- * const errors: string[][] = [];
300
- * const arrayResult = ['success', null, 'error', 'success'];
301
- *
302
- * checkResult({
303
- * path: ['batch'],
304
- * result: arrayResult,
305
- * errors
306
- * });
307
- *
308
- * console.log(errors); // [['batch']] - for the 'error' item
309
- * ```
310
- */
311
- const checkResult = ({ path, result, errors }) => {
312
- (0, ts_pattern_1.match)(result)
313
- .with(ts_pattern_1.P.array(), matchedResult => {
314
- return matchedResult.map(item => {
315
- if (item !== null) {
316
- (0, exports.checkResult)({ path, result: item, errors });
317
- }
318
- });
319
- })
320
- .with(ts_pattern_1.P.string, matchedResult => {
321
- if (matchedResult === 'error') {
322
- errors.push(path);
323
- }
324
- })
325
- .with(ts_pattern_1.P.nullish, matchedResult => {
326
- return (0, exports.checkResult)({ path, result: matchedResult, errors });
327
- })
328
- .otherwise(matched => {
329
- if (typeof matched === 'object') {
330
- return Object.entries(matched).map(([key, value]) => {
331
- return (0, exports.checkResult)({ path: [...path, key], result: value, errors });
332
- });
333
- }
334
- else {
335
- throw new Error('Invalid result type');
336
- }
337
- });
338
- };
339
- exports.checkResult = checkResult;