te.js 2.0.3 → 2.1.1
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.
- package/README.md +197 -187
- package/auto-docs/analysis/handler-analyzer.js +58 -58
- package/auto-docs/analysis/source-resolver.js +101 -101
- package/auto-docs/constants.js +37 -37
- package/auto-docs/docs-llm/index.js +7 -0
- package/auto-docs/{llm → docs-llm}/prompts.js +222 -222
- package/auto-docs/{llm → docs-llm}/provider.js +132 -187
- package/auto-docs/index.js +146 -146
- package/auto-docs/openapi/endpoint-processor.js +277 -277
- package/auto-docs/openapi/generator.js +107 -107
- package/auto-docs/openapi/level3.js +131 -131
- package/auto-docs/openapi/spec-builders.js +244 -244
- package/auto-docs/ui/docs-ui.js +186 -186
- package/auto-docs/utils/logger.js +17 -17
- package/auto-docs/utils/strip-usage.js +10 -10
- package/cli/docs-command.js +315 -315
- package/cli/fly-command.js +71 -71
- package/cli/index.js +56 -56
- package/database/index.js +165 -165
- package/database/mongodb.js +146 -146
- package/database/redis.js +201 -201
- package/docs/README.md +36 -36
- package/docs/ammo.md +362 -362
- package/docs/api-reference.md +490 -489
- package/docs/auto-docs.md +216 -215
- package/docs/cli.md +152 -152
- package/docs/configuration.md +275 -233
- package/docs/database.md +390 -391
- package/docs/error-handling.md +438 -417
- package/docs/file-uploads.md +333 -334
- package/docs/getting-started.md +214 -215
- package/docs/middleware.md +355 -356
- package/docs/rate-limiting.md +393 -394
- package/docs/routing.md +302 -302
- package/package.json +62 -62
- package/rate-limit/algorithms/fixed-window.js +141 -141
- package/rate-limit/algorithms/sliding-window.js +147 -147
- package/rate-limit/algorithms/token-bucket.js +115 -115
- package/rate-limit/base.js +165 -165
- package/rate-limit/index.js +147 -147
- package/rate-limit/storage/base.js +104 -104
- package/rate-limit/storage/memory.js +101 -101
- package/rate-limit/storage/redis.js +88 -88
- package/server/ammo/body-parser.js +220 -220
- package/server/ammo/dispatch-helper.js +103 -103
- package/server/ammo/enhancer.js +57 -57
- package/server/ammo.js +454 -356
- package/server/endpoint.js +97 -74
- package/server/error.js +9 -9
- package/server/errors/code-context.js +125 -0
- package/server/errors/llm-error-service.js +140 -0
- package/server/files/helper.js +33 -33
- package/server/files/uploader.js +143 -143
- package/server/handler.js +158 -113
- package/server/target.js +185 -175
- package/server/targets/middleware-validator.js +22 -22
- package/server/targets/path-validator.js +21 -21
- package/server/targets/registry.js +160 -160
- package/server/targets/shoot-validator.js +21 -21
- package/te.js +428 -363
- package/utils/auto-register.js +17 -17
- package/utils/configuration.js +64 -64
- package/utils/errors-llm-config.js +84 -0
- package/utils/request-logger.js +43 -43
- package/utils/status-codes.js +82 -82
- package/utils/tejas-entrypoint-html.js +18 -18
- package/auto-docs/llm/index.js +0 -6
- package/auto-docs/llm/parse.js +0 -88
package/server/endpoint.js
CHANGED
|
@@ -1,74 +1,97 @@
|
|
|
1
|
-
import isMiddlewareValid from './targets/middleware-validator.js';
|
|
2
|
-
import { isPathValid, standardizePath } from './targets/path-validator.js';
|
|
3
|
-
import isShootValid from './targets/shoot-validator.js';
|
|
4
|
-
|
|
5
|
-
class Endpoint {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.path = '';
|
|
8
|
-
this.middlewares = [];
|
|
9
|
-
this.handler = null;
|
|
10
|
-
this.metadata = null;
|
|
11
|
-
/**
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
1
|
+
import isMiddlewareValid from './targets/middleware-validator.js';
|
|
2
|
+
import { isPathValid, standardizePath } from './targets/path-validator.js';
|
|
3
|
+
import isShootValid from './targets/shoot-validator.js';
|
|
4
|
+
|
|
5
|
+
class Endpoint {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.path = '';
|
|
8
|
+
this.middlewares = [];
|
|
9
|
+
this.handler = null;
|
|
10
|
+
this.metadata = null;
|
|
11
|
+
/** Allowed HTTP methods (e.g. ['GET', 'POST']). null = method-agnostic. */
|
|
12
|
+
this.methods = null;
|
|
13
|
+
/** Source group (e.g. target file id) for grouping in docs. Set by loader before register(). */
|
|
14
|
+
this.group = null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setPath(base, path) {
|
|
18
|
+
const standardizedBase = standardizePath(base);
|
|
19
|
+
const standardizedPath = standardizePath(path);
|
|
20
|
+
|
|
21
|
+
let fullPath = `${standardizedBase}${standardizedPath}`;
|
|
22
|
+
if (fullPath.length === 0) fullPath = '/';
|
|
23
|
+
|
|
24
|
+
if (!isPathValid(fullPath)) return this;
|
|
25
|
+
|
|
26
|
+
this.path = fullPath;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setMiddlewares(middlewares) {
|
|
31
|
+
const validMiddlewares = middlewares.filter(isMiddlewareValid);
|
|
32
|
+
if (validMiddlewares.length === 0) return this;
|
|
33
|
+
|
|
34
|
+
this.middlewares = this.middlewares.concat(validMiddlewares);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
setHandler(handler) {
|
|
39
|
+
if (!isShootValid(handler)) return this;
|
|
40
|
+
this.handler = handler;
|
|
41
|
+
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
setMetadata(metadata) {
|
|
46
|
+
this.metadata = metadata ?? null;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string[]|null} methods - Allowed HTTP methods (e.g. ['GET', 'POST']). null = method-agnostic.
|
|
52
|
+
*/
|
|
53
|
+
setMethods(methods) {
|
|
54
|
+
if (methods == null) {
|
|
55
|
+
this.methods = null;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
const arr = Array.isArray(methods) ? methods : [methods];
|
|
59
|
+
let normalized = arr.map((m) => String(m).toUpperCase()).filter(Boolean);
|
|
60
|
+
if (normalized.includes('GET') && !normalized.includes('HEAD')) {
|
|
61
|
+
normalized = [...normalized, 'HEAD'];
|
|
62
|
+
}
|
|
63
|
+
this.methods = normalized.length > 0 ? normalized : null;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setGroup(group) {
|
|
68
|
+
this.group = group ?? null;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getPath() {
|
|
73
|
+
return this.path;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
getMiddlewares() {
|
|
77
|
+
return this.middlewares;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getHandler() {
|
|
81
|
+
return this.handler;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getMetadata() {
|
|
85
|
+
return this.metadata;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getMethods() {
|
|
89
|
+
return this.methods;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
getGroup() {
|
|
93
|
+
return this.group;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export default Endpoint;
|
package/server/error.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
class TejError extends Error {
|
|
2
|
-
constructor(code, message) {
|
|
3
|
-
super(message);
|
|
4
|
-
this.name = this.constructor.name;
|
|
5
|
-
this.code = code;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export default TejError;
|
|
1
|
+
class TejError extends Error {
|
|
2
|
+
constructor(code, message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = this.constructor.name;
|
|
5
|
+
this.code = code;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default TejError;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capture code context from the call stack: surrounding source with line numbers,
|
|
3
|
+
* including upstream callers and downstream code. Used by LLM error inference.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readFile } from 'node:fs/promises';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
|
|
10
|
+
/** Path segments that identify te.js internals (excluded from "user" stack frames). */
|
|
11
|
+
const INTERNAL_PATTERNS = [
|
|
12
|
+
'server/ammo.js',
|
|
13
|
+
'server/handler.js',
|
|
14
|
+
'server/errors/llm-error-service.js',
|
|
15
|
+
'server/errors/code-context.js',
|
|
16
|
+
'node_modules',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const LINES_ABOVE = 25;
|
|
20
|
+
const LINES_BELOW = 25;
|
|
21
|
+
const MAX_FRAMES = 6;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Parse a single stack frame line to extract file path, line, and column.
|
|
25
|
+
* Handles "at fn (file:///path:line:col)" and "at file:///path:line:col" and "at /path:line:col".
|
|
26
|
+
* @param {string} line
|
|
27
|
+
* @returns {{ filePath: string, line: number, column: number } | null}
|
|
28
|
+
*/
|
|
29
|
+
function parseStackFrame(line) {
|
|
30
|
+
const trimmed = line.trim();
|
|
31
|
+
if (!trimmed.startsWith('at ')) return null;
|
|
32
|
+
// Last occurrence of :number:number is line:column (path may contain colons on Windows/file URL)
|
|
33
|
+
const match = trimmed.match(/:(\d+):(\d+)\s*\)?\s*$/);
|
|
34
|
+
if (!match) return null;
|
|
35
|
+
const lineNum = parseInt(match[1], 10);
|
|
36
|
+
const colNum = parseInt(match[2], 10);
|
|
37
|
+
const before = trimmed.slice(0, trimmed.lastIndexOf(':' + match[1] + ':' + match[2]));
|
|
38
|
+
// Strip "at ... (" or "at " prefix to get path
|
|
39
|
+
let filePath = before.replace(/^\s*at\s+(?:.*?\s+\()?/, '').replace(/\)?\s*$/, '').trim();
|
|
40
|
+
if (filePath.startsWith('file://')) {
|
|
41
|
+
try {
|
|
42
|
+
filePath = fileURLToPath(filePath);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!filePath || lineNum <= 0) return null;
|
|
48
|
+
return { filePath, line: lineNum, column: colNum };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Return true if this file path is internal (te.js / node_modules) and should be skipped for user context.
|
|
53
|
+
* @param {string} filePath - Absolute or relative path
|
|
54
|
+
*/
|
|
55
|
+
function isInternalFrame(filePath) {
|
|
56
|
+
const normalized = path.normalize(filePath).replace(/\\/g, '/');
|
|
57
|
+
return INTERNAL_PATTERNS.some((p) => normalized.includes(p));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read source file and return lines [lineNum - LINES_ABOVE, lineNum + LINES_BELOW] with line numbers.
|
|
62
|
+
* @param {string} filePath - Absolute path
|
|
63
|
+
* @param {number} lineNum - Center line (1-based)
|
|
64
|
+
* @returns {Promise<{ file: string, line: number, snippet: string } | null>}
|
|
65
|
+
*/
|
|
66
|
+
async function readSnippet(filePath, lineNum) {
|
|
67
|
+
let content;
|
|
68
|
+
try {
|
|
69
|
+
content = await readFile(filePath, 'utf-8');
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const lines = content.split(/\r?\n/);
|
|
74
|
+
const start = Math.max(0, lineNum - 1 - LINES_ABOVE);
|
|
75
|
+
const end = Math.min(lines.length, lineNum + LINES_BELOW);
|
|
76
|
+
const snippet = lines
|
|
77
|
+
.slice(start, end)
|
|
78
|
+
.map((text, i) => {
|
|
79
|
+
const num = start + i + 1;
|
|
80
|
+
const marker = num === lineNum ? ' →' : ' ';
|
|
81
|
+
return `${String(num).padStart(4)}${marker} ${text}`;
|
|
82
|
+
})
|
|
83
|
+
.join('\n');
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
file: filePath,
|
|
87
|
+
line: lineNum,
|
|
88
|
+
snippet,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Capture code context from the current call stack: parse stack, filter to user frames,
|
|
94
|
+
* and read surrounding source (with line numbers) for each frame. First frame is the
|
|
95
|
+
* throw site; remaining frames are upstream callers. Each snippet includes lines
|
|
96
|
+
* above and below (downstream in the same function).
|
|
97
|
+
*
|
|
98
|
+
* @param {string} [stack] - Stack string (e.g. new Error().stack). If omitted, captures current stack.
|
|
99
|
+
* @param {{ maxFrames?: number, linesAround?: number }} [options]
|
|
100
|
+
* @returns {Promise<{ snippets: Array<{ file: string, line: number, snippet: string }> }>}
|
|
101
|
+
*/
|
|
102
|
+
export async function captureCodeContext(stack, options = {}) {
|
|
103
|
+
const stackStr = typeof stack === 'string' && stack ? stack : new Error().stack;
|
|
104
|
+
if (!stackStr) return { snippets: [] };
|
|
105
|
+
|
|
106
|
+
const maxFrames = options.maxFrames ?? MAX_FRAMES;
|
|
107
|
+
const lines = stackStr.split('\n');
|
|
108
|
+
const frames = [];
|
|
109
|
+
|
|
110
|
+
for (const line of lines) {
|
|
111
|
+
const parsed = parseStackFrame(line);
|
|
112
|
+
if (!parsed) continue;
|
|
113
|
+
if (isInternalFrame(parsed.filePath)) continue;
|
|
114
|
+
frames.push(parsed);
|
|
115
|
+
if (frames.length >= maxFrames) break;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const snippets = [];
|
|
119
|
+
for (const { filePath, line } of frames) {
|
|
120
|
+
const one = await readSnippet(filePath, line);
|
|
121
|
+
if (one) snippets.push(one);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { snippets };
|
|
125
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM-based error inference: given code context (surrounding + upstream/downstream),
|
|
3
|
+
* returns statusCode and message (and optionally devInsight in non-production).
|
|
4
|
+
* Uses shared lib/llm with errors.llm config. Developers do not pass an error object;
|
|
5
|
+
* the LLM infers from the code where ammo.throw() was called.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createProvider } from '../../lib/llm/index.js';
|
|
9
|
+
import { extractJSON } from '../../lib/llm/parse.js';
|
|
10
|
+
import { getErrorsLlmConfig } from '../../utils/errors-llm-config.js';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_STATUS = 500;
|
|
13
|
+
const DEFAULT_MESSAGE = 'Internal Server Error';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Build prompt text from code context (and optional error) for the LLM.
|
|
17
|
+
* @param {object} context
|
|
18
|
+
* @param {{ snippets: Array<{ file: string, line: number, snippet: string }> }} context.codeContext - Source snippets with line numbers (first = throw site, rest = upstream).
|
|
19
|
+
* @param {string} [context.method] - HTTP method.
|
|
20
|
+
* @param {string} [context.path] - Request path.
|
|
21
|
+
* @param {boolean} [context.includeDevInsight] - If true, ask for devInsight.
|
|
22
|
+
* @param {'endUser'|'developer'} [context.messageType] - Message tone.
|
|
23
|
+
* @param {string|Error|undefined} [context.error] - Optional error if one was passed (secondary signal).
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
function buildPrompt(context) {
|
|
27
|
+
const { codeContext, method, path, includeDevInsight, messageType, error } = context;
|
|
28
|
+
const forDeveloper = messageType === 'developer';
|
|
29
|
+
|
|
30
|
+
const requestPart = [method, path].filter(Boolean).length
|
|
31
|
+
? `Request: ${[method, path].filter(Boolean).join(' ')}`
|
|
32
|
+
: '';
|
|
33
|
+
|
|
34
|
+
let codePart = 'No code context was captured.';
|
|
35
|
+
if (codeContext?.snippets?.length) {
|
|
36
|
+
codePart = codeContext.snippets
|
|
37
|
+
.map((s, i) => {
|
|
38
|
+
const label = i === 0 ? 'Call site (where ammo.throw() was invoked)' : `Upstream caller ${i}`;
|
|
39
|
+
return `--- ${label}: ${s.file} (line ${s.line}) ---\n${s.snippet}`;
|
|
40
|
+
})
|
|
41
|
+
.join('\n\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let errorPart = '';
|
|
45
|
+
if (error !== undefined && error !== null) {
|
|
46
|
+
if (error instanceof Error) {
|
|
47
|
+
errorPart = `\nOptional error message (may be empty): ${error.message}`;
|
|
48
|
+
} else {
|
|
49
|
+
errorPart = `\nOptional error/message: ${String(error)}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const devPart = includeDevInsight
|
|
54
|
+
? '\nAlso provide a short "devInsight" string (one or two sentences) for the developer: (a) Is this likely a bug in the code or an environment/setup issue? (b) If the developer can fix it, suggest the fix. Be concise.'
|
|
55
|
+
: '';
|
|
56
|
+
|
|
57
|
+
const messageInstruction = forDeveloper
|
|
58
|
+
? '- "message": string (short message for developers: may include technical detail, error type, or cause; do not include raw stack traces)'
|
|
59
|
+
: '- "message": string (short, end-user-facing message: safe for clients; do not expose stack traces, internal details, or technical jargon)';
|
|
60
|
+
|
|
61
|
+
return `You are helping map an application error to an HTTP response. The developer called ammo.throw() (or an error was thrown and caught) at the call site below. Use the surrounding code with line numbers and all upstream/downstream context to infer what went wrong and choose an appropriate HTTP status and message.
|
|
62
|
+
|
|
63
|
+
Consider:
|
|
64
|
+
- The code BEFORE the throw (upstream in the same function and in callers) — what led to this point.
|
|
65
|
+
- The code AFTER the throw line (downstream) — what would have run next; this shows intent and expected flow.
|
|
66
|
+
- The first snippet is the call site (line marked with →); later snippets are upstream callers.
|
|
67
|
+
|
|
68
|
+
${requestPart ? requestPart + '\n\n' : ''}Code context (with line numbers; → marks the throw line):
|
|
69
|
+
|
|
70
|
+
${codePart}${errorPart}
|
|
71
|
+
${devPart ? '\n' + devPart : ''}
|
|
72
|
+
|
|
73
|
+
Respond with only valid JSON, no markdown or explanation. Use this shape:
|
|
74
|
+
- "statusCode": number (HTTP status, typically 4xx or 5xx; use 500 for generic/server errors)
|
|
75
|
+
${messageInstruction}
|
|
76
|
+
${includeDevInsight ? '- "devInsight": string (brief note for the developer only)' : ''}
|
|
77
|
+
|
|
78
|
+
JSON:`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Infer HTTP statusCode and message (and optionally devInsight) from code context using the LLM.
|
|
83
|
+
* Uses errors.llm config (getErrorsLlmConfig). Call only when errors.llm.enabled is true and config is valid.
|
|
84
|
+
* The primary input is codeContext (surrounding + upstream/downstream snippets); error is optional.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} context - Context for the prompt.
|
|
87
|
+
* @param {{ snippets: Array<{ file: string, line: number, snippet: string }> }} context.codeContext - Source snippets with line numbers (from captureCodeContext).
|
|
88
|
+
* @param {string} [context.method] - HTTP method.
|
|
89
|
+
* @param {string} [context.path] - Request path.
|
|
90
|
+
* @param {boolean} [context.includeDevInsight] - In non-production, dev insight is included by default; set to false to disable.
|
|
91
|
+
* @param {'endUser'|'developer'} [context.messageType] - Override config: 'endUser' or 'developer'. Default from errors.llm.messageType.
|
|
92
|
+
* @param {string|Error|undefined} [context.error] - Optional error if the caller passed one (secondary signal).
|
|
93
|
+
* @returns {Promise<{ statusCode: number, message: string, devInsight?: string }>}
|
|
94
|
+
*/
|
|
95
|
+
export async function inferErrorFromContext(context) {
|
|
96
|
+
const config = getErrorsLlmConfig();
|
|
97
|
+
const { baseURL, apiKey, model, messageType: configMessageType } = config;
|
|
98
|
+
const provider = createProvider({ baseURL, apiKey, model });
|
|
99
|
+
|
|
100
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
101
|
+
const includeDevInsight = !isProduction && context.includeDevInsight !== false;
|
|
102
|
+
const messageType = context.messageType ?? configMessageType;
|
|
103
|
+
|
|
104
|
+
const prompt = buildPrompt({
|
|
105
|
+
codeContext: context.codeContext,
|
|
106
|
+
method: context.method,
|
|
107
|
+
path: context.path,
|
|
108
|
+
includeDevInsight,
|
|
109
|
+
messageType,
|
|
110
|
+
error: context.error,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const { content } = await provider.analyze(prompt);
|
|
114
|
+
const parsed = extractJSON(content);
|
|
115
|
+
|
|
116
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
117
|
+
return {
|
|
118
|
+
statusCode: DEFAULT_STATUS,
|
|
119
|
+
message: DEFAULT_MESSAGE,
|
|
120
|
+
...(includeDevInsight && { devInsight: 'Could not parse LLM response.' }),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let statusCode = Number(parsed.statusCode);
|
|
125
|
+
if (Number.isNaN(statusCode) || statusCode < 100 || statusCode > 599) {
|
|
126
|
+
statusCode = DEFAULT_STATUS;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const message =
|
|
130
|
+
typeof parsed.message === 'string' && parsed.message.trim()
|
|
131
|
+
? parsed.message.trim()
|
|
132
|
+
: DEFAULT_MESSAGE;
|
|
133
|
+
|
|
134
|
+
const result = { statusCode, message };
|
|
135
|
+
if (includeDevInsight && typeof parsed.devInsight === 'string' && parsed.devInsight.trim()) {
|
|
136
|
+
result.devInsight = parsed.devInsight.trim();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
}
|
package/server/files/helper.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import mime from 'mime';
|
|
2
|
-
|
|
3
|
-
const paths = (destination, filename) => {
|
|
4
|
-
const dir = `${process.cwd()}\\${destination}`;
|
|
5
|
-
const path = `${dir}\\${filename}`;
|
|
6
|
-
|
|
7
|
-
const absolute = path;
|
|
8
|
-
const relative = path.replace(process.cwd(), '');
|
|
9
|
-
|
|
10
|
-
return { dir, absolute, relative };
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const extAndType = (obj) => {
|
|
14
|
-
const contentType = obj.headers['content-type'];
|
|
15
|
-
const ext = mime.getExtension(contentType);
|
|
16
|
-
const type = mime.getType(ext);
|
|
17
|
-
return {
|
|
18
|
-
ext,
|
|
19
|
-
type,
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const extract = (contentDisposition, key) => {
|
|
24
|
-
if (!contentDisposition) {
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const parts = contentDisposition.split(';').map((part) => part.trim());
|
|
29
|
-
const part = parts.find((part) => part.startsWith(key));
|
|
30
|
-
return part ? part?.split('=')[1]?.trim()?.replace(/"/g, '') : undefined;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export { extAndType, extract, paths };
|
|
1
|
+
import mime from 'mime';
|
|
2
|
+
|
|
3
|
+
const paths = (destination, filename) => {
|
|
4
|
+
const dir = `${process.cwd()}\\${destination}`;
|
|
5
|
+
const path = `${dir}\\${filename}`;
|
|
6
|
+
|
|
7
|
+
const absolute = path;
|
|
8
|
+
const relative = path.replace(process.cwd(), '');
|
|
9
|
+
|
|
10
|
+
return { dir, absolute, relative };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const extAndType = (obj) => {
|
|
14
|
+
const contentType = obj.headers['content-type'];
|
|
15
|
+
const ext = mime.getExtension(contentType);
|
|
16
|
+
const type = mime.getType(ext);
|
|
17
|
+
return {
|
|
18
|
+
ext,
|
|
19
|
+
type,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const extract = (contentDisposition, key) => {
|
|
24
|
+
if (!contentDisposition) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const parts = contentDisposition.split(';').map((part) => part.trim());
|
|
29
|
+
const part = parts.find((part) => part.startsWith(key));
|
|
30
|
+
return part ? part?.split('=')[1]?.trim()?.replace(/"/g, '') : undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { extAndType, extract, paths };
|