te.js 2.1.0 → 2.1.2

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 (70) hide show
  1. package/README.md +197 -196
  2. package/auto-docs/analysis/handler-analyzer.js +58 -58
  3. package/auto-docs/analysis/source-resolver.js +101 -101
  4. package/auto-docs/constants.js +37 -37
  5. package/auto-docs/docs-llm/index.js +7 -7
  6. package/auto-docs/docs-llm/prompts.js +222 -222
  7. package/auto-docs/docs-llm/provider.js +132 -132
  8. package/auto-docs/index.js +146 -146
  9. package/auto-docs/openapi/endpoint-processor.js +277 -277
  10. package/auto-docs/openapi/generator.js +107 -107
  11. package/auto-docs/openapi/level3.js +131 -131
  12. package/auto-docs/openapi/spec-builders.js +244 -244
  13. package/auto-docs/ui/docs-ui.js +186 -186
  14. package/auto-docs/utils/logger.js +17 -17
  15. package/auto-docs/utils/strip-usage.js +10 -10
  16. package/cli/docs-command.js +315 -315
  17. package/cli/fly-command.js +71 -71
  18. package/cli/index.js +56 -56
  19. package/cors/index.js +71 -0
  20. package/database/index.js +165 -165
  21. package/database/mongodb.js +146 -146
  22. package/database/redis.js +201 -201
  23. package/docs/README.md +36 -36
  24. package/docs/ammo.md +362 -362
  25. package/docs/api-reference.md +490 -490
  26. package/docs/auto-docs.md +216 -216
  27. package/docs/cli.md +152 -152
  28. package/docs/configuration.md +275 -275
  29. package/docs/database.md +390 -390
  30. package/docs/error-handling.md +438 -438
  31. package/docs/file-uploads.md +333 -333
  32. package/docs/getting-started.md +214 -214
  33. package/docs/middleware.md +355 -355
  34. package/docs/rate-limiting.md +393 -393
  35. package/docs/routing.md +302 -302
  36. package/lib/llm/client.js +73 -0
  37. package/lib/llm/index.js +7 -0
  38. package/lib/llm/parse.js +89 -0
  39. package/package.json +64 -62
  40. package/rate-limit/algorithms/fixed-window.js +141 -141
  41. package/rate-limit/algorithms/sliding-window.js +147 -147
  42. package/rate-limit/algorithms/token-bucket.js +115 -115
  43. package/rate-limit/base.js +165 -165
  44. package/rate-limit/index.js +147 -147
  45. package/rate-limit/storage/base.js +104 -104
  46. package/rate-limit/storage/memory.js +101 -101
  47. package/rate-limit/storage/redis.js +88 -88
  48. package/server/ammo/body-parser.js +220 -220
  49. package/server/ammo/dispatch-helper.js +103 -103
  50. package/server/ammo/enhancer.js +57 -57
  51. package/server/ammo.js +454 -415
  52. package/server/endpoint.js +97 -74
  53. package/server/error.js +9 -9
  54. package/server/errors/code-context.js +125 -125
  55. package/server/errors/llm-error-service.js +140 -140
  56. package/server/files/helper.js +33 -33
  57. package/server/files/uploader.js +143 -143
  58. package/server/handler.js +158 -119
  59. package/server/target.js +185 -175
  60. package/server/targets/middleware-validator.js +22 -22
  61. package/server/targets/path-validator.js +21 -21
  62. package/server/targets/registry.js +160 -160
  63. package/server/targets/shoot-validator.js +21 -21
  64. package/te.js +428 -402
  65. package/utils/auto-register.js +17 -17
  66. package/utils/configuration.js +64 -64
  67. package/utils/errors-llm-config.js +84 -84
  68. package/utils/request-logger.js +43 -43
  69. package/utils/status-codes.js +82 -82
  70. package/utils/tejas-entrypoint-html.js +18 -18
