actionmcp 0.0.0-stage → 0.1.0
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 -0
- package/README.md +74 -2
- package/dist/analyzer/config.d.ts +15 -0
- package/dist/analyzer/config.js +43 -0
- package/dist/analyzer/diagnostics.js +126 -0
- package/dist/analyzer/explicit.js +128 -0
- package/dist/analyzer/index.js +178 -0
- package/dist/analyzer/jsdoc.js +32 -0
- package/dist/analyzer/options.js +65 -0
- package/dist/analyzer/routes.js +81 -0
- package/dist/analyzer/scan.js +280 -0
- package/dist/analyzer/source.js +100 -0
- package/dist/analyzer/types.js +133 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +5 -0
- package/dist/cli/commands.js +107 -0
- package/dist/cli/environment.js +53 -0
- package/dist/cli/index.js +97 -0
- package/dist/cli/output.js +27 -0
- package/dist/cli/project.js +33 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +4 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +84 -0
- package/dist/core/compare.js +6 -0
- package/dist/core/events.js +12 -0
- package/dist/core/execute.js +70 -0
- package/dist/core/formdata.js +15 -0
- package/dist/core/guards.js +8 -0
- package/dist/core/match.js +16 -0
- package/dist/core/names.js +51 -0
- package/dist/core/result.d.ts +18 -0
- package/dist/core/result.js +85 -0
- package/dist/core/schema.d.ts +6 -0
- package/dist/core/schema.js +90 -0
- package/dist/core/types.d.ts +34 -0
- package/dist/core/webmcp.js +62 -0
- package/dist/generator/index.js +36 -0
- package/dist/generator/render.js +91 -0
- package/dist/generator/watch.js +76 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +5 -0
- package/dist/next/NavigateTool.js +40 -0
- package/dist/next/config-store.js +12 -0
- package/dist/next/redirect.js +15 -0
- package/dist/next/routes.d.ts +3 -0
- package/dist/next/routes.js +57 -0
- package/dist/react/AgentTools.d.ts +14 -0
- package/dist/react/AgentTools.js +35 -0
- package/dist/react/Confirm.d.ts +3 -0
- package/dist/react/Confirm.js +99 -0
- package/dist/react/Devtools.d.ts +2 -0
- package/dist/react/Devtools.js +13 -0
- package/dist/react/DevtoolsPanel.js +223 -0
- package/dist/react/Provider.d.ts +6 -0
- package/dist/react/Provider.js +21 -0
- package/dist/react/Registrar.js +12 -0
- package/dist/react/context.d.ts +14 -0
- package/dist/react/context.js +5 -0
- package/dist/react/useAgentTools.js +103 -0
- package/dist/registry.d.ts +4 -0
- package/dist/registry.js +4 -0
- package/dist/server/agent.d.ts +43 -0
- package/dist/server/agent.js +34 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +4 -0
- package/dist/server/registry.d.ts +53 -0
- package/dist/server/registry.js +93 -0
- package/dist/server/resolve.js +68 -0
- package/package.json +96 -4
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { compareText } from "../core/compare.js";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parseModule, readObjectLiteral, staticString } from "./source.js";
|
|
5
|
+
// src/analyzer/routes.ts
|
|
6
|
+
const PAGE_FILE = /^page\.(?:tsx|ts|jsx|js|mdx)$/;
|
|
7
|
+
const ROUTE_GROUP = /^\([^.].*\)$/;
|
|
8
|
+
const INTERCEPTING_ROUTE = /^\(\.{1,3}\)/;
|
|
9
|
+
const OPTIONAL_CATCH_ALL = /^\[\[\.\.\.(.+)\]\]$/;
|
|
10
|
+
const CATCH_ALL = /^\[\.\.\.(.+)\]$/;
|
|
11
|
+
const DYNAMIC = /^\[(.+)\]$/;
|
|
12
|
+
const APP_DIRECTORIES = ["app", "src/app"];
|
|
13
|
+
function findAppDirectory(cwd) {
|
|
14
|
+
return APP_DIRECTORIES.map((directory) => join(cwd, directory)).find((directory) => existsSync(directory));
|
|
15
|
+
}
|
|
16
|
+
function scanRoutes(cwd) {
|
|
17
|
+
const appDirectory = findAppDirectory(cwd);
|
|
18
|
+
if (!appDirectory) return [];
|
|
19
|
+
const routes = /* @__PURE__ */ new Map();
|
|
20
|
+
visit(appDirectory, [], routes);
|
|
21
|
+
return [...routes.values()].toSorted((left, right) => compareText(left.pattern, right.pattern));
|
|
22
|
+
}
|
|
23
|
+
function visit(directory, segments, routes) {
|
|
24
|
+
const entries = readdirSync(directory, { withFileTypes: true });
|
|
25
|
+
const page = entries.find((entry) => entry.isFile() && PAGE_FILE.test(entry.name));
|
|
26
|
+
if (page) {
|
|
27
|
+
const route = toRoute(segments, join(directory, page.name));
|
|
28
|
+
if (!routes.has(route.pattern)) routes.set(route.pattern, route);
|
|
29
|
+
}
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
if (!entry.isDirectory() || isExcluded(entry.name, segments.length === 0)) continue;
|
|
32
|
+
visit(join(directory, entry.name), ROUTE_GROUP.test(entry.name) ? segments : [...segments, entry.name], routes);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function isExcluded(name, isRoot) {
|
|
36
|
+
return name.startsWith("_") || name.startsWith("@") || INTERCEPTING_ROUTE.test(name) || name === "node_modules" || isRoot && name === "api";
|
|
37
|
+
}
|
|
38
|
+
function toRoute(segments, pageFile) {
|
|
39
|
+
const route = {
|
|
40
|
+
pattern: `/${segments.join("/")}`,
|
|
41
|
+
params: []
|
|
42
|
+
};
|
|
43
|
+
for (const segment of segments) {
|
|
44
|
+
const optionalCatchAll = OPTIONAL_CATCH_ALL.exec(segment)?.[1];
|
|
45
|
+
const catchAll = CATCH_ALL.exec(segment)?.[1];
|
|
46
|
+
const dynamic = DYNAMIC.exec(segment)?.[1];
|
|
47
|
+
if (optionalCatchAll !== void 0) {
|
|
48
|
+
route.params.push(optionalCatchAll);
|
|
49
|
+
route.optionalCatchAll = optionalCatchAll;
|
|
50
|
+
} else if (catchAll !== void 0) {
|
|
51
|
+
route.params.push(catchAll);
|
|
52
|
+
route.catchAll = catchAll;
|
|
53
|
+
} else if (dynamic !== void 0) route.params.push(dynamic);
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
...route,
|
|
57
|
+
...readPageMetadata(pageFile)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function readPageMetadata(pageFile) {
|
|
61
|
+
if (pageFile.endsWith(".mdx")) return {};
|
|
62
|
+
const code = readFileSync(pageFile, "utf8");
|
|
63
|
+
if (!code.includes("generateStaticParams") && !code.includes("metadata")) return {};
|
|
64
|
+
const metadata = {};
|
|
65
|
+
for (const statement of parseModule(pageFile, code).program.body) {
|
|
66
|
+
if (statement.type !== "ExportNamedDeclaration" || !statement.declaration) continue;
|
|
67
|
+
const { declaration } = statement;
|
|
68
|
+
if (declaration.type === "FunctionDeclaration" && declaration.id?.name === "generateStaticParams") metadata.staticParams = true;
|
|
69
|
+
if (declaration.type !== "VariableDeclaration") continue;
|
|
70
|
+
for (const declarator of declaration.declarations) {
|
|
71
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
72
|
+
if (declarator.id.name === "generateStaticParams") metadata.staticParams = true;
|
|
73
|
+
if (declarator.id.name === "metadata" && declarator.init?.type === "ObjectExpression") {
|
|
74
|
+
const title = staticString(readObjectLiteral(declarator.init).properties.get("title"));
|
|
75
|
+
if (title) metadata.title = title;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return metadata;
|
|
80
|
+
}
|
|
81
|
+
export { findAppDirectory, scanRoutes };
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { resolveAnnotations } from "../server/registry.js";
|
|
2
|
+
import { createDiagnostic } from "./diagnostics.js";
|
|
3
|
+
import { formDataAdapter, parseAgentTag } from "./jsdoc.js";
|
|
4
|
+
import { readToolOptions } from "./options.js";
|
|
5
|
+
import { collectCalleeNames, collectObjectKeys, collectReferencedNames, hasUseServerDirective, walk } from "./source.js";
|
|
6
|
+
// src/analyzer/scan.ts
|
|
7
|
+
const SERVER_ENTRY = "actionmcp/server";
|
|
8
|
+
const COMPONENT_ENTRIES = ["actionmcp", "actionmcp/client"];
|
|
9
|
+
const MIN_DESCRIPTION_LENGTH = 20;
|
|
10
|
+
const VALIDATION_CALL = /^(?:parse|safeParse|parseAsync|safeParseAsync|validate|assert\w*)$/;
|
|
11
|
+
const FREE_FORM_STRING = /\bstring\s*\(|['"`]string\b/;
|
|
12
|
+
const PERSONAL_FIELDS = /* @__PURE__ */ new Set([
|
|
13
|
+
"email",
|
|
14
|
+
"phone",
|
|
15
|
+
"address",
|
|
16
|
+
"birthdate",
|
|
17
|
+
"birthday",
|
|
18
|
+
"dob",
|
|
19
|
+
"ssn",
|
|
20
|
+
"passport"
|
|
21
|
+
]);
|
|
22
|
+
function mayContainTools(code) {
|
|
23
|
+
return code.includes(SERVER_ENTRY) || code.includes("@agent") || code.includes("AgentTools");
|
|
24
|
+
}
|
|
25
|
+
function scanModule(module, options) {
|
|
26
|
+
const context = {
|
|
27
|
+
module,
|
|
28
|
+
options,
|
|
29
|
+
useServer: hasUseServerDirective(module.program),
|
|
30
|
+
agentNames: /* @__PURE__ */ new Set(),
|
|
31
|
+
namespaces: /* @__PURE__ */ new Set(),
|
|
32
|
+
componentNames: /* @__PURE__ */ new Set(),
|
|
33
|
+
declarations: /* @__PURE__ */ new Map(),
|
|
34
|
+
exports: /* @__PURE__ */ new Map(),
|
|
35
|
+
scan: {
|
|
36
|
+
tools: [],
|
|
37
|
+
typeRequests: [],
|
|
38
|
+
diagnostics: [],
|
|
39
|
+
usesNavigate: false
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
indexModule(context);
|
|
43
|
+
scanAgentCalls(context);
|
|
44
|
+
if (options.jsdoc) scanAgentTags(context);
|
|
45
|
+
context.scan.usesNavigate = usesNavigate(context);
|
|
46
|
+
return context.scan;
|
|
47
|
+
}
|
|
48
|
+
function indexModule(context) {
|
|
49
|
+
for (const statement of context.module.program.body) if (statement.type === "ImportDeclaration") indexImport(context, statement);
|
|
50
|
+
else if (statement.type === "ExportNamedDeclaration") {
|
|
51
|
+
if (statement.declaration) for (const name of indexDeclaration(context, statement.declaration)) context.exports.set(name, name);
|
|
52
|
+
else if (!statement.source) {
|
|
53
|
+
for (const specifier of statement.specifiers) if (specifier.local.type === "Identifier" && specifier.exported.type === "Identifier") context.exports.set(specifier.local.name, specifier.exported.name);
|
|
54
|
+
}
|
|
55
|
+
} else if (statement.type === "ExportDefaultDeclaration") {
|
|
56
|
+
if (statement.declaration.type === "Identifier") context.exports.set(statement.declaration.name, "default");
|
|
57
|
+
} else indexDeclaration(context, statement);
|
|
58
|
+
}
|
|
59
|
+
function indexImport(context, statement) {
|
|
60
|
+
const source = statement.source.value;
|
|
61
|
+
for (const specifier of statement.specifiers) if (source === SERVER_ENTRY) {
|
|
62
|
+
if (specifier.type === "ImportNamespaceSpecifier") context.namespaces.add(specifier.local.name);
|
|
63
|
+
else if (specifier.type === "ImportSpecifier" && importedName(specifier.imported) === "agent") context.agentNames.add(specifier.local.name);
|
|
64
|
+
} else if (COMPONENT_ENTRIES.includes(source)) {
|
|
65
|
+
if (specifier.type === "ImportSpecifier" && importedName(specifier.imported) === "AgentTools") context.componentNames.add(specifier.local.name);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function importedName(node) {
|
|
69
|
+
if (node.type === "Identifier") return node.name;
|
|
70
|
+
return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
71
|
+
}
|
|
72
|
+
function indexDeclaration(context, node) {
|
|
73
|
+
if (node.type === "FunctionDeclaration" && node.id) {
|
|
74
|
+
context.declarations.set(node.id.name, node);
|
|
75
|
+
return [node.id.name];
|
|
76
|
+
}
|
|
77
|
+
if (node.type !== "VariableDeclaration") return [];
|
|
78
|
+
const names = [];
|
|
79
|
+
for (const declarator of node.declarations) if (declarator.id.type === "Identifier" && declarator.init) {
|
|
80
|
+
context.declarations.set(declarator.id.name, declarator.init);
|
|
81
|
+
names.push(declarator.id.name);
|
|
82
|
+
}
|
|
83
|
+
return names;
|
|
84
|
+
}
|
|
85
|
+
function isAgentCall(context, node) {
|
|
86
|
+
if (node?.type !== "CallExpression") return false;
|
|
87
|
+
const { callee } = node;
|
|
88
|
+
if (callee.type === "Identifier") return context.agentNames.has(callee.name);
|
|
89
|
+
return callee.type === "MemberExpression" && callee.object.type === "Identifier" && context.namespaces.has(callee.object.name) && callee.property.type === "Identifier" && callee.property.name === "agent";
|
|
90
|
+
}
|
|
91
|
+
function scanAgentCalls(context) {
|
|
92
|
+
for (const [localName, node] of context.declarations) if (isAgentCall(context, node)) addAgentTool(context, node, context.exports.get(localName));
|
|
93
|
+
for (const statement of context.module.program.body) if (statement.type === "ExportDefaultDeclaration" && isAgentCall(context, statement.declaration)) addAgentTool(context, statement.declaration, "default");
|
|
94
|
+
}
|
|
95
|
+
function addAgentTool(context, call, exportName) {
|
|
96
|
+
const { module } = context;
|
|
97
|
+
const reading = readToolOptions(call.arguments[0], call.start);
|
|
98
|
+
const annotations = resolveAnnotations(reading.readOnly, reading.consequential, reading.untrustedOutput);
|
|
99
|
+
const tool = {
|
|
100
|
+
name: reading.name ?? exportName ?? "",
|
|
101
|
+
kind: "agent",
|
|
102
|
+
source: {
|
|
103
|
+
file: module.file,
|
|
104
|
+
export: exportName ?? "",
|
|
105
|
+
line: module.locate(call.start).line
|
|
106
|
+
},
|
|
107
|
+
description: reading.description ?? "",
|
|
108
|
+
schemaSource: reading.input ? "standard-schema" : "none",
|
|
109
|
+
annotations,
|
|
110
|
+
confirm: reading.confirm ?? "consequential",
|
|
111
|
+
validated: true,
|
|
112
|
+
diagnostics: [],
|
|
113
|
+
...reading.input ? { inputSource: module.text(reading.input) } : {}
|
|
114
|
+
};
|
|
115
|
+
const report = reporter(context, tool);
|
|
116
|
+
if (exportName === void 0) report("AT004", call.start, "the agent() call is not exported");
|
|
117
|
+
if (!context.useServer) report("AT004", call.start, "the file has no 'use server' directive");
|
|
118
|
+
if (exportName === "default" && reading.name === void 0) report("AT001", call.start, "a default export needs the `name` option");
|
|
119
|
+
for (const issue of reading.issues) report(issue.code, issue.offset, issue.detail);
|
|
120
|
+
checkDescription(tool, report, call.start);
|
|
121
|
+
if (tool.confirm === "never" && annotations.consequentialHint) report("AT103", call.start);
|
|
122
|
+
const handler = resolveFunction(context, call.arguments[1]);
|
|
123
|
+
if (handler) {
|
|
124
|
+
checkAuthentication(context, handler, report);
|
|
125
|
+
const input = resolveReference(context, reading.input);
|
|
126
|
+
if (input) checkPersonalFields(collectObjectKeys(input), handler, report, input.start);
|
|
127
|
+
}
|
|
128
|
+
const output = resolveReference(context, reading.output);
|
|
129
|
+
if (output && !annotations.untrustedContentHint && FREE_FORM_STRING.test(module.text(output))) report("AT106", output.start);
|
|
130
|
+
context.scan.tools.push(tool);
|
|
131
|
+
}
|
|
132
|
+
function scanAgentTags(context) {
|
|
133
|
+
for (const comment of context.module.comments) {
|
|
134
|
+
if (comment.type !== "Block" || !comment.value.startsWith("*")) continue;
|
|
135
|
+
const tag = parseAgentTag(comment.value);
|
|
136
|
+
if (tag) addJsdocTool(context, comment, tag);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function addJsdocTool(context, comment, tag) {
|
|
140
|
+
const { module } = context;
|
|
141
|
+
const target = documentedFunction(context, comment);
|
|
142
|
+
const exportName = target?.exportName;
|
|
143
|
+
const annotations = resolveAnnotations(tag.readOnly || void 0, tag.consequential || void 0, tag.untrustedOutput || void 0);
|
|
144
|
+
const tool = {
|
|
145
|
+
name: tag.name ?? (exportName === "default" ? target?.localName : exportName) ?? target?.localName ?? "",
|
|
146
|
+
kind: "jsdoc",
|
|
147
|
+
source: {
|
|
148
|
+
file: module.file,
|
|
149
|
+
export: exportName ?? "",
|
|
150
|
+
line: module.locate(target?.node.start ?? comment.start).line
|
|
151
|
+
},
|
|
152
|
+
description: tag.description,
|
|
153
|
+
schemaSource: "none",
|
|
154
|
+
annotations,
|
|
155
|
+
confirm: "consequential",
|
|
156
|
+
validated: false,
|
|
157
|
+
diagnostics: []
|
|
158
|
+
};
|
|
159
|
+
const report = reporter(context, tool);
|
|
160
|
+
const offset = target?.node.start ?? comment.start;
|
|
161
|
+
if (!target) report("AT004", offset, "@agent is not attached to a function");
|
|
162
|
+
else if (exportName === void 0) report("AT004", offset, "the function is not exported");
|
|
163
|
+
if (!context.useServer) report("AT004", offset, "the file has no 'use server' directive");
|
|
164
|
+
for (const modifier of tag.unknownModifiers) report("AT006", comment.start, `"${modifier}"`);
|
|
165
|
+
if (tag.description === "") report("AT003", comment.start, "the JSDoc comment has no description");
|
|
166
|
+
checkDescription(tool, report, comment.start);
|
|
167
|
+
if (target && exportName !== void 0) {
|
|
168
|
+
const { params } = target.node;
|
|
169
|
+
const [first] = params;
|
|
170
|
+
if (tag.formData || params.some(isFormDataParameter)) {
|
|
171
|
+
tool.adapter = formDataAdapter(params.length);
|
|
172
|
+
report("AT101", offset, "a FormData parameter has no inferable schema; use defineAgentTools() with an input schema");
|
|
173
|
+
} else if (first && !hasTypeAnnotation(first)) report("AT101", first.start, "the first parameter has no type annotation");
|
|
174
|
+
else if (first) {
|
|
175
|
+
tool.schemaSource = "typescript";
|
|
176
|
+
context.scan.typeRequests.push({
|
|
177
|
+
tool,
|
|
178
|
+
file: module.file,
|
|
179
|
+
parameterOffset: first.start
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (first && target.node.body) {
|
|
183
|
+
if (![...collectCalleeNames(target.node.body)].some((name) => VALIDATION_CALL.test(name))) report("AT102", offset);
|
|
184
|
+
}
|
|
185
|
+
checkAuthentication(context, target.node, report);
|
|
186
|
+
if (first) checkPersonalFields(collectObjectKeys(first), target.node, report, first.start);
|
|
187
|
+
}
|
|
188
|
+
context.scan.tools.push(tool);
|
|
189
|
+
}
|
|
190
|
+
function documentedFunction(context, comment) {
|
|
191
|
+
const { module } = context;
|
|
192
|
+
const statement = module.program.body.find((candidate) => candidate.start >= comment.end && module.code.slice(comment.end, candidate.start).trim() === "");
|
|
193
|
+
if (!statement) return void 0;
|
|
194
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
195
|
+
const node = asFunction(statement.declaration);
|
|
196
|
+
return node ? {
|
|
197
|
+
node,
|
|
198
|
+
localName: functionName(node),
|
|
199
|
+
exportName: "default"
|
|
200
|
+
} : void 0;
|
|
201
|
+
}
|
|
202
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
203
|
+
if (!declaration) return void 0;
|
|
204
|
+
const localName = declaredName(declaration);
|
|
205
|
+
const node = asFunction(declaration.type === "VariableDeclaration" ? declaration.declarations[0]?.init : declaration);
|
|
206
|
+
if (!node || localName === void 0) return void 0;
|
|
207
|
+
return {
|
|
208
|
+
node,
|
|
209
|
+
localName,
|
|
210
|
+
exportName: context.exports.get(localName)
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function declaredName(node) {
|
|
214
|
+
if (node.type === "FunctionDeclaration") return node.id?.name;
|
|
215
|
+
if (node.type !== "VariableDeclaration") return void 0;
|
|
216
|
+
const id = node.declarations[0]?.id;
|
|
217
|
+
return id?.type === "Identifier" ? id.name : void 0;
|
|
218
|
+
}
|
|
219
|
+
function functionName(node) {
|
|
220
|
+
return node.type === "ArrowFunctionExpression" ? void 0 : node.id?.name;
|
|
221
|
+
}
|
|
222
|
+
function asFunction(node) {
|
|
223
|
+
if (!node) return void 0;
|
|
224
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" ? node : void 0;
|
|
225
|
+
}
|
|
226
|
+
function resolveReference(context, node) {
|
|
227
|
+
return node?.type === "Identifier" ? context.declarations.get(node.name) : node;
|
|
228
|
+
}
|
|
229
|
+
function resolveFunction(context, node) {
|
|
230
|
+
return asFunction(resolveReference(context, node));
|
|
231
|
+
}
|
|
232
|
+
function isFormDataParameter(parameter) {
|
|
233
|
+
const annotation = typeAnnotationOf(parameter);
|
|
234
|
+
return annotation?.type === "TSTypeReference" && annotation.typeName.type === "Identifier" && annotation.typeName.name === "FormData";
|
|
235
|
+
}
|
|
236
|
+
function hasTypeAnnotation(parameter) {
|
|
237
|
+
return typeAnnotationOf(parameter) !== void 0;
|
|
238
|
+
}
|
|
239
|
+
function typeAnnotationOf(parameter) {
|
|
240
|
+
return (parameter.type === "AssignmentPattern" ? parameter.left : parameter).typeAnnotation?.typeAnnotation ?? void 0;
|
|
241
|
+
}
|
|
242
|
+
function reporter(context, tool) {
|
|
243
|
+
return (code, offset, detail) => {
|
|
244
|
+
context.scan.diagnostics.push(createDiagnostic(code, context.module.locate(offset), detail, tool.name || void 0));
|
|
245
|
+
if (!tool.diagnostics.includes(code)) tool.diagnostics.push(code);
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function checkDescription(tool, report, offset) {
|
|
249
|
+
const length = tool.description.trim().length;
|
|
250
|
+
if (length > 0 && length < MIN_DESCRIPTION_LENGTH) report("AT108", offset, `"${tool.description.trim()}"`);
|
|
251
|
+
}
|
|
252
|
+
function checkAuthentication(context, handler, report) {
|
|
253
|
+
if (!handler.body) return;
|
|
254
|
+
const calls = collectCalleeNames(handler.body);
|
|
255
|
+
if (!context.options.auth.some((name) => calls.has(name))) report("AT105", handler.start);
|
|
256
|
+
}
|
|
257
|
+
function checkPersonalFields(keys, handler, report, offset) {
|
|
258
|
+
if (!handler.body) return;
|
|
259
|
+
const referenced = collectReferencedNames(handler.body);
|
|
260
|
+
const unused = [...keys].filter((key) => isPersonalField(key) && !referenced.has(key));
|
|
261
|
+
if (unused.length > 0) report("AT107", offset, unused.join(", "));
|
|
262
|
+
}
|
|
263
|
+
function isPersonalField(key) {
|
|
264
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1 $2").split(/[\s_-]+/).some((word) => PERSONAL_FIELDS.has(word.toLowerCase()));
|
|
265
|
+
}
|
|
266
|
+
function usesNavigate(context) {
|
|
267
|
+
if (context.componentNames.size === 0) return false;
|
|
268
|
+
let found = false;
|
|
269
|
+
walk(context.module.program, (node) => {
|
|
270
|
+
if (node.type !== "JSXOpeningElement") return;
|
|
271
|
+
if (node.name.type !== "JSXIdentifier" || !context.componentNames.has(node.name.name)) return;
|
|
272
|
+
for (const attribute of node.attributes) {
|
|
273
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name !== "navigate") continue;
|
|
274
|
+
const value = attribute.value;
|
|
275
|
+
if (!(value?.type === "JSXExpressionContainer" && value.expression.type === "Literal" && value.expression.value === false)) found = true;
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
return found;
|
|
279
|
+
}
|
|
280
|
+
export { mayContainTools, scanModule };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { parseSync, visitorKeys } from "oxc-parser";
|
|
2
|
+
// src/analyzer/source.ts
|
|
3
|
+
function parseModule(file, code) {
|
|
4
|
+
const { program, comments } = parseSync(file, code);
|
|
5
|
+
const lineStarts = [0];
|
|
6
|
+
for (let index = code.indexOf("\n"); index !== -1; index = code.indexOf("\n", index + 1)) lineStarts.push(index + 1);
|
|
7
|
+
return {
|
|
8
|
+
file,
|
|
9
|
+
code,
|
|
10
|
+
program,
|
|
11
|
+
comments,
|
|
12
|
+
locate: (offset) => {
|
|
13
|
+
let low = 0;
|
|
14
|
+
let high = lineStarts.length - 1;
|
|
15
|
+
while (low < high) {
|
|
16
|
+
const middle = Math.ceil((low + high) / 2);
|
|
17
|
+
if ((lineStarts[middle] ?? 0) <= offset) low = middle;
|
|
18
|
+
else high = middle - 1;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
file,
|
|
22
|
+
line: low + 1,
|
|
23
|
+
column: offset - (lineStarts[low] ?? 0) + 1
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
text: (node) => code.slice(node.start, node.end)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function walk(root, visit) {
|
|
30
|
+
visit(root);
|
|
31
|
+
for (const key of visitorKeys[root.type] ?? []) {
|
|
32
|
+
const child = root[key];
|
|
33
|
+
for (const candidate of Array.isArray(child) ? child : [child]) if (isNode(candidate)) walk(candidate, visit);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function isNode(value) {
|
|
37
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
38
|
+
}
|
|
39
|
+
function hasUseServerDirective(program) {
|
|
40
|
+
return program.body.some((statement) => statement.type === "ExpressionStatement" && statement.directive === "use server");
|
|
41
|
+
}
|
|
42
|
+
function staticString(node) {
|
|
43
|
+
if (!node) return void 0;
|
|
44
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
45
|
+
if (node.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis.map((quasi) => quasi.value.cooked ?? "").join("");
|
|
46
|
+
}
|
|
47
|
+
function staticBoolean(node) {
|
|
48
|
+
return node?.type === "Literal" && typeof node.value === "boolean" ? node.value : void 0;
|
|
49
|
+
}
|
|
50
|
+
function propertyName(node, computed) {
|
|
51
|
+
if (!computed && node.type === "Identifier") return node.name;
|
|
52
|
+
return staticString(node);
|
|
53
|
+
}
|
|
54
|
+
function readObjectLiteral(node) {
|
|
55
|
+
const properties = /* @__PURE__ */ new Map();
|
|
56
|
+
let hasSpread = false;
|
|
57
|
+
for (const property of node.properties) {
|
|
58
|
+
if (property.type === "SpreadElement") {
|
|
59
|
+
hasSpread = true;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const name = propertyName(property.key, property.computed);
|
|
63
|
+
if (name !== void 0) properties.set(name, property.value);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
properties,
|
|
67
|
+
hasSpread
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function calleeName(node) {
|
|
71
|
+
if (node.type === "Identifier") return node.name;
|
|
72
|
+
if (node.type === "MemberExpression") return propertyName(node.property, node.computed);
|
|
73
|
+
}
|
|
74
|
+
function collectCalleeNames(root) {
|
|
75
|
+
const names = /* @__PURE__ */ new Set();
|
|
76
|
+
walk(root, (node) => {
|
|
77
|
+
if (node.type !== "CallExpression") return;
|
|
78
|
+
const name = calleeName(node.callee);
|
|
79
|
+
if (name !== void 0) names.add(name);
|
|
80
|
+
});
|
|
81
|
+
return names;
|
|
82
|
+
}
|
|
83
|
+
function collectReferencedNames(root) {
|
|
84
|
+
const names = /* @__PURE__ */ new Set();
|
|
85
|
+
walk(root, (node) => {
|
|
86
|
+
if (node.type === "Identifier") names.add(node.name);
|
|
87
|
+
else if (node.type === "Literal" && typeof node.value === "string") names.add(node.value);
|
|
88
|
+
});
|
|
89
|
+
return names;
|
|
90
|
+
}
|
|
91
|
+
function collectObjectKeys(root) {
|
|
92
|
+
const keys = /* @__PURE__ */ new Set();
|
|
93
|
+
walk(root, (node) => {
|
|
94
|
+
if (node.type !== "Property" && node.type !== "TSPropertySignature") return;
|
|
95
|
+
const name = propertyName(node.key, node.computed);
|
|
96
|
+
if (name !== void 0) keys.add(name);
|
|
97
|
+
});
|
|
98
|
+
return keys;
|
|
99
|
+
}
|
|
100
|
+
export { calleeName, collectCalleeNames, collectObjectKeys, collectReferencedNames, hasUseServerDirective, parseModule, propertyName, readObjectLiteral, staticBoolean, staticString, walk };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { compareText } from "../core/compare.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { isRecord } from "../core/guards.js";
|
|
6
|
+
import { sanitizeJsonSchema } from "../core/schema.js";
|
|
7
|
+
// src/analyzer/types.ts
|
|
8
|
+
const CACHE_VERSION = 1;
|
|
9
|
+
async function inferParameterSchemas(targets, options) {
|
|
10
|
+
if (targets.length === 0) return [];
|
|
11
|
+
const cache = readCache(options.cacheFile);
|
|
12
|
+
const tsconfig = findTsconfig(options.cwd);
|
|
13
|
+
const tsconfigText = tsconfig ? readFileSync(tsconfig, "utf8") : "";
|
|
14
|
+
const hashes = targets.map((target) => hashTarget(target, tsconfigText));
|
|
15
|
+
const missing = targets.filter((target, index) => cache.get(cacheKey(target))?.hash !== hashes[index]);
|
|
16
|
+
if (missing.length > 0) {
|
|
17
|
+
const inferred = await inferWithTypeScript(missing, options.cwd, tsconfig);
|
|
18
|
+
missing.forEach((target, index) => {
|
|
19
|
+
const inference = inferred[index];
|
|
20
|
+
const hash = hashes[targets.indexOf(target)];
|
|
21
|
+
if (inference && hash !== void 0) cache.set(cacheKey(target), {
|
|
22
|
+
hash,
|
|
23
|
+
inference
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
writeCache(options.cacheFile, cache, targets);
|
|
27
|
+
}
|
|
28
|
+
return targets.map((target) => cache.get(cacheKey(target))?.inference ?? {
|
|
29
|
+
ok: false,
|
|
30
|
+
reason: "The type could not be read."
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function normalizeTypeSchema(schema) {
|
|
34
|
+
const { $schema: _dialect, definitions, ...rest } = schema;
|
|
35
|
+
const renamed = renameDefinitionReferences(isRecord(definitions) && Object.keys(definitions).length > 0 ? {
|
|
36
|
+
...rest,
|
|
37
|
+
$defs: definitions
|
|
38
|
+
} : rest);
|
|
39
|
+
const sanitized = sanitizeJsonSchema(renamed);
|
|
40
|
+
if (sanitized.type !== "object") return {
|
|
41
|
+
ok: false,
|
|
42
|
+
reason: "the first parameter is not an object type"
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
ok: true,
|
|
46
|
+
schema: sanitized
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function renameDefinitionReferences(node) {
|
|
50
|
+
if (Array.isArray(node)) return node.map(renameDefinitionReferences);
|
|
51
|
+
if (!isRecord(node)) return node;
|
|
52
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, key === "$ref" && typeof value === "string" ? value.replace(/^#\/definitions\//, "#/$defs/") : renameDefinitionReferences(value)]));
|
|
53
|
+
}
|
|
54
|
+
async function inferWithTypeScript(targets, cwd, tsconfig) {
|
|
55
|
+
const { default: typescript } = await import("typescript");
|
|
56
|
+
const { SchemaGenerator, createFormatter, createParser, DEFAULT_CONFIG } = await import("ts-json-schema-generator");
|
|
57
|
+
const compilerOptions = {
|
|
58
|
+
...readCompilerOptions(typescript, cwd, tsconfig),
|
|
59
|
+
noEmit: true,
|
|
60
|
+
skipLibCheck: true
|
|
61
|
+
};
|
|
62
|
+
const program = typescript.createProgram([...new Set(targets.map((target) => target.file))], compilerOptions);
|
|
63
|
+
const config = {
|
|
64
|
+
...DEFAULT_CONFIG,
|
|
65
|
+
expose: "none",
|
|
66
|
+
topRef: false,
|
|
67
|
+
jsDoc: "extended",
|
|
68
|
+
additionalProperties: true,
|
|
69
|
+
skipTypeCheck: true
|
|
70
|
+
};
|
|
71
|
+
const generator = new SchemaGenerator(program, createParser(program, config), createFormatter(config), config);
|
|
72
|
+
return targets.map((target) => {
|
|
73
|
+
const sourceFile = program.getSourceFile(target.file);
|
|
74
|
+
const parameter = sourceFile && findParameter(typescript, sourceFile, target.parameterOffset);
|
|
75
|
+
if (!parameter?.type) return {
|
|
76
|
+
ok: false,
|
|
77
|
+
reason: "the first parameter has no type annotation"
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
return normalizeTypeSchema(generator.createSchemaFromNodes([parameter.type]));
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
reason: error instanceof Error ? error.message.split("\n")[0] ?? error.name : String(error)
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function findParameter(typescript, sourceFile, offset) {
|
|
90
|
+
const search = (node) => {
|
|
91
|
+
if (offset < node.getFullStart() || offset >= node.getEnd()) return void 0;
|
|
92
|
+
if (typescript.isParameter(node) && node.getStart(sourceFile) === offset) return node;
|
|
93
|
+
return typescript.forEachChild(node, search);
|
|
94
|
+
};
|
|
95
|
+
return search(sourceFile);
|
|
96
|
+
}
|
|
97
|
+
function readCompilerOptions(typescript, cwd, tsconfig) {
|
|
98
|
+
if (!tsconfig) return {
|
|
99
|
+
strict: true,
|
|
100
|
+
target: typescript.ScriptTarget.ES2022,
|
|
101
|
+
module: typescript.ModuleKind.ESNext,
|
|
102
|
+
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
|
103
|
+
jsx: typescript.JsxEmit.Preserve
|
|
104
|
+
};
|
|
105
|
+
const { config } = typescript.readConfigFile(tsconfig, typescript.sys.readFile);
|
|
106
|
+
return typescript.parseJsonConfigFileContent(config ?? {}, typescript.sys, cwd).options;
|
|
107
|
+
}
|
|
108
|
+
function findTsconfig(cwd) {
|
|
109
|
+
const file = join(cwd, "tsconfig.json");
|
|
110
|
+
return existsSync(file) ? file : void 0;
|
|
111
|
+
}
|
|
112
|
+
function cacheKey(target) {
|
|
113
|
+
return `${target.file}#${target.parameterOffset}`;
|
|
114
|
+
}
|
|
115
|
+
function hashTarget(target, tsconfigText) {
|
|
116
|
+
return createHash("sha256").update(String(CACHE_VERSION)).update(readFileSync(target.file)).update(tsconfigText).update(String(target.parameterOffset)).digest("hex");
|
|
117
|
+
}
|
|
118
|
+
function readCache(cacheFile) {
|
|
119
|
+
if (!cacheFile || !existsSync(cacheFile)) return /* @__PURE__ */ new Map();
|
|
120
|
+
try {
|
|
121
|
+
return new Map(Object.entries(JSON.parse(readFileSync(cacheFile, "utf8"))));
|
|
122
|
+
} catch {
|
|
123
|
+
return /* @__PURE__ */ new Map();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function writeCache(cacheFile, cache, targets) {
|
|
127
|
+
if (!cacheFile) return;
|
|
128
|
+
const live = new Set(targets.map(cacheKey));
|
|
129
|
+
const entries = [...cache].filter(([key]) => live.has(key)).toSorted(([left], [right]) => compareText(left, right));
|
|
130
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
131
|
+
writeFileSync(cacheFile, `${JSON.stringify(Object.fromEntries(entries), null, 2)}\n`);
|
|
132
|
+
}
|
|
133
|
+
export { inferParameterSchemas, normalizeTypeSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|