@nestia/core 12.0.0-dev.20260601.1 → 12.0.0-dev.20260612.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.
- package/LICENSE +21 -21
- package/MIGRATION.md +169 -169
- package/README.md +93 -93
- package/lib/adaptors/McpAdaptor.d.ts +75 -0
- package/lib/adaptors/McpAdaptor.js +257 -0
- package/lib/adaptors/McpAdaptor.js.map +1 -0
- package/lib/adaptors/WebSocketAdaptor.js +4 -4
- package/lib/adaptors/WebSocketAdaptor.js.map +1 -1
- package/lib/decorators/McpRoute.d.ts +69 -0
- package/lib/decorators/McpRoute.js +58 -0
- package/lib/decorators/McpRoute.js.map +1 -0
- package/lib/decorators/TypedParam.js +4 -4
- package/lib/decorators/TypedParam.js.map +1 -1
- package/lib/decorators/TypedRoute.js +1 -1
- package/lib/decorators/TypedRoute.js.map +1 -1
- package/lib/decorators/internal/IMcpRouteReflect.d.ts +2 -0
- package/lib/decorators/internal/IMcpRouteReflect.js +3 -0
- package/lib/decorators/internal/IMcpRouteReflect.js.map +1 -0
- package/lib/decorators/internal/get_path_and_querify.js +4 -4
- package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
- package/lib/decorators/internal/get_path_and_stringify.js +4 -4
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
- package/lib/decorators/internal/load_controller.js +34 -65
- package/lib/decorators/internal/load_controller.js.map +1 -1
- package/lib/decorators/internal/validate_request_body.js +4 -4
- package/lib/decorators/internal/validate_request_body.js.map +1 -1
- package/lib/decorators/internal/validate_request_form_data.js +4 -4
- package/lib/decorators/internal/validate_request_form_data.js.map +1 -1
- package/lib/decorators/internal/validate_request_headers.js +4 -4
- package/lib/decorators/internal/validate_request_headers.js.map +1 -1
- package/lib/decorators/internal/validate_request_query.js +4 -4
- package/lib/decorators/internal/validate_request_query.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +2 -0
- package/lib/module.js.map +1 -1
- package/native/cmd/ttsc-nestia/main.go +11 -11
- package/native/go.mod +32 -32
- package/native/go.sum +54 -54
- package/native/plugin/plan.go +102 -102
- package/native/transform/ast.go +32 -32
- package/native/transform/build.go +380 -444
- package/native/transform/cleanup.go +408 -408
- package/native/transform/contributor.go +97 -68
- package/native/transform/core_querify.go +231 -227
- package/native/transform/core_transform.go +1996 -1713
- package/native/transform/core_websocket.go +115 -115
- package/native/transform/exports.go +13 -13
- package/native/transform/mcp_transform.go +414 -0
- package/native/transform/node_transform.go +357 -0
- package/native/transform/path_rewrite.go +285 -285
- package/native/transform/printer.go +244 -244
- package/native/transform/rewrite.go +668 -662
- package/native/transform/run.go +73 -73
- package/native/transform/transform.go +336 -403
- package/native/transform/typia_fast.go +352 -326
- package/native/transform/typia_replacement.go +24 -24
- package/native/transform.cjs +43 -43
- package/package.json +15 -8
- package/src/adaptors/McpAdaptor.ts +276 -0
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/EncryptedBody.ts +97 -97
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +213 -213
- package/src/decorators/HumanRoute.ts +21 -21
- package/src/decorators/McpRoute.ts +154 -0
- package/src/decorators/NoTransformConfigurationError.ts +40 -40
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +180 -180
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +187 -187
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/TypedRoute.ts +198 -196
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/doNotThrowTransformError.ts +5 -5
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IMcpRouteReflect.ts +40 -0
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/get_path_and_querify.ts +94 -94
- package/src/decorators/internal/get_path_and_stringify.ts +110 -110
- package/src/decorators/internal/get_text_body.ts +16 -16
- package/src/decorators/internal/headers_to_object.ts +11 -11
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +91 -76
- package/src/decorators/internal/route_error.ts +43 -43
- package/src/decorators/internal/validate_request_body.ts +64 -64
- package/src/decorators/internal/validate_request_form_data.ts +67 -67
- package/src/decorators/internal/validate_request_headers.ts +76 -76
- package/src/decorators/internal/validate_request_query.ts +83 -83
- package/src/index.ts +5 -5
- package/src/module.ts +25 -23
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +30 -30
- package/src/transform.ts +101 -101
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +115 -115
- package/src/utils/Singleton.ts +16 -16
- package/src/utils/SourceFinder.ts +54 -54
- package/src/utils/VersioningStrategy.ts +27 -27
- package/native/transform/cleanup_test.go +0 -76
- package/native/transform/commonjs_import_alias_test.go +0 -49
- package/native/transform/core_dispatch_test.go +0 -127
- package/native/transform/path_rewrite_test.go +0 -243
- package/native/transform/rewrite_test.go +0 -118
- package/native/transform/rewrite_unique_base_test.go +0 -48
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
package transform
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"fmt"
|
|
5
|
-
"strings"
|
|
6
|
-
|
|
7
|
-
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
8
|
-
shimscanner "github.com/microsoft/typescript-go/shim/scanner"
|
|
9
|
-
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
func validateNestiaCoreWebSocketRoute(
|
|
13
|
-
prog *driver.Program,
|
|
14
|
-
context nestiaCoreFileContext,
|
|
15
|
-
method *shimast.Node,
|
|
16
|
-
call *shimast.CallExpression,
|
|
17
|
-
segments []string,
|
|
18
|
-
) []Diagnostic {
|
|
19
|
-
if call == nil || len(segments) == 0 || segments[len(segments)-1] != "WebSocketRoute" {
|
|
20
|
-
_ = call
|
|
21
|
-
return nil
|
|
22
|
-
}
|
|
23
|
-
diagnostics := []Diagnostic{}
|
|
24
|
-
accepted := false
|
|
25
|
-
methodDecl := method.AsMethodDeclaration()
|
|
26
|
-
if methodDecl == nil || methodDecl.Parameters == nil {
|
|
27
|
-
return []Diagnostic{nestiaCoreWebSocketDiagnostic(context.file, method, "WebSocketRoute", fmt.Sprintf(
|
|
28
|
-
"method %q must have at least one parameter decorated by @WebSocketRoute.Acceptor().",
|
|
29
|
-
NodeName(method),
|
|
30
|
-
))}
|
|
31
|
-
}
|
|
32
|
-
for _, param := range methodDecl.Parameters.Nodes {
|
|
33
|
-
category := nestiaCoreWebSocketParameterCategory(prog, param)
|
|
34
|
-
name := NodeName(param)
|
|
35
|
-
if category == "" {
|
|
36
|
-
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
37
|
-
"parameter %q is not decorated with nested function of WebSocketRoute module.",
|
|
38
|
-
name,
|
|
39
|
-
)))
|
|
40
|
-
continue
|
|
41
|
-
}
|
|
42
|
-
switch category {
|
|
43
|
-
case "Acceptor":
|
|
44
|
-
accepted = true
|
|
45
|
-
if strings.HasPrefix(nestiaCoreWebSocketParameterTypeName(param), "WebSocketAcceptor") == false {
|
|
46
|
-
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
47
|
-
"parameter %q must have WebSocketAcceptor<Header, Provider, Listener> type.",
|
|
48
|
-
name,
|
|
49
|
-
)))
|
|
50
|
-
}
|
|
51
|
-
case "Driver":
|
|
52
|
-
if strings.HasPrefix(nestiaCoreWebSocketParameterTypeName(param), "Driver") == false {
|
|
53
|
-
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
54
|
-
"parameter %q must have Driver<Listener> type.",
|
|
55
|
-
name,
|
|
56
|
-
)))
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if accepted == false {
|
|
61
|
-
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, method, "WebSocketRoute", fmt.Sprintf(
|
|
62
|
-
"method %q must have at least one parameter decorated by @WebSocketRoute.Acceptor().",
|
|
63
|
-
NodeName(method),
|
|
64
|
-
)))
|
|
65
|
-
}
|
|
66
|
-
return diagnostics
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
func nestiaCoreWebSocketParameterCategory(prog *driver.Program, param *shimast.Node) string {
|
|
70
|
-
if param == nil || len(param.Decorators()) != 1 {
|
|
71
|
-
return ""
|
|
72
|
-
}
|
|
73
|
-
call, segments, ok := nestiaCoreDecoratorCall(prog, param.Decorators()[0])
|
|
74
|
-
if ok == false || len(segments) < 2 || segments[len(segments)-2] != "WebSocketRoute" {
|
|
75
|
-
_ = call
|
|
76
|
-
return ""
|
|
77
|
-
}
|
|
78
|
-
return segments[len(segments)-1]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
func nestiaCoreWebSocketParameterTypeName(param *shimast.Node) string {
|
|
82
|
-
if param == nil || param.AsParameterDeclaration() == nil || param.AsParameterDeclaration().Type == nil {
|
|
83
|
-
return ""
|
|
84
|
-
}
|
|
85
|
-
text := NodeText(param.AsParameterDeclaration().Type)
|
|
86
|
-
text = strings.TrimSpace(text)
|
|
87
|
-
if index := strings.Index(text, "<"); index >= 0 {
|
|
88
|
-
text = text[:index]
|
|
89
|
-
}
|
|
90
|
-
if index := strings.LastIndex(text, "."); index >= 0 {
|
|
91
|
-
text = text[index+1:]
|
|
92
|
-
}
|
|
93
|
-
return text
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
func nestiaCoreWebSocketDiagnostic(file *shimast.SourceFile, node *shimast.Node, kind string, message string) Diagnostic {
|
|
97
|
-
filePath := ""
|
|
98
|
-
line, column := 0, 0
|
|
99
|
-
if file != nil {
|
|
100
|
-
filePath = file.FileName()
|
|
101
|
-
if node != nil {
|
|
102
|
-
if pos := node.Pos(); pos >= 0 {
|
|
103
|
-
l, c := shimscanner.GetECMALineAndByteOffsetOfPosition(file, pos)
|
|
104
|
-
line, column = l+1, c+1
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
return Diagnostic{
|
|
109
|
-
File: filePath,
|
|
110
|
-
Line: line,
|
|
111
|
-
Column: column,
|
|
112
|
-
Code: "nestia.core." + kind,
|
|
113
|
-
Message: message,
|
|
114
|
-
}
|
|
115
|
-
}
|
|
1
|
+
package transform
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"strings"
|
|
6
|
+
|
|
7
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
8
|
+
shimscanner "github.com/microsoft/typescript-go/shim/scanner"
|
|
9
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func validateNestiaCoreWebSocketRoute(
|
|
13
|
+
prog *driver.Program,
|
|
14
|
+
context nestiaCoreFileContext,
|
|
15
|
+
method *shimast.Node,
|
|
16
|
+
call *shimast.CallExpression,
|
|
17
|
+
segments []string,
|
|
18
|
+
) []Diagnostic {
|
|
19
|
+
if call == nil || len(segments) == 0 || segments[len(segments)-1] != "WebSocketRoute" {
|
|
20
|
+
_ = call
|
|
21
|
+
return nil
|
|
22
|
+
}
|
|
23
|
+
diagnostics := []Diagnostic{}
|
|
24
|
+
accepted := false
|
|
25
|
+
methodDecl := method.AsMethodDeclaration()
|
|
26
|
+
if methodDecl == nil || methodDecl.Parameters == nil {
|
|
27
|
+
return []Diagnostic{nestiaCoreWebSocketDiagnostic(context.file, method, "WebSocketRoute", fmt.Sprintf(
|
|
28
|
+
"method %q must have at least one parameter decorated by @WebSocketRoute.Acceptor().",
|
|
29
|
+
NodeName(method),
|
|
30
|
+
))}
|
|
31
|
+
}
|
|
32
|
+
for _, param := range methodDecl.Parameters.Nodes {
|
|
33
|
+
category := nestiaCoreWebSocketParameterCategory(prog, param)
|
|
34
|
+
name := NodeName(param)
|
|
35
|
+
if category == "" {
|
|
36
|
+
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
37
|
+
"parameter %q is not decorated with nested function of WebSocketRoute module.",
|
|
38
|
+
name,
|
|
39
|
+
)))
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
switch category {
|
|
43
|
+
case "Acceptor":
|
|
44
|
+
accepted = true
|
|
45
|
+
if strings.HasPrefix(nestiaCoreWebSocketParameterTypeName(param), "WebSocketAcceptor") == false {
|
|
46
|
+
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
47
|
+
"parameter %q must have WebSocketAcceptor<Header, Provider, Listener> type.",
|
|
48
|
+
name,
|
|
49
|
+
)))
|
|
50
|
+
}
|
|
51
|
+
case "Driver":
|
|
52
|
+
if strings.HasPrefix(nestiaCoreWebSocketParameterTypeName(param), "Driver") == false {
|
|
53
|
+
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, param, "WebSocketRoute", fmt.Sprintf(
|
|
54
|
+
"parameter %q must have Driver<Listener> type.",
|
|
55
|
+
name,
|
|
56
|
+
)))
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if accepted == false {
|
|
61
|
+
diagnostics = append(diagnostics, nestiaCoreWebSocketDiagnostic(context.file, method, "WebSocketRoute", fmt.Sprintf(
|
|
62
|
+
"method %q must have at least one parameter decorated by @WebSocketRoute.Acceptor().",
|
|
63
|
+
NodeName(method),
|
|
64
|
+
)))
|
|
65
|
+
}
|
|
66
|
+
return diagnostics
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func nestiaCoreWebSocketParameterCategory(prog *driver.Program, param *shimast.Node) string {
|
|
70
|
+
if param == nil || len(param.Decorators()) != 1 {
|
|
71
|
+
return ""
|
|
72
|
+
}
|
|
73
|
+
call, segments, ok := nestiaCoreDecoratorCall(prog, param.Decorators()[0])
|
|
74
|
+
if ok == false || len(segments) < 2 || segments[len(segments)-2] != "WebSocketRoute" {
|
|
75
|
+
_ = call
|
|
76
|
+
return ""
|
|
77
|
+
}
|
|
78
|
+
return segments[len(segments)-1]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func nestiaCoreWebSocketParameterTypeName(param *shimast.Node) string {
|
|
82
|
+
if param == nil || param.AsParameterDeclaration() == nil || param.AsParameterDeclaration().Type == nil {
|
|
83
|
+
return ""
|
|
84
|
+
}
|
|
85
|
+
text := NodeText(param.AsParameterDeclaration().Type)
|
|
86
|
+
text = strings.TrimSpace(text)
|
|
87
|
+
if index := strings.Index(text, "<"); index >= 0 {
|
|
88
|
+
text = text[:index]
|
|
89
|
+
}
|
|
90
|
+
if index := strings.LastIndex(text, "."); index >= 0 {
|
|
91
|
+
text = text[index+1:]
|
|
92
|
+
}
|
|
93
|
+
return text
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func nestiaCoreWebSocketDiagnostic(file *shimast.SourceFile, node *shimast.Node, kind string, message string) Diagnostic {
|
|
97
|
+
filePath := ""
|
|
98
|
+
line, column := 0, 0
|
|
99
|
+
if file != nil {
|
|
100
|
+
filePath = file.FileName()
|
|
101
|
+
if node != nil {
|
|
102
|
+
if pos := node.Pos(); pos >= 0 {
|
|
103
|
+
l, c := shimscanner.GetECMALineAndByteOffsetOfPosition(file, pos)
|
|
104
|
+
line, column = l+1, c+1
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return Diagnostic{
|
|
109
|
+
File: filePath,
|
|
110
|
+
Line: line,
|
|
111
|
+
Column: column,
|
|
112
|
+
Code: "nestia.core." + kind,
|
|
113
|
+
Message: message,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
package transform
|
|
2
|
-
|
|
3
|
-
// NewSourceRewrite builds a SourceRewrite from another Go module (e.g. the
|
|
4
|
-
// `@nestia/sdk` plugin). SourceRewrite keeps its span fields unexported so
|
|
5
|
-
// that the only way to produce one is this constructor or the in-package
|
|
6
|
-
// collectors — every rewrite therefore carries a validated [start,end) span.
|
|
7
|
-
func NewSourceRewrite(start, end int, replacement string) SourceRewrite {
|
|
8
|
-
return SourceRewrite{
|
|
9
|
-
start: start,
|
|
10
|
-
end: end,
|
|
11
|
-
replacement: replacement,
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
package transform
|
|
2
|
+
|
|
3
|
+
// NewSourceRewrite builds a SourceRewrite from another Go module (e.g. the
|
|
4
|
+
// `@nestia/sdk` plugin). SourceRewrite keeps its span fields unexported so
|
|
5
|
+
// that the only way to produce one is this constructor or the in-package
|
|
6
|
+
// collectors — every rewrite therefore carries a validated [start,end) span.
|
|
7
|
+
func NewSourceRewrite(start, end int, replacement string) SourceRewrite {
|
|
8
|
+
return SourceRewrite{
|
|
9
|
+
start: start,
|
|
10
|
+
end: end,
|
|
11
|
+
replacement: replacement,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
package transform
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"strings"
|
|
6
|
+
|
|
7
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
8
|
+
shimchecker "github.com/microsoft/typescript-go/shim/checker"
|
|
9
|
+
shimprinter "github.com/microsoft/typescript-go/shim/printer"
|
|
10
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
11
|
+
nativecontext "github.com/samchon/typia/packages/typia/native/core/context"
|
|
12
|
+
nativefactories "github.com/samchon/typia/packages/typia/native/core/factories"
|
|
13
|
+
nativellm "github.com/samchon/typia/packages/typia/native/core/programmers/llm"
|
|
14
|
+
schemametadata "github.com/samchon/typia/packages/typia/native/core/schemas/metadata"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
func nestiaCoreGenerateMcpRouteParams(
|
|
18
|
+
prog *driver.Program,
|
|
19
|
+
importer *nativecontext.ImportProgrammer,
|
|
20
|
+
ec *shimprinter.EmitContext,
|
|
21
|
+
options nestiaCoreOptions,
|
|
22
|
+
modulo *shimast.Node,
|
|
23
|
+
typ *shimchecker.Type,
|
|
24
|
+
) *shimast.Node {
|
|
25
|
+
nestiaCoreAnalyzeMcpParameters(prog, typ)
|
|
26
|
+
return nestiaCoreGenerateTypedBody(prog, importer, ec, options, modulo, typ)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
func nestiaCoreMcpRouteArgumentNode(
|
|
30
|
+
prog *driver.Program,
|
|
31
|
+
importer *nativecontext.ImportProgrammer,
|
|
32
|
+
ec *shimprinter.EmitContext,
|
|
33
|
+
_ nestiaCoreOptions,
|
|
34
|
+
file *shimast.SourceFile,
|
|
35
|
+
method *shimast.Node,
|
|
36
|
+
call *shimast.CallExpression,
|
|
37
|
+
returnType *shimchecker.Type,
|
|
38
|
+
) (*shimast.Node, error) {
|
|
39
|
+
return safeNestiaCoreGenerateNode(func() (*shimast.Node, error) {
|
|
40
|
+
if nestiaCoreArgumentCount(call) != 1 {
|
|
41
|
+
return nil, fmt.Errorf("@McpRoute() requires one string literal tool name")
|
|
42
|
+
}
|
|
43
|
+
input := call.Arguments.Nodes[0]
|
|
44
|
+
if input.Kind != shimast.KindStringLiteral && input.Kind != shimast.KindObjectLiteralExpression {
|
|
45
|
+
return nil, fmt.Errorf("@McpRoute() argument must be a string literal tool name")
|
|
46
|
+
}
|
|
47
|
+
param, err := nestiaCoreMcpRouteParameter(prog, method)
|
|
48
|
+
if err != nil {
|
|
49
|
+
return nil, err
|
|
50
|
+
}
|
|
51
|
+
inputSchema := nativellm.LlmParametersProgrammer.Write(nativellm.LlmParametersProgrammer_IWriteProps{
|
|
52
|
+
Context: nestiaCoreTypiaContext(prog, importer, ec, false, false, false),
|
|
53
|
+
Metadata: nestiaCoreAnalyzeMcpParameters(prog, param.typ),
|
|
54
|
+
Config: nestiaCoreMcpLlmConfig(),
|
|
55
|
+
})
|
|
56
|
+
nestiaCoreAnalyzeMcpReturn(prog, returnType)
|
|
57
|
+
doc := nestiaCoreMcpRouteJSDoc(file, method)
|
|
58
|
+
return nestiaCoreMcpRouteConfigNode(input, inputSchema, doc, ec), nil
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func nestiaCoreMcpRouteConfigNode(
|
|
63
|
+
input *shimast.Node,
|
|
64
|
+
inputSchema *shimast.Node,
|
|
65
|
+
doc nestiaCoreMcpRouteDoc,
|
|
66
|
+
ec *shimprinter.EmitContext,
|
|
67
|
+
) *shimast.Node {
|
|
68
|
+
f := nativecontext.EmitFactoryOf(nestiaCoreFactory, ec)
|
|
69
|
+
properties := []*shimast.Node{}
|
|
70
|
+
if input.Kind == shimast.KindStringLiteral {
|
|
71
|
+
properties = append(properties, nestiaCoreProperty("name", f.NewStringLiteral(input.Text(), shimast.TokenFlagsNone), ec))
|
|
72
|
+
} else {
|
|
73
|
+
if literal := input.AsObjectLiteralExpression(); literal != nil && literal.Properties != nil {
|
|
74
|
+
properties = append(properties, literal.Properties.Nodes...)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if nestiaCoreObjectLiteralHasProperty(properties, "inputSchema") == false {
|
|
78
|
+
properties = append(properties, nestiaCoreProperty("inputSchema", inputSchema, ec))
|
|
79
|
+
}
|
|
80
|
+
if doc.Description != "" && nestiaCoreObjectLiteralHasProperty(properties, "description") == false {
|
|
81
|
+
properties = append(properties, nestiaCoreProperty("description", f.NewStringLiteral(doc.Description, shimast.TokenFlagsNone), ec))
|
|
82
|
+
}
|
|
83
|
+
if doc.Title != "" && nestiaCoreObjectLiteralHasProperty(properties, "title") == false {
|
|
84
|
+
properties = append(properties, nestiaCoreProperty("title", f.NewStringLiteral(doc.Title, shimast.TokenFlagsNone), ec))
|
|
85
|
+
}
|
|
86
|
+
return f.NewObjectLiteralExpression(f.NewNodeList(properties), true)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func nestiaCoreMcpRouteAlreadyTransformed(call *shimast.CallExpression) bool {
|
|
90
|
+
if call == nil || nestiaCoreArgumentCount(call) != 1 {
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
arg := call.Arguments.Nodes[0]
|
|
94
|
+
if arg == nil || arg.Kind != shimast.KindObjectLiteralExpression {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
literal := arg.AsObjectLiteralExpression()
|
|
98
|
+
if literal == nil || literal.Properties == nil {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
return nestiaCoreObjectLiteralHasProperty(literal.Properties.Nodes, "inputSchema")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func nestiaCoreObjectLiteralHasProperty(properties []*shimast.Node, name string) bool {
|
|
105
|
+
for _, property := range properties {
|
|
106
|
+
if property == nil || property.Kind != shimast.KindPropertyAssignment {
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
if prop := property.AsPropertyAssignment(); prop != nil && nestiaCorePropertyNameText(prop.Name()) == name {
|
|
110
|
+
return true
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
func nestiaCorePropertyNameText(node *shimast.Node) string {
|
|
117
|
+
if node == nil {
|
|
118
|
+
return ""
|
|
119
|
+
}
|
|
120
|
+
switch node.Kind {
|
|
121
|
+
case shimast.KindIdentifier, shimast.KindStringLiteral:
|
|
122
|
+
return node.Text()
|
|
123
|
+
default:
|
|
124
|
+
return ""
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type nestiaCoreMcpRouteParam struct {
|
|
129
|
+
index int
|
|
130
|
+
typ *shimchecker.Type
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
func nestiaCoreMcpRouteParameter(prog *driver.Program, method *shimast.Node) (nestiaCoreMcpRouteParam, error) {
|
|
134
|
+
decl := method.AsMethodDeclaration()
|
|
135
|
+
methodName := nestiaCoreMcpMethodName(method)
|
|
136
|
+
count := 0
|
|
137
|
+
if decl.Parameters != nil {
|
|
138
|
+
count = len(decl.Parameters.Nodes)
|
|
139
|
+
}
|
|
140
|
+
if count != 1 {
|
|
141
|
+
return nestiaCoreMcpRouteParam{}, fmt.Errorf("@McpRoute method %q must declare exactly one @McpRoute.Params() parameter", methodName)
|
|
142
|
+
}
|
|
143
|
+
found := []int{}
|
|
144
|
+
for index, param := range decl.Parameters.Nodes {
|
|
145
|
+
for _, decorator := range param.Decorators() {
|
|
146
|
+
_, segments, ok := nestiaCoreDecoratorCall(prog, decorator)
|
|
147
|
+
if ok && nestiaCoreParameterKind(segments) == "McpRouteParams" {
|
|
148
|
+
found = append(found, index)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if len(found) != 1 || found[0] != 0 {
|
|
153
|
+
return nestiaCoreMcpRouteParam{}, fmt.Errorf("@McpRoute method %q must decorate its only parameter with @McpRoute.Params()", methodName)
|
|
154
|
+
}
|
|
155
|
+
param := decl.Parameters.Nodes[0]
|
|
156
|
+
typ := prog.Checker.GetTypeAtLocation(param)
|
|
157
|
+
if typ == nil {
|
|
158
|
+
return nestiaCoreMcpRouteParam{}, fmt.Errorf("@McpRoute method %q parameter type could not be resolved", methodName)
|
|
159
|
+
}
|
|
160
|
+
return nestiaCoreMcpRouteParam{index: 0, typ: typ}, nil
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
func nestiaCoreMcpMethodName(method *shimast.Node) string {
|
|
164
|
+
if method == nil || method.Kind != shimast.KindMethodDeclaration {
|
|
165
|
+
return ""
|
|
166
|
+
}
|
|
167
|
+
name := method.AsMethodDeclaration().Name()
|
|
168
|
+
return nestiaCorePropertyNameText(name)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
func nestiaCoreAnalyzeMcpParameters(prog *driver.Program, typ *shimchecker.Type) *schemametadata.MetadataSchema {
|
|
172
|
+
collection := schemametadata.NewMetadataCollection()
|
|
173
|
+
result := nativefactories.MetadataFactory.Analyze(nativefactories.MetadataFactory_IProps{
|
|
174
|
+
Checker: prog.Checker,
|
|
175
|
+
Options: nativefactories.MetadataFactory_IOptions{
|
|
176
|
+
Escape: false,
|
|
177
|
+
Constant: true,
|
|
178
|
+
Absorb: true,
|
|
179
|
+
Validate: func(next struct {
|
|
180
|
+
Metadata *schemametadata.MetadataSchema
|
|
181
|
+
Explore nativefactories.MetadataFactory_IExplore
|
|
182
|
+
Top *schemametadata.MetadataSchema
|
|
183
|
+
}) []string {
|
|
184
|
+
return nativellm.LlmParametersProgrammer.Validate(struct {
|
|
185
|
+
Config map[string]any
|
|
186
|
+
Metadata *schemametadata.MetadataSchema
|
|
187
|
+
Explore nativefactories.MetadataFactory_IExplore
|
|
188
|
+
}{
|
|
189
|
+
Config: nestiaCoreMcpLlmConfig(),
|
|
190
|
+
Metadata: next.Metadata,
|
|
191
|
+
Explore: next.Explore,
|
|
192
|
+
})
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
Components: collection,
|
|
196
|
+
Type: typ,
|
|
197
|
+
})
|
|
198
|
+
if result.Success == false {
|
|
199
|
+
panic(nativecontext.TransformerError_from(struct {
|
|
200
|
+
Code string
|
|
201
|
+
Errors []nativecontext.TransformerError_MetadataFactory_IError
|
|
202
|
+
}{
|
|
203
|
+
Code: "@nestia.core.McpRoute.Params",
|
|
204
|
+
Errors: nestiaCoreMetadataErrors(result.Errors),
|
|
205
|
+
}))
|
|
206
|
+
}
|
|
207
|
+
return result.Data
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
func nestiaCoreAnalyzeMcpReturn(prog *driver.Program, typ *shimchecker.Type) {
|
|
211
|
+
if nestiaCoreMcpIsVoidLikeReturn(typ) {
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
collection := schemametadata.NewMetadataCollection()
|
|
215
|
+
result := nativefactories.MetadataFactory.Analyze(nativefactories.MetadataFactory_IProps{
|
|
216
|
+
Checker: prog.Checker,
|
|
217
|
+
Options: nativefactories.MetadataFactory_IOptions{
|
|
218
|
+
Escape: false,
|
|
219
|
+
Constant: true,
|
|
220
|
+
Absorb: true,
|
|
221
|
+
Validate: func(next struct {
|
|
222
|
+
Metadata *schemametadata.MetadataSchema
|
|
223
|
+
Explore nativefactories.MetadataFactory_IExplore
|
|
224
|
+
Top *schemametadata.MetadataSchema
|
|
225
|
+
}) []string {
|
|
226
|
+
errors := []string{}
|
|
227
|
+
if next.Metadata == next.Top {
|
|
228
|
+
errors = append(errors, nestiaCoreValidateMcpObjectMetadata("return", next.Metadata)...)
|
|
229
|
+
}
|
|
230
|
+
errors = append(errors, nativellm.LlmSchemaProgrammer.Validate(struct {
|
|
231
|
+
Config map[string]any
|
|
232
|
+
Metadata *schemametadata.MetadataSchema
|
|
233
|
+
Explore nativefactories.MetadataFactory_IExplore
|
|
234
|
+
}{
|
|
235
|
+
Config: nestiaCoreMcpLlmConfig(),
|
|
236
|
+
Metadata: next.Metadata,
|
|
237
|
+
Explore: next.Explore,
|
|
238
|
+
})...)
|
|
239
|
+
return errors
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
Components: collection,
|
|
243
|
+
Type: typ,
|
|
244
|
+
})
|
|
245
|
+
if result.Success == false {
|
|
246
|
+
panic(nativecontext.TransformerError_from(struct {
|
|
247
|
+
Code string
|
|
248
|
+
Errors []nativecontext.TransformerError_MetadataFactory_IError
|
|
249
|
+
}{
|
|
250
|
+
Code: "@nestia.core.McpRoute",
|
|
251
|
+
Errors: nestiaCoreMetadataErrors(result.Errors),
|
|
252
|
+
}))
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
func nestiaCoreMcpIsVoidLikeReturn(typ *shimchecker.Type) bool {
|
|
257
|
+
if typ == nil {
|
|
258
|
+
return true
|
|
259
|
+
}
|
|
260
|
+
if typ.Flags()&shimchecker.TypeFlagsNever != 0 {
|
|
261
|
+
return true
|
|
262
|
+
}
|
|
263
|
+
if typ.Flags()&(shimchecker.TypeFlagsVoid|shimchecker.TypeFlagsUndefined) != 0 {
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
if typ.Flags()&shimchecker.TypeFlagsUnion == 0 {
|
|
267
|
+
return false
|
|
268
|
+
}
|
|
269
|
+
hasVoid := false
|
|
270
|
+
hasValue := false
|
|
271
|
+
for _, elem := range typ.AsUnionOrIntersectionType().Types() {
|
|
272
|
+
if elem.Flags()&shimchecker.TypeFlagsNever != 0 {
|
|
273
|
+
continue
|
|
274
|
+
}
|
|
275
|
+
if elem.Flags()&(shimchecker.TypeFlagsVoid|shimchecker.TypeFlagsUndefined) != 0 {
|
|
276
|
+
hasVoid = true
|
|
277
|
+
} else {
|
|
278
|
+
hasValue = true
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if hasVoid && hasValue {
|
|
282
|
+
panic(nativecontext.NewTransformerError(nativecontext.TransformerError_IProps{
|
|
283
|
+
Code: "@nestia.core.McpRoute",
|
|
284
|
+
Message: "MCP route return type must not mix void/undefined with structured output.",
|
|
285
|
+
}))
|
|
286
|
+
}
|
|
287
|
+
return hasVoid && !hasValue
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
func nestiaCoreValidateMcpObjectMetadata(label string, metadata *schemametadata.MetadataSchema) []string {
|
|
291
|
+
if metadata == nil {
|
|
292
|
+
return []string{fmt.Sprintf("MCP %s type must be an object type or void.", label)}
|
|
293
|
+
}
|
|
294
|
+
errors := []string{}
|
|
295
|
+
if len(metadata.Objects) == 0 || len(metadata.Objects) != 1 || metadata.Size() > 1 {
|
|
296
|
+
errors = append(errors, fmt.Sprintf("MCP %s type must be a single object type or void.", label))
|
|
297
|
+
}
|
|
298
|
+
for _, object := range metadata.Objects {
|
|
299
|
+
if object == nil || object.Type == nil {
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
for _, property := range object.Type.Properties {
|
|
303
|
+
if property != nil && property.Key != nil && property.Key.IsSoleLiteral() == false {
|
|
304
|
+
errors = append(errors, fmt.Sprintf("MCP %s type must not have dynamic keys.", label))
|
|
305
|
+
break
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if metadata.Nullable {
|
|
310
|
+
errors = append(errors, fmt.Sprintf("MCP %s type must be non-nullable.", label))
|
|
311
|
+
}
|
|
312
|
+
if metadata.IsRequired() == false {
|
|
313
|
+
errors = append(errors, fmt.Sprintf("MCP %s type must be non-undefined.", label))
|
|
314
|
+
}
|
|
315
|
+
return errors
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
func nestiaCoreMcpLlmConfig() map[string]any {
|
|
319
|
+
return map[string]any{"strict": false}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
type nestiaCoreMcpRouteDoc struct {
|
|
323
|
+
Description string
|
|
324
|
+
Title string
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
func nestiaCoreMcpRouteJSDoc(file *shimast.SourceFile, method *shimast.Node) nestiaCoreMcpRouteDoc {
|
|
328
|
+
doc := nestiaCoreMcpRouteDoc{}
|
|
329
|
+
source, ok := SourceFileText(file)
|
|
330
|
+
if ok == false || method == nil {
|
|
331
|
+
return doc
|
|
332
|
+
}
|
|
333
|
+
comment := nestiaCoreMcpLeadingJSDoc(source, method)
|
|
334
|
+
if comment == "" {
|
|
335
|
+
return doc
|
|
336
|
+
}
|
|
337
|
+
description := []string{}
|
|
338
|
+
inTags := false
|
|
339
|
+
for _, line := range strings.Split(comment, "\n") {
|
|
340
|
+
text := strings.TrimSpace(line)
|
|
341
|
+
text = strings.TrimPrefix(text, "*")
|
|
342
|
+
text = strings.TrimSpace(text)
|
|
343
|
+
if text == "" {
|
|
344
|
+
if inTags == false && len(description) != 0 {
|
|
345
|
+
description = append(description, "")
|
|
346
|
+
}
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
if strings.HasPrefix(text, "@") {
|
|
350
|
+
inTags = true
|
|
351
|
+
name, body := nestiaCoreMcpParseJSDocTag(text)
|
|
352
|
+
if name == "title" {
|
|
353
|
+
doc.Title = strings.TrimSpace(body)
|
|
354
|
+
}
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
if inTags == false {
|
|
358
|
+
description = append(description, text)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
doc.Description = strings.TrimSpace(strings.Join(description, "\n"))
|
|
362
|
+
return doc
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
func nestiaCoreMcpLeadingJSDoc(source string, method *shimast.Node) string {
|
|
366
|
+
positions := []int{method.Pos()}
|
|
367
|
+
if decorators := method.Decorators(); len(decorators) != 0 {
|
|
368
|
+
positions = append(positions, decorators[0].Pos())
|
|
369
|
+
}
|
|
370
|
+
for _, pos := range positions {
|
|
371
|
+
if pos < 0 || pos > len(source) {
|
|
372
|
+
continue
|
|
373
|
+
}
|
|
374
|
+
if comment := nestiaCoreMcpJSDocAtOrBefore(source, pos); comment != "" {
|
|
375
|
+
return comment
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return ""
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
func nestiaCoreMcpJSDocAtOrBefore(source string, pos int) string {
|
|
382
|
+
cursor := pos
|
|
383
|
+
for cursor < len(source) && (source[cursor] == ' ' || source[cursor] == '\t' || source[cursor] == '\r' || source[cursor] == '\n') {
|
|
384
|
+
cursor++
|
|
385
|
+
}
|
|
386
|
+
if strings.HasPrefix(source[cursor:], "/**") {
|
|
387
|
+
if end := strings.Index(source[cursor:], "*/"); end >= 0 {
|
|
388
|
+
return source[cursor+3 : cursor+end]
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
left := pos
|
|
392
|
+
for left > 0 && (source[left-1] == ' ' || source[left-1] == '\t' || source[left-1] == '\r' || source[left-1] == '\n') {
|
|
393
|
+
left--
|
|
394
|
+
}
|
|
395
|
+
if left < 2 || source[left-2:left] != "*/" {
|
|
396
|
+
return ""
|
|
397
|
+
}
|
|
398
|
+
start := strings.LastIndex(source[:left-2], "/**")
|
|
399
|
+
if start < 0 {
|
|
400
|
+
return ""
|
|
401
|
+
}
|
|
402
|
+
return source[start+3 : left-2]
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
func nestiaCoreMcpParseJSDocTag(text string) (string, string) {
|
|
406
|
+
text = strings.TrimPrefix(strings.TrimSpace(text), "@")
|
|
407
|
+
name := text
|
|
408
|
+
body := ""
|
|
409
|
+
if index := strings.IndexAny(text, " \t"); index >= 0 {
|
|
410
|
+
name = text[:index]
|
|
411
|
+
body = strings.TrimSpace(text[index+1:])
|
|
412
|
+
}
|
|
413
|
+
return name, body
|
|
414
|
+
}
|