@@ -1,101 +1,101 @@
1
- /**
2
- * Resolves downstream dependency sources for level-2 documentation (handler + deps context).
3
- * Reads a target file, extracts relative imports, and returns their source code.
4
- */
5
-
6
- import { readFile } from 'node:fs/promises';
7
- import path from 'node:path';
8
- import { DEPENDENCY_CONTEXT_MAX_CHARS } from '../constants.js';
9
-
10
- /** Match ES module imports: import x from './...' or import { x } from '../...' or import '...' */
11
- const IMPORT_REGEX = /import\s+(?:(?:\{[^}]*\}|\w+)(?:\s*,\s*(?:\{[^}]*\}|\w+))*\s+from\s+)?['"]([^'"]+)['"]/g;
12
-
13
- /**
14
- * Extract import specifiers that are relative paths (./ or ../).
15
- * @param {string} source - File source code
16
- * @returns {string[]} Relative paths (e.g. ['../services/user.service.js'])
17
- */
18
- function extractRelativeImports(source) {
19
- const out = [];
20
- let match;
21
- IMPORT_REGEX.lastIndex = 0;
22
- while ((match = IMPORT_REGEX.exec(source)) !== null) {
23
- const spec = match[1];
24
- if (spec.startsWith('./') || spec.startsWith('../')) {
25
- out.push(spec);
26
- }
27
- }
28
- return [...new Set(out)];
29
- }
30
-
31
- /**
32
- * Resolve target file path from groupId and DIR_TARGETS.
33
- * @param {string} groupId - e.g. 'users' or 'subdir/users'
34
- * @param {string} dirTargets - e.g. 'targets'
35
- * @returns {string} Absolute path to the target file
36
- */
37
- function resolveTargetFilePath(groupId, dirTargets = 'targets') {
38
- const baseDir = path.join(process.cwd(), dirTargets);
39
- const relative = groupId.replace(/\//g, path.sep) + '.target.js';
40
- return path.join(baseDir, relative);
41
- }
42
-
43
- /**
44
- * Read a target file and all its relative-import dependencies (one level deep).
45
- * @param {string} groupId - Source group id (e.g. 'users')
46
- * @param {string} [dirTargets] - DIR_TARGETS (default from process.env or 'targets')
47
- * @returns {Promise<Map<string, string>>} Map of absolute file path -> source code
48
- */
49
- export async function resolveDependencySources(groupId, dirTargets = process.env.DIR_TARGETS || 'targets') {
50
- const targetPath = resolveTargetFilePath(groupId, dirTargets);
51
- const result = new Map();
52
-
53
- let targetSource;
54
- try {
55
- targetSource = await readFile(targetPath, 'utf8');
56
- } catch {
57
- return result;
58
- }
59
-
60
- result.set(targetPath, targetSource);
61
- const targetDir = path.dirname(targetPath);
62
- const relativeImports = extractRelativeImports(targetSource);
63
-
64
- for (const rel of relativeImports) {
65
- const resolvedPath = path.resolve(targetDir, rel);
66
- try {
67
- const code = await readFile(resolvedPath, 'utf8');
68
- result.set(resolvedPath, code);
69
- } catch {
70
- // Skip missing or unreadable files
71
- }
72
- }
73
-
74
- return result;
75
- }
76
-
77
- /**
78
- * Format resolved dependency sources as a single string for LLM context.
79
- * @param {Map<string, string>} sources - Map of file path -> source
80
- * @param {string} [targetPath] - Path to the target file (excluded from "dependencies" label)
81
- * @param {number} [maxChars] - Max total characters to include (default: DEPENDENCY_CONTEXT_MAX_CHARS)
82
- * @returns {string}
83
- */
84
- export function formatDependencyContext(sources, targetPath, maxChars = DEPENDENCY_CONTEXT_MAX_CHARS) {
85
- if (!sources || sources.size === 0) return '';
86
- const lines = [];
87
- let total = 0;
88
- for (const [filePath, code] of sources) {
89
- const label = filePath === targetPath ? 'Target' : path.relative(process.cwd(), filePath);
90
- const block = `\n// --- ${label} ---\n${code}`;
91
- if (total + block.length > maxChars) {
92
- lines.push(block.slice(0, maxChars - total) + '\n// ... (truncated)');
93
- break;
94
- }
95
- lines.push(block);
96
- total += block.length;
97
- }
98
- return lines.join('\n');
99
- }
100
-
101
- export { extractRelativeImports, resolveTargetFilePath };
1
+ /**
2
+ * Resolves downstream dependency sources for level-2 documentation (handler + deps context).
3
+ * Reads a target file, extracts relative imports, and returns their source code.
4
+ */
5
+
6
+ import { readFile } from 'node:fs/promises';
7
+ import path from 'node:path';
8
+ import { DEPENDENCY_CONTEXT_MAX_CHARS } from '../constants.js';
9
+
10
+ /** Match ES module imports: import x from './...' or import { x } from '../...' or import '...' */
11
+ const IMPORT_REGEX = /import\s+(?:(?:\{[^}]*\}|\w+)(?:\s*,\s*(?:\{[^}]*\}|\w+))*\s+from\s+)?['"]([^'"]+)['"]/g;
12
+
13
+ /**
14
+ * Extract import specifiers that are relative paths (./ or ../).
15
+ * @param {string} source - File source code
16
+ * @returns {string[]} Relative paths (e.g. ['../services/user.service.js'])
17
+ */
18
+ function extractRelativeImports(source) {
19
+ const out = [];
20
+ let match;
21
+ IMPORT_REGEX.lastIndex = 0;
22
+ while ((match = IMPORT_REGEX.exec(source)) !== null) {
23
+ const spec = match[1];
24
+ if (spec.startsWith('./') || spec.startsWith('../')) {
25
+ out.push(spec);
26
+ }
27
+ }
28
+ return [...new Set(out)];
29
+ }
30
+
31
+ /**
32
+ * Resolve target file path from groupId and DIR_TARGETS.
33
+ * @param {string} groupId - e.g. 'users' or 'subdir/users'
34
+ * @param {string} dirTargets - e.g. 'targets'
35
+ * @returns {string} Absolute path to the target file
36
+ */
37
+ function resolveTargetFilePath(groupId, dirTargets = 'targets') {
38
+ const baseDir = path.join(process.cwd(), dirTargets);
39
+ const relative = groupId.replace(/\//g, path.sep) + '.target.js';
40
+ return path.join(baseDir, relative);
41
+ }
42
+
43
+ /**
44
+ * Read a target file and all its relative-import dependencies (one level deep).
45
+ * @param {string} groupId - Source group id (e.g. 'users')
46
+ * @param {string} [dirTargets] - DIR_TARGETS (default from process.env or 'targets')
47
+ * @returns {Promise<Map<string, string>>} Map of absolute file path -> source code
48
+ */
49
+ export async function resolveDependencySources(groupId, dirTargets = process.env.DIR_TARGETS || 'targets') {
50
+ const targetPath = resolveTargetFilePath(groupId, dirTargets);
51
+ const result = new Map();
52
+
53
+ let targetSource;
54
+ try {
55
+ targetSource = await readFile(targetPath, 'utf8');
56
+ } catch {
57
+ return result;
58
+ }
59
+
60
+ result.set(targetPath, targetSource);
61
+ const targetDir = path.dirname(targetPath);
62
+ const relativeImports = extractRelativeImports(targetSource);
63
+
64
+ for (const rel of relativeImports) {
65
+ const resolvedPath = path.resolve(targetDir, rel);
66
+ try {
67
+ const code = await readFile(resolvedPath, 'utf8');
68
+ result.set(resolvedPath, code);
69
+ } catch {
70
+ // Skip missing or unreadable files
71
+ }
72
+ }
73
+
74
+ return result;
75
+ }
76
+
77
+ /**
78
+ * Format resolved dependency sources as a single string for LLM context.
79
+ * @param {Map<string, string>} sources - Map of file path -> source
80
+ * @param {string} [targetPath] - Path to the target file (excluded from "dependencies" label)
81
+ * @param {number} [maxChars] - Max total characters to include (default: DEPENDENCY_CONTEXT_MAX_CHARS)
82
+ * @returns {string}
83
+ */
84
+ export function formatDependencyContext(sources, targetPath, maxChars = DEPENDENCY_CONTEXT_MAX_CHARS) {
85
+ if (!sources || sources.size === 0) return '';
86
+ const lines = [];
87
+ let total = 0;
88
+ for (const [filePath, code] of sources) {
89
+ const label = filePath === targetPath ? 'Target' : path.relative(process.cwd(), filePath);
90
+ const block = `\n// --- ${label} ---\n${code}`;
91
+ if (total + block.length > maxChars) {
92
+ lines.push(block.slice(0, maxChars - total) + '\n// ... (truncated)');
93
+ break;
94
+ }
95
+ lines.push(block);
96
+ total += block.length;
97
+ }
98
+ return lines.join('\n');
99
+ }
100
+
101
+ export { extractRelativeImports, resolveTargetFilePath };
@@ -1,37 +1,37 @@
1
- /**
2
- * Shared constants for auto-documentation.
3
- * Centralizes OpenAPI version, method keys, and token/character limits.
4
- */
5
-
6
- /** OpenAPI 3.0 version string */
7
- export const OPENAPI_VERSION = '3.0.3';
8
-
9
- /** Lowercase HTTP method names (for method-keyed LLM response detection and OpenAPI operation keys). */
10
- export const METHOD_KEYS = new Set(['get', 'put', 'post', 'delete', 'patch', 'head', 'options']);
11
-
12
- /** When an endpoint accepts all HTTP methods, document it once under this key. */
13
- export const METHOD_AGNOSTIC_OPERATION_KEY = 'get';
14
-
15
- /** Max handler source length by level (chars; tokens roughly scale). Level 1 = moderate, 2 = high. */
16
- export const HANDLER_SOURCE_MAX_LENGTH_BY_LEVEL = { 1: 2800, 2: 6000 };
17
-
18
- /** Default max chars for dependency context in formatDependencyContext and level-2 prompts. */
19
- export const DEPENDENCY_CONTEXT_MAX_CHARS = 6000;
20
-
21
- /** Max handler source chars in single-endpoint enhance prompts (no deps). */
22
- export const PROMPT_HANDLER_SLICE = 3000;
23
-
24
- /** Max handler source chars in single-endpoint enhance prompts (with deps). */
25
- export const PROMPT_HANDLER_SLICE_WITH_DEPS = 4000;
26
-
27
- /** Max dependency source chars included in enhance/summarize prompts. */
28
- export const PROMPT_DEPENDENCY_SLICE = 5000;
29
-
30
- /** Max total code chars in group-summary prompt when no dependency context. */
31
- export const PROMPT_GROUP_CODE_LIMIT = 4000;
32
-
33
- /** Max total code chars in group-summary prompt when dependency context is present. */
34
- export const PROMPT_GROUP_CODE_LIMIT_WITH_DEPS = 6000;
35
-
36
- /** Max handler source chars per endpoint in group-summary code snippets. */
37
- export const PROMPT_GROUP_SNIPPET_CHARS = 800;
1
+ /**
2
+ * Shared constants for auto-documentation.
3
+ * Centralizes OpenAPI version, method keys, and token/character limits.
4
+ */
5
+
6
+ /** OpenAPI 3.0 version string */
7
+ export const OPENAPI_VERSION = '3.0.3';
8
+
9
+ /** Lowercase HTTP method names (for method-keyed LLM response detection and OpenAPI operation keys). */
10
+ export const METHOD_KEYS = new Set(['get', 'put', 'post', 'delete', 'patch', 'head', 'options']);
11
+
12
+ /** When an endpoint accepts all HTTP methods, document it once under this key. */
13
+ export const METHOD_AGNOSTIC_OPERATION_KEY = 'get';
14
+
15
+ /** Max handler source length by level (chars; tokens roughly scale). Level 1 = moderate, 2 = high. */
16
+ export const HANDLER_SOURCE_MAX_LENGTH_BY_LEVEL = { 1: 2800, 2: 6000 };
17
+
18
+ /** Default max chars for dependency context in formatDependencyContext and level-2 prompts. */
19
+ export const DEPENDENCY_CONTEXT_MAX_CHARS = 6000;
20
+
21
+ /** Max handler source chars in single-endpoint enhance prompts (no deps). */
22
+ export const PROMPT_HANDLER_SLICE = 3000;
23
+
24
+ /** Max handler source chars in single-endpoint enhance prompts (with deps). */
25
+ export const PROMPT_HANDLER_SLICE_WITH_DEPS = 4000;
26
+
27
+ /** Max dependency source chars included in enhance/summarize prompts. */
28
+ export const PROMPT_DEPENDENCY_SLICE = 5000;
29
+
30
+ /** Max total code chars in group-summary prompt when no dependency context. */
31
+ export const PROMPT_GROUP_CODE_LIMIT = 4000;
32
+
33
+ /** Max total code chars in group-summary prompt when dependency context is present. */
34
+ export const PROMPT_GROUP_CODE_LIMIT_WITH_DEPS = 6000;
35
+
36
+ /** Max handler source chars per endpoint in group-summary code snippets. */
37
+ export const PROMPT_GROUP_SNIPPET_CHARS = 800;
@@ -1,7 +1,7 @@
1
- /**
2
- * LLM provider for auto-documentation (docs-specific).
3
- * Use createProvider(config) with baseURL, apiKey, model.
4
- * For the generic LLM client only, use lib/llm.
5
- */
6
-
7
- export { LLMProvider, createProvider, extractJSON, extractJSONArray, reconcileOrderedTags } from './provider.js';
1
+ /**
2
+ * LLM provider for auto-documentation (docs-specific).
3
+ * Use createProvider(config) with baseURL, apiKey, model.
4
+ * For the generic LLM client only, use lib/llm.
5
+ */
6
+
7
+ export { LLMProvider, createProvider, extractJSON, extractJSONArray, reconcileOrderedTags } from './provider.js';