actionmcp 0.0.0-stage → 0.2.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 +136 -0
- package/dist/analyzer/explicit.js +128 -0
- package/dist/analyzer/index.js +191 -0
- package/dist/analyzer/jsdoc.js +32 -0
- package/dist/analyzer/options.js +65 -0
- package/dist/analyzer/routes.js +105 -0
- package/dist/analyzer/scan.js +292 -0
- package/dist/analyzer/source.js +100 -0
- package/dist/analyzer/toolchain.js +27 -0
- package/dist/analyzer/types.js +141 -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 +95 -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 +62 -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 +149 -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 +23 -0
- package/dist/react/Registrar.js +12 -0
- package/dist/react/context.d.ts +24 -0
- package/dist/react/context.js +5 -0
- package/dist/react/useAgentTools.js +105 -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 +102 -4
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readObjectLiteral, staticBoolean, staticString } from "./source.js";
|
|
2
|
+
// src/analyzer/options.ts
|
|
3
|
+
const CONFIRM_POLICIES = [
|
|
4
|
+
"always",
|
|
5
|
+
"consequential",
|
|
6
|
+
"never"
|
|
7
|
+
];
|
|
8
|
+
function readToolOptions(node, callOffset) {
|
|
9
|
+
const issues = [];
|
|
10
|
+
if (node?.type !== "ObjectExpression") {
|
|
11
|
+
issues.push({
|
|
12
|
+
code: "AT005",
|
|
13
|
+
detail: "the options are not an object literal",
|
|
14
|
+
offset: node?.start ?? callOffset
|
|
15
|
+
});
|
|
16
|
+
return { issues };
|
|
17
|
+
}
|
|
18
|
+
const { properties, hasSpread } = readObjectLiteral(node);
|
|
19
|
+
const reading = { issues };
|
|
20
|
+
const description = properties.get("description");
|
|
21
|
+
const descriptionText = staticString(description);
|
|
22
|
+
if (descriptionText !== void 0) {
|
|
23
|
+
reading.description = descriptionText;
|
|
24
|
+
if (descriptionText.trim() === "") issues.push({
|
|
25
|
+
code: "AT003",
|
|
26
|
+
detail: "the description is empty",
|
|
27
|
+
offset: description?.start ?? node.start
|
|
28
|
+
});
|
|
29
|
+
} else if (description || hasSpread) issues.push({
|
|
30
|
+
code: "AT005",
|
|
31
|
+
detail: "the description is not a string literal",
|
|
32
|
+
offset: description?.start ?? node.start
|
|
33
|
+
});
|
|
34
|
+
else issues.push({
|
|
35
|
+
code: "AT003",
|
|
36
|
+
detail: "the description is missing",
|
|
37
|
+
offset: node.start
|
|
38
|
+
});
|
|
39
|
+
const name = properties.get("name");
|
|
40
|
+
if (name) {
|
|
41
|
+
const nameText = staticString(name);
|
|
42
|
+
if (nameText === void 0) issues.push({
|
|
43
|
+
code: "AT001",
|
|
44
|
+
detail: "the name is not a string literal",
|
|
45
|
+
offset: name.start
|
|
46
|
+
});
|
|
47
|
+
else reading.name = nameText;
|
|
48
|
+
}
|
|
49
|
+
const title = staticString(properties.get("title"));
|
|
50
|
+
if (title !== void 0) reading.title = title;
|
|
51
|
+
const readOnly = staticBoolean(properties.get("readOnly"));
|
|
52
|
+
if (readOnly !== void 0) reading.readOnly = readOnly;
|
|
53
|
+
const consequential = staticBoolean(properties.get("consequential"));
|
|
54
|
+
if (consequential !== void 0) reading.consequential = consequential;
|
|
55
|
+
const untrustedOutput = staticBoolean(properties.get("untrustedOutput"));
|
|
56
|
+
if (untrustedOutput !== void 0) reading.untrustedOutput = untrustedOutput;
|
|
57
|
+
const confirm = staticString(properties.get("confirm"));
|
|
58
|
+
if (confirm !== void 0 && CONFIRM_POLICIES.includes(confirm)) reading.confirm = confirm;
|
|
59
|
+
const input = properties.get("input");
|
|
60
|
+
if (input) reading.input = input;
|
|
61
|
+
const output = properties.get("output");
|
|
62
|
+
if (output) reading.output = output;
|
|
63
|
+
return reading;
|
|
64
|
+
}
|
|
65
|
+
export { readToolOptions };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { compareText } from "../core/compare.js";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, statSync } 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, cache = /* @__PURE__ */ new Map()) {
|
|
17
|
+
const appDirectory = findAppDirectory(cwd);
|
|
18
|
+
if (!appDirectory) {
|
|
19
|
+
cache.clear();
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const routes = /* @__PURE__ */ new Map();
|
|
23
|
+
const pages = /* @__PURE__ */ new Set();
|
|
24
|
+
visit(appDirectory, [], {
|
|
25
|
+
routes,
|
|
26
|
+
pages,
|
|
27
|
+
cache
|
|
28
|
+
});
|
|
29
|
+
for (const file of cache.keys()) if (!pages.has(file)) cache.delete(file);
|
|
30
|
+
return [...routes.values()].toSorted((left, right) => compareText(left.pattern, right.pattern));
|
|
31
|
+
}
|
|
32
|
+
function visit(directory, segments, scan) {
|
|
33
|
+
const entries = readdirSync(directory, { withFileTypes: true });
|
|
34
|
+
const page = entries.find((entry) => entry.isFile() && PAGE_FILE.test(entry.name));
|
|
35
|
+
if (page) {
|
|
36
|
+
const pageFile = join(directory, page.name);
|
|
37
|
+
scan.pages.add(pageFile);
|
|
38
|
+
const route = toRoute(segments, cachedPageMetadata(pageFile, scan.cache));
|
|
39
|
+
if (!scan.routes.has(route.pattern)) scan.routes.set(route.pattern, route);
|
|
40
|
+
}
|
|
41
|
+
for (const entry of entries) {
|
|
42
|
+
if (!entry.isDirectory() || isExcluded(entry.name, segments.length === 0)) continue;
|
|
43
|
+
visit(join(directory, entry.name), ROUTE_GROUP.test(entry.name) ? segments : [...segments, entry.name], scan);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function cachedPageMetadata(pageFile, cache) {
|
|
47
|
+
const { mtimeMs, size } = statSync(pageFile);
|
|
48
|
+
const stamp = `${mtimeMs}:${size}`;
|
|
49
|
+
let cached = cache.get(pageFile);
|
|
50
|
+
if (cached?.stamp !== stamp) {
|
|
51
|
+
cached = {
|
|
52
|
+
stamp,
|
|
53
|
+
metadata: readPageMetadata(pageFile)
|
|
54
|
+
};
|
|
55
|
+
cache.set(pageFile, cached);
|
|
56
|
+
}
|
|
57
|
+
return cached.metadata;
|
|
58
|
+
}
|
|
59
|
+
function isExcluded(name, isRoot) {
|
|
60
|
+
return name.startsWith("_") || name.startsWith("@") || INTERCEPTING_ROUTE.test(name) || name === "node_modules" || isRoot && name === "api";
|
|
61
|
+
}
|
|
62
|
+
function toRoute(segments, metadata) {
|
|
63
|
+
const route = {
|
|
64
|
+
pattern: `/${segments.join("/")}`,
|
|
65
|
+
params: []
|
|
66
|
+
};
|
|
67
|
+
for (const segment of segments) {
|
|
68
|
+
const optionalCatchAll = OPTIONAL_CATCH_ALL.exec(segment)?.[1];
|
|
69
|
+
const catchAll = CATCH_ALL.exec(segment)?.[1];
|
|
70
|
+
const dynamic = DYNAMIC.exec(segment)?.[1];
|
|
71
|
+
if (optionalCatchAll !== void 0) {
|
|
72
|
+
route.params.push(optionalCatchAll);
|
|
73
|
+
route.optionalCatchAll = optionalCatchAll;
|
|
74
|
+
} else if (catchAll !== void 0) {
|
|
75
|
+
route.params.push(catchAll);
|
|
76
|
+
route.catchAll = catchAll;
|
|
77
|
+
} else if (dynamic !== void 0) route.params.push(dynamic);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
...route,
|
|
81
|
+
...metadata
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function readPageMetadata(pageFile) {
|
|
85
|
+
if (pageFile.endsWith(".mdx")) return {};
|
|
86
|
+
const code = readFileSync(pageFile, "utf8");
|
|
87
|
+
if (!code.includes("generateStaticParams") && !code.includes("metadata")) return {};
|
|
88
|
+
const metadata = {};
|
|
89
|
+
for (const statement of parseModule(pageFile, code).program.body) {
|
|
90
|
+
if (statement.type !== "ExportNamedDeclaration" || !statement.declaration) continue;
|
|
91
|
+
const { declaration } = statement;
|
|
92
|
+
if (declaration.type === "FunctionDeclaration" && declaration.id?.name === "generateStaticParams") metadata.staticParams = true;
|
|
93
|
+
if (declaration.type !== "VariableDeclaration") continue;
|
|
94
|
+
for (const declarator of declaration.declarations) {
|
|
95
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
96
|
+
if (declarator.id.name === "generateStaticParams") metadata.staticParams = true;
|
|
97
|
+
if (declarator.id.name === "metadata" && declarator.init?.type === "ObjectExpression") {
|
|
98
|
+
const title = staticString(readObjectLiteral(declarator.init).properties.get("title"));
|
|
99
|
+
if (title) metadata.title = title;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return metadata;
|
|
104
|
+
}
|
|
105
|
+
export { findAppDirectory, scanRoutes };
|
|
@@ -0,0 +1,292 @@
|
|
|
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
|
+
const registered = /* @__PURE__ */ new Set();
|
|
93
|
+
for (const [localName, node] of context.declarations) {
|
|
94
|
+
if (!isAgentCall(context, node)) continue;
|
|
95
|
+
registered.add(node);
|
|
96
|
+
addAgentTool(context, node, context.exports.get(localName));
|
|
97
|
+
}
|
|
98
|
+
for (const statement of context.module.program.body) if (statement.type === "ExportDefaultDeclaration" && isAgentCall(context, statement.declaration)) {
|
|
99
|
+
registered.add(statement.declaration);
|
|
100
|
+
addAgentTool(context, statement.declaration, "default");
|
|
101
|
+
}
|
|
102
|
+
walk(context.module.program, (node) => {
|
|
103
|
+
if (!isAgentCall(context, node) || registered.has(node)) return;
|
|
104
|
+
context.scan.diagnostics.push(createDiagnostic("AT109", context.module.locate(node.start)));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function addAgentTool(context, call, exportName) {
|
|
108
|
+
const { module } = context;
|
|
109
|
+
const reading = readToolOptions(call.arguments[0], call.start);
|
|
110
|
+
const annotations = resolveAnnotations(reading.readOnly, reading.consequential, reading.untrustedOutput);
|
|
111
|
+
const tool = {
|
|
112
|
+
name: reading.name ?? exportName ?? "",
|
|
113
|
+
kind: "agent",
|
|
114
|
+
source: {
|
|
115
|
+
file: module.file,
|
|
116
|
+
export: exportName ?? "",
|
|
117
|
+
line: module.locate(call.start).line
|
|
118
|
+
},
|
|
119
|
+
description: reading.description ?? "",
|
|
120
|
+
schemaSource: reading.input ? "standard-schema" : "none",
|
|
121
|
+
annotations,
|
|
122
|
+
confirm: reading.confirm ?? "consequential",
|
|
123
|
+
validated: true,
|
|
124
|
+
diagnostics: [],
|
|
125
|
+
...reading.input ? { inputSource: module.text(reading.input) } : {}
|
|
126
|
+
};
|
|
127
|
+
const report = reporter(context, tool);
|
|
128
|
+
if (exportName === void 0) report("AT004", call.start, "the agent() call is not exported");
|
|
129
|
+
if (!context.useServer) report("AT004", call.start, "the file has no 'use server' directive");
|
|
130
|
+
if (exportName === "default" && reading.name === void 0) report("AT001", call.start, "a default export needs the `name` option");
|
|
131
|
+
for (const issue of reading.issues) report(issue.code, issue.offset, issue.detail);
|
|
132
|
+
checkDescription(tool, report, call.start);
|
|
133
|
+
if (tool.confirm === "never" && annotations.consequentialHint) report("AT103", call.start);
|
|
134
|
+
const handler = resolveFunction(context, call.arguments[1]);
|
|
135
|
+
if (handler) {
|
|
136
|
+
checkAuthentication(context, handler, report);
|
|
137
|
+
const input = resolveReference(context, reading.input);
|
|
138
|
+
if (input) checkPersonalFields(collectObjectKeys(input), handler, report, input.start);
|
|
139
|
+
}
|
|
140
|
+
const output = resolveReference(context, reading.output);
|
|
141
|
+
if (output && !annotations.untrustedContentHint && FREE_FORM_STRING.test(module.text(output))) report("AT106", output.start);
|
|
142
|
+
context.scan.tools.push(tool);
|
|
143
|
+
}
|
|
144
|
+
function scanAgentTags(context) {
|
|
145
|
+
for (const comment of context.module.comments) {
|
|
146
|
+
if (comment.type !== "Block" || !comment.value.startsWith("*")) continue;
|
|
147
|
+
const tag = parseAgentTag(comment.value);
|
|
148
|
+
if (tag) addJsdocTool(context, comment, tag);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function addJsdocTool(context, comment, tag) {
|
|
152
|
+
const { module } = context;
|
|
153
|
+
const target = documentedFunction(context, comment);
|
|
154
|
+
const exportName = target?.exportName;
|
|
155
|
+
const annotations = resolveAnnotations(tag.readOnly || void 0, tag.consequential || void 0, tag.untrustedOutput || void 0);
|
|
156
|
+
const tool = {
|
|
157
|
+
name: tag.name ?? (exportName === "default" ? target?.localName : exportName) ?? target?.localName ?? "",
|
|
158
|
+
kind: "jsdoc",
|
|
159
|
+
source: {
|
|
160
|
+
file: module.file,
|
|
161
|
+
export: exportName ?? "",
|
|
162
|
+
line: module.locate(target?.node.start ?? comment.start).line
|
|
163
|
+
},
|
|
164
|
+
description: tag.description,
|
|
165
|
+
schemaSource: "none",
|
|
166
|
+
annotations,
|
|
167
|
+
confirm: "consequential",
|
|
168
|
+
validated: false,
|
|
169
|
+
diagnostics: []
|
|
170
|
+
};
|
|
171
|
+
const report = reporter(context, tool);
|
|
172
|
+
const offset = target?.node.start ?? comment.start;
|
|
173
|
+
if (!target) report("AT004", offset, "@agent is not attached to a function");
|
|
174
|
+
else if (exportName === void 0) report("AT004", offset, "the function is not exported");
|
|
175
|
+
if (!context.useServer) report("AT004", offset, "the file has no 'use server' directive");
|
|
176
|
+
for (const modifier of tag.unknownModifiers) report("AT006", comment.start, `"${modifier}"`);
|
|
177
|
+
if (tag.description === "") report("AT003", comment.start, "the JSDoc comment has no description");
|
|
178
|
+
checkDescription(tool, report, comment.start);
|
|
179
|
+
if (target && exportName !== void 0) {
|
|
180
|
+
const { params } = target.node;
|
|
181
|
+
const [first] = params;
|
|
182
|
+
if (tag.formData || params.some(isFormDataParameter)) {
|
|
183
|
+
tool.adapter = formDataAdapter(params.length);
|
|
184
|
+
report("AT101", offset, "a FormData parameter has no inferable schema; use defineAgentTools() with an input schema");
|
|
185
|
+
} else if (first && !hasTypeAnnotation(first)) report("AT101", first.start, "the first parameter has no type annotation");
|
|
186
|
+
else if (first) {
|
|
187
|
+
tool.schemaSource = "typescript";
|
|
188
|
+
context.scan.typeRequests.push({
|
|
189
|
+
tool,
|
|
190
|
+
file: module.file,
|
|
191
|
+
parameterOffset: first.start
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (first && target.node.body) {
|
|
195
|
+
if (![...collectCalleeNames(target.node.body)].some((name) => VALIDATION_CALL.test(name))) report("AT102", offset);
|
|
196
|
+
}
|
|
197
|
+
checkAuthentication(context, target.node, report);
|
|
198
|
+
if (first) checkPersonalFields(collectObjectKeys(first), target.node, report, first.start);
|
|
199
|
+
}
|
|
200
|
+
context.scan.tools.push(tool);
|
|
201
|
+
}
|
|
202
|
+
function documentedFunction(context, comment) {
|
|
203
|
+
const { module } = context;
|
|
204
|
+
const statement = module.program.body.find((candidate) => candidate.start >= comment.end && module.code.slice(comment.end, candidate.start).trim() === "");
|
|
205
|
+
if (!statement) return void 0;
|
|
206
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
207
|
+
const node = asFunction(statement.declaration);
|
|
208
|
+
return node ? {
|
|
209
|
+
node,
|
|
210
|
+
localName: functionName(node),
|
|
211
|
+
exportName: "default"
|
|
212
|
+
} : void 0;
|
|
213
|
+
}
|
|
214
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
215
|
+
if (!declaration) return void 0;
|
|
216
|
+
const localName = declaredName(declaration);
|
|
217
|
+
const node = asFunction(declaration.type === "VariableDeclaration" ? declaration.declarations[0]?.init : declaration);
|
|
218
|
+
if (!node || localName === void 0) return void 0;
|
|
219
|
+
return {
|
|
220
|
+
node,
|
|
221
|
+
localName,
|
|
222
|
+
exportName: context.exports.get(localName)
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function declaredName(node) {
|
|
226
|
+
if (node.type === "FunctionDeclaration") return node.id?.name;
|
|
227
|
+
if (node.type !== "VariableDeclaration") return void 0;
|
|
228
|
+
const id = node.declarations[0]?.id;
|
|
229
|
+
return id?.type === "Identifier" ? id.name : void 0;
|
|
230
|
+
}
|
|
231
|
+
function functionName(node) {
|
|
232
|
+
return node.type === "ArrowFunctionExpression" ? void 0 : node.id?.name;
|
|
233
|
+
}
|
|
234
|
+
function asFunction(node) {
|
|
235
|
+
if (!node) return void 0;
|
|
236
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" ? node : void 0;
|
|
237
|
+
}
|
|
238
|
+
function resolveReference(context, node) {
|
|
239
|
+
return node?.type === "Identifier" ? context.declarations.get(node.name) : node;
|
|
240
|
+
}
|
|
241
|
+
function resolveFunction(context, node) {
|
|
242
|
+
return asFunction(resolveReference(context, node));
|
|
243
|
+
}
|
|
244
|
+
function isFormDataParameter(parameter) {
|
|
245
|
+
const annotation = typeAnnotationOf(parameter);
|
|
246
|
+
return annotation?.type === "TSTypeReference" && annotation.typeName.type === "Identifier" && annotation.typeName.name === "FormData";
|
|
247
|
+
}
|
|
248
|
+
function hasTypeAnnotation(parameter) {
|
|
249
|
+
return typeAnnotationOf(parameter) !== void 0;
|
|
250
|
+
}
|
|
251
|
+
function typeAnnotationOf(parameter) {
|
|
252
|
+
return (parameter.type === "AssignmentPattern" ? parameter.left : parameter).typeAnnotation?.typeAnnotation ?? void 0;
|
|
253
|
+
}
|
|
254
|
+
function reporter(context, tool) {
|
|
255
|
+
return (code, offset, detail) => {
|
|
256
|
+
context.scan.diagnostics.push(createDiagnostic(code, context.module.locate(offset), detail, tool.name || void 0));
|
|
257
|
+
if (!tool.diagnostics.includes(code)) tool.diagnostics.push(code);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function checkDescription(tool, report, offset) {
|
|
261
|
+
const length = tool.description.trim().length;
|
|
262
|
+
if (length > 0 && length < MIN_DESCRIPTION_LENGTH) report("AT108", offset, `"${tool.description.trim()}"`);
|
|
263
|
+
}
|
|
264
|
+
function checkAuthentication(context, handler, report) {
|
|
265
|
+
if (!handler.body) return;
|
|
266
|
+
const calls = collectCalleeNames(handler.body);
|
|
267
|
+
if (!context.options.auth.some((name) => calls.has(name))) report("AT105", handler.start);
|
|
268
|
+
}
|
|
269
|
+
function checkPersonalFields(keys, handler, report, offset) {
|
|
270
|
+
if (!handler.body) return;
|
|
271
|
+
const referenced = collectReferencedNames(handler.body);
|
|
272
|
+
const unused = [...keys].filter((key) => isPersonalField(key) && !referenced.has(key));
|
|
273
|
+
if (unused.length > 0) report("AT107", offset, unused.join(", "));
|
|
274
|
+
}
|
|
275
|
+
function isPersonalField(key) {
|
|
276
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1 $2").split(/[\s_-]+/).some((word) => PERSONAL_FIELDS.has(word.toLowerCase()));
|
|
277
|
+
}
|
|
278
|
+
function usesNavigate(context) {
|
|
279
|
+
if (context.componentNames.size === 0) return false;
|
|
280
|
+
let found = false;
|
|
281
|
+
walk(context.module.program, (node) => {
|
|
282
|
+
if (node.type !== "JSXOpeningElement") return;
|
|
283
|
+
if (node.name.type !== "JSXIdentifier" || !context.componentNames.has(node.name.name)) return;
|
|
284
|
+
for (const attribute of node.attributes) {
|
|
285
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name !== "navigate") continue;
|
|
286
|
+
const value = attribute.value;
|
|
287
|
+
if (!(value?.type === "JSXExpressionContainer" && value.expression.type === "Literal" && value.expression.value === false)) found = true;
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
return found;
|
|
291
|
+
}
|
|
292
|
+
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,27 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
// src/analyzer/toolchain.ts
|
|
4
|
+
const SCHEMA_GENERATOR_PACKAGE = "ts-json-schema-generator";
|
|
5
|
+
function toolchainOrigins(cwd) {
|
|
6
|
+
return [join(cwd, "package.json"), import.meta.url];
|
|
7
|
+
}
|
|
8
|
+
function loadTypeToolchain(origins, generatorPackage = SCHEMA_GENERATOR_PACKAGE) {
|
|
9
|
+
for (const origin of origins) {
|
|
10
|
+
const entry = resolveFrom(origin, generatorPackage);
|
|
11
|
+
if (entry === void 0) continue;
|
|
12
|
+
const requireFromGenerator = createRequire(entry);
|
|
13
|
+
return {
|
|
14
|
+
generator: requireFromGenerator(entry),
|
|
15
|
+
typescript: requireFromGenerator("typescript")
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function resolveFrom(origin, specifier) {
|
|
20
|
+
try {
|
|
21
|
+
return createRequire(origin).resolve(specifier);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error.code === "MODULE_NOT_FOUND") return void 0;
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export { SCHEMA_GENERATOR_PACKAGE, loadTypeToolchain, toolchainOrigins };
|