@nestia/core 12.0.0-rc.2 → 12.0.0-rc.3

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 (80) hide show
  1. package/LICENSE +21 -21
  2. package/MIGRATION.md +138 -169
  3. package/lib/decorators/NoTransformConfigurationError.js +5 -5
  4. package/lib/decorators/NoTransformConfigurationError.js.map +1 -1
  5. package/native/cmd/ttsc-nestia/main.go +11 -11
  6. package/native/go.mod +32 -32
  7. package/native/go.sum +54 -54
  8. package/native/plugin/plan.go +102 -102
  9. package/native/transform/ast.go +32 -32
  10. package/native/transform/build.go +388 -380
  11. package/native/transform/cleanup.go +408 -408
  12. package/native/transform/contributor.go +97 -97
  13. package/native/transform/core_querify.go +231 -231
  14. package/native/transform/core_transform.go +1996 -1996
  15. package/native/transform/core_websocket.go +115 -115
  16. package/native/transform/exports.go +13 -13
  17. package/native/transform/mcp_transform.go +414 -414
  18. package/native/transform/node_transform.go +357 -357
  19. package/native/transform/path_rewrite.go +285 -285
  20. package/native/transform/printer.go +244 -244
  21. package/native/transform/rewrite.go +668 -668
  22. package/native/transform/run.go +73 -73
  23. package/native/transform/transform.go +336 -336
  24. package/native/transform/typia_fast.go +375 -352
  25. package/native/transform/typia_replacement.go +24 -24
  26. package/native/transform.cjs +43 -43
  27. package/package.json +9 -9
  28. package/src/adaptors/McpAdaptor.ts +276 -276
  29. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  30. package/src/decorators/DynamicModule.ts +44 -44
  31. package/src/decorators/EncryptedBody.ts +97 -97
  32. package/src/decorators/EncryptedController.ts +40 -40
  33. package/src/decorators/EncryptedModule.ts +98 -98
  34. package/src/decorators/EncryptedRoute.ts +213 -213
  35. package/src/decorators/HumanRoute.ts +21 -21
  36. package/src/decorators/McpRoute.ts +154 -154
  37. package/src/decorators/NoTransformConfigurationError.ts +40 -40
  38. package/src/decorators/PlainBody.ts +76 -76
  39. package/src/decorators/SwaggerCustomizer.ts +97 -97
  40. package/src/decorators/SwaggerExample.ts +180 -180
  41. package/src/decorators/TypedBody.ts +57 -57
  42. package/src/decorators/TypedException.ts +147 -147
  43. package/src/decorators/TypedFormData.ts +187 -187
  44. package/src/decorators/TypedHeaders.ts +66 -66
  45. package/src/decorators/TypedParam.ts +77 -77
  46. package/src/decorators/TypedQuery.ts +234 -234
  47. package/src/decorators/TypedRoute.ts +198 -198
  48. package/src/decorators/WebSocketRoute.ts +242 -242
  49. package/src/decorators/doNotThrowTransformError.ts +5 -5
  50. package/src/decorators/internal/EncryptedConstant.ts +2 -2
  51. package/src/decorators/internal/IMcpRouteReflect.ts +40 -40
  52. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  53. package/src/decorators/internal/get_path_and_querify.ts +94 -94
  54. package/src/decorators/internal/get_path_and_stringify.ts +110 -110
  55. package/src/decorators/internal/get_text_body.ts +16 -16
  56. package/src/decorators/internal/headers_to_object.ts +11 -11
  57. package/src/decorators/internal/is_request_body_undefined.ts +12 -12
  58. package/src/decorators/internal/load_controller.ts +94 -94
  59. package/src/decorators/internal/route_error.ts +43 -43
  60. package/src/decorators/internal/validate_request_body.ts +64 -64
  61. package/src/decorators/internal/validate_request_form_data.ts +67 -67
  62. package/src/decorators/internal/validate_request_headers.ts +76 -76
  63. package/src/decorators/internal/validate_request_query.ts +83 -83
  64. package/src/index.ts +5 -5
  65. package/src/module.ts +25 -25
  66. package/src/options/IRequestBodyValidator.ts +20 -20
  67. package/src/options/IRequestFormDataProps.ts +27 -27
  68. package/src/options/IRequestHeadersValidator.ts +22 -22
  69. package/src/options/IRequestQueryValidator.ts +20 -20
  70. package/src/options/IResponseBodyQuerifier.ts +25 -25
  71. package/src/options/IResponseBodyStringifier.ts +30 -30
  72. package/src/transform.ts +26 -26
  73. package/src/typings/Creator.ts +3 -3
  74. package/src/typings/get-function-location.d.ts +7 -7
  75. package/src/utils/ArrayUtil.ts +7 -7
  76. package/src/utils/ExceptionManager.ts +115 -115
  77. package/src/utils/Singleton.ts +16 -16
  78. package/src/utils/SourceFinder.ts +54 -54
  79. package/src/utils/VersioningStrategy.ts +27 -27
  80. package/README.md +0 -93
@@ -1,414 +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
- }
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
+ }