@pagaqr/mcp 0.0.0-stage → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +202 -0
- package/NOTICE +9 -0
- package/README.md +99 -2
- package/dist/capture-cli.d.ts +2 -0
- package/dist/capture-cli.js +34 -0
- package/dist/capture-local.d.ts +9 -0
- package/dist/capture-local.js +18 -0
- package/dist/capture-store.d.ts +7 -0
- package/dist/capture-store.js +175 -0
- package/dist/catalogue.d.ts +33 -0
- package/dist/catalogue.js +78 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +27 -0
- package/dist/diagnose.d.ts +23 -0
- package/dist/diagnose.js +93 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +134 -0
- package/dist/invocation-log.d.ts +9 -0
- package/dist/invocation-log.js +70 -0
- package/dist/masked-terminal.d.ts +4 -0
- package/dist/masked-terminal.js +71 -0
- package/dist/next-postgres.json +99 -0
- package/dist/sbom.cdx.json +523 -0
- package/dist/setup/database.d.ts +7 -0
- package/dist/setup/database.js +83 -0
- package/dist/setup/notice.d.ts +2 -0
- package/dist/setup/notice.js +2 -0
- package/dist/setup/run.d.ts +25 -0
- package/dist/setup/run.js +215 -0
- package/dist/setup/runtime.js +837 -0
- package/dist/setup/state.d.ts +7 -0
- package/dist/setup/state.js +81 -0
- package/dist/setup/step.d.ts +59 -0
- package/dist/setup/step.js +9 -0
- package/dist/setup/steps/pey.d.ts +8 -0
- package/dist/setup/steps/pey.js +271 -0
- package/dist/setup/steps/vercel.d.ts +5 -0
- package/dist/setup/steps/vercel.js +375 -0
- package/dist/setup/store-secrets.d.ts +3 -0
- package/dist/setup/store-secrets.js +67 -0
- package/dist/setup/terminal-guard.d.ts +9 -0
- package/dist/setup/terminal-guard.js +17 -0
- package/dist/setup/verify.d.ts +34 -0
- package/dist/setup/verify.js +144 -0
- package/dist/setup-cli.d.ts +2 -0
- package/dist/setup-cli.js +70 -0
- package/dist/template.d.ts +4 -0
- package/dist/template.js +119 -0
- package/dist/webhook.d.ts +1 -0
- package/dist/webhook.js +30 -0
- package/package.json +51 -3
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const firstParty = {
|
|
2
|
+
pagaqr: "0.7.0",
|
|
3
|
+
"@pagaqr/postgres": "0.7.0",
|
|
4
|
+
"@pagaqr/checkout-react": "0.7.0",
|
|
5
|
+
"@pagaqr/check": "0.7.0",
|
|
6
|
+
"@pagaqr/mcp": "0.7.1",
|
|
7
|
+
"@pagaqr/express": "0.7.0",
|
|
8
|
+
"@pagaqr/mysql": "0.7.0",
|
|
9
|
+
"@pagaqr/mongodb": "0.7.0",
|
|
10
|
+
"@pagaqr/dynamodb": "0.7.0",
|
|
11
|
+
};
|
|
12
|
+
export const catalogue = {
|
|
13
|
+
published: [],
|
|
14
|
+
preview: {
|
|
15
|
+
profile: "next-postgres",
|
|
16
|
+
status: "local_preparation",
|
|
17
|
+
checkout: "ready",
|
|
18
|
+
privacyNotice: "merchant_configuration_required",
|
|
19
|
+
node: "26.14.0",
|
|
20
|
+
dependencies: {
|
|
21
|
+
next: "16.5.0",
|
|
22
|
+
react: "19.3.1",
|
|
23
|
+
"react-dom": "19.3.1",
|
|
24
|
+
pg: "8.23.1",
|
|
25
|
+
"server-only": "0.0.1",
|
|
26
|
+
},
|
|
27
|
+
devDependencies: {
|
|
28
|
+
typescript: "6.0.3",
|
|
29
|
+
vitest: "5.1.2",
|
|
30
|
+
"@pagaqr/check": firstParty["@pagaqr/check"],
|
|
31
|
+
"@types/node": "26.6.11",
|
|
32
|
+
"@types/pg": "8.23.1",
|
|
33
|
+
"@types/react": "19.3.3",
|
|
34
|
+
"@types/react-dom": "19.3.0",
|
|
35
|
+
},
|
|
36
|
+
workspace: {
|
|
37
|
+
pagaqr: firstParty["pagaqr"],
|
|
38
|
+
"@pagaqr/postgres": firstParty["@pagaqr/postgres"],
|
|
39
|
+
"@pagaqr/checkout-react": firstParty["@pagaqr/checkout-react"],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
firstParty,
|
|
43
|
+
};
|
|
44
|
+
const SECTIONS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
|
|
45
|
+
function paymentName(name) {
|
|
46
|
+
return name.toLowerCase().includes("pagaqr");
|
|
47
|
+
}
|
|
48
|
+
function aliasDestination(spec) {
|
|
49
|
+
if (typeof spec !== "string")
|
|
50
|
+
return undefined;
|
|
51
|
+
const npmAlias = /^npm:((?:@[^/@]+\/)?[^@]+)(?:@|$)/.exec(spec)?.[1];
|
|
52
|
+
return npmAlias ?? /^workspace:((?:@[^/@]+\/)?[^@]+)@/.exec(spec)?.[1];
|
|
53
|
+
}
|
|
54
|
+
export function usesCatalogue(manifest, requireExactVersions = true) {
|
|
55
|
+
for (const section of SECTIONS) {
|
|
56
|
+
const group = manifest[section];
|
|
57
|
+
if (group === undefined)
|
|
58
|
+
continue;
|
|
59
|
+
if (group === null || typeof group !== "object" || Array.isArray(group))
|
|
60
|
+
return false;
|
|
61
|
+
for (const [name, spec] of Object.entries(group)) {
|
|
62
|
+
const alias = aliasDestination(spec);
|
|
63
|
+
if (!paymentName(name) && !(alias && paymentName(alias)))
|
|
64
|
+
continue;
|
|
65
|
+
const pin = firstParty[name];
|
|
66
|
+
if (pin === undefined || alias !== undefined)
|
|
67
|
+
return false;
|
|
68
|
+
if (requireExactVersions && spec !== pin && spec !== "workspace:" + pin)
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const bundles of [manifest["bundleDependencies"], manifest["bundledDependencies"]]) {
|
|
73
|
+
if (Array.isArray(bundles) &&
|
|
74
|
+
bundles.some((name) => typeof name === "string" && paymentName(name) && !Object.hasOwn(firstParty, name)))
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { argv, cwd, exit, stderr } from "node:process";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
|
5
|
+
import { createDiagnosticServer } from "./index.js";
|
|
6
|
+
async function start() {
|
|
7
|
+
const { values } = parseArgs({
|
|
8
|
+
args: argv.slice(2),
|
|
9
|
+
options: {
|
|
10
|
+
project: { type: "string" },
|
|
11
|
+
"webhook-url": { type: "string" },
|
|
12
|
+
install: { type: "boolean" },
|
|
13
|
+
"audit-log": { type: "boolean" },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
const server = createDiagnosticServer({
|
|
17
|
+
projectDirectory: values.project ?? cwd(),
|
|
18
|
+
install: values.install === true,
|
|
19
|
+
auditLog: values["audit-log"] === true,
|
|
20
|
+
...(values["webhook-url"] === undefined ? {} : { webhookUrl: values["webhook-url"] }),
|
|
21
|
+
});
|
|
22
|
+
await server.connect(new StdioServerTransport());
|
|
23
|
+
}
|
|
24
|
+
void start().catch(() => {
|
|
25
|
+
stderr.write("Invalid MCP launcher configuration.\n");
|
|
26
|
+
exit(2);
|
|
27
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare const MISSING: {
|
|
2
|
+
readonly status: "missing";
|
|
3
|
+
};
|
|
4
|
+
declare const UNAVAILABLE: {
|
|
5
|
+
readonly status: "unavailable";
|
|
6
|
+
};
|
|
7
|
+
type Data = Record<string, unknown>;
|
|
8
|
+
type Metadata = typeof MISSING | typeof UNAVAILABLE | {
|
|
9
|
+
status: "found";
|
|
10
|
+
manifest: Data;
|
|
11
|
+
};
|
|
12
|
+
export declare function readProjectManifest(root: string): Metadata;
|
|
13
|
+
declare function report(root: string): {
|
|
14
|
+
project: string;
|
|
15
|
+
packages: {
|
|
16
|
+
name: "pagaqr" | "@pagaqr/postgres" | "@pagaqr/checkout-react" | "@pagaqr/check" | "@pagaqr/express" | "@pagaqr/mysql" | "@pagaqr/mongodb" | "@pagaqr/dynamodb";
|
|
17
|
+
declaredIn: ("dependencies" | "devDependencies" | "peerDependencies" | "optionalDependencies")[];
|
|
18
|
+
installed: string;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
export declare function resolveProjectDirectory(directory: string): string | undefined;
|
|
22
|
+
export declare function diagnoseInstallation(root: string | undefined): () => ReturnType<typeof report>;
|
|
23
|
+
export {};
|
package/dist/diagnose.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, readSync, realpathSync } from "node:fs";
|
|
2
|
+
import { basename, isAbsolute, join, relative, sep } from "node:path";
|
|
3
|
+
const PACKAGES = [
|
|
4
|
+
"pagaqr",
|
|
5
|
+
"@pagaqr/postgres",
|
|
6
|
+
"@pagaqr/mysql",
|
|
7
|
+
"@pagaqr/mongodb",
|
|
8
|
+
"@pagaqr/dynamodb",
|
|
9
|
+
"@pagaqr/express",
|
|
10
|
+
"@pagaqr/checkout-react",
|
|
11
|
+
"@pagaqr/check",
|
|
12
|
+
];
|
|
13
|
+
const SECTIONS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
|
|
14
|
+
const MAX_MANIFEST_BYTES = 256 * 1024;
|
|
15
|
+
const MISSING = { status: "missing" };
|
|
16
|
+
const UNAVAILABLE = { status: "unavailable" };
|
|
17
|
+
function object(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
function inside(root, path) {
|
|
21
|
+
const fromRoot = relative(root, path);
|
|
22
|
+
return !!fromRoot && fromRoot !== ".." && !fromRoot.startsWith(".." + sep) && !isAbsolute(fromRoot);
|
|
23
|
+
}
|
|
24
|
+
function readManifest(path, boundary, allowPackageDirectoryLink) {
|
|
25
|
+
try {
|
|
26
|
+
const resolved = realpathSync.native(path);
|
|
27
|
+
if (!inside(boundary, resolved) || (!allowPackageDirectoryLink && resolved !== path))
|
|
28
|
+
return UNAVAILABLE;
|
|
29
|
+
if (basename(resolved) !== "package.json")
|
|
30
|
+
return UNAVAILABLE;
|
|
31
|
+
if (!lstatSync(resolved).isFile())
|
|
32
|
+
return UNAVAILABLE;
|
|
33
|
+
const flags = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);
|
|
34
|
+
const fd = openSync(resolved, flags);
|
|
35
|
+
try {
|
|
36
|
+
const info = fstatSync(fd);
|
|
37
|
+
if (!info.isFile() ||
|
|
38
|
+
(!allowPackageDirectoryLink && info.nlink !== 1) ||
|
|
39
|
+
info.size > MAX_MANIFEST_BYTES)
|
|
40
|
+
return UNAVAILABLE;
|
|
41
|
+
const buffer = Buffer.alloc(info.size + 1);
|
|
42
|
+
const size = readSync(fd, buffer, 0, buffer.length, 0);
|
|
43
|
+
if (size !== info.size)
|
|
44
|
+
return UNAVAILABLE;
|
|
45
|
+
const parsed = JSON.parse(buffer.toString("utf8", 0, size));
|
|
46
|
+
return object(parsed) ? { status: "found", manifest: parsed } : UNAVAILABLE;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
closeSync(fd);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT" ? MISSING : UNAVAILABLE;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function readProjectManifest(root) {
|
|
57
|
+
return readManifest(join(root, "package.json"), root, false);
|
|
58
|
+
}
|
|
59
|
+
function report(root) {
|
|
60
|
+
const project = readProjectManifest(root);
|
|
61
|
+
if (project.status !== "found")
|
|
62
|
+
return { project: project.status, packages: [] };
|
|
63
|
+
const modules = join(root, "node_modules");
|
|
64
|
+
const packages = PACKAGES.flatMap((name) => {
|
|
65
|
+
const declaredIn = SECTIONS.filter((section) => {
|
|
66
|
+
const group = project.manifest[section];
|
|
67
|
+
return object(group) && Object.hasOwn(group, name);
|
|
68
|
+
});
|
|
69
|
+
const installedManifest = readManifest(join(modules, name, "package.json"), modules, true);
|
|
70
|
+
const installed = installedManifest.status === "found"
|
|
71
|
+
? installedManifest.manifest["name"] === name &&
|
|
72
|
+
typeof installedManifest.manifest["version"] === "string"
|
|
73
|
+
? "present"
|
|
74
|
+
: "unavailable"
|
|
75
|
+
: installedManifest.status;
|
|
76
|
+
return name === "pagaqr" || declaredIn.length || installed === "present"
|
|
77
|
+
? [{ name, declaredIn, installed }]
|
|
78
|
+
: [];
|
|
79
|
+
});
|
|
80
|
+
return { project: "found", packages };
|
|
81
|
+
}
|
|
82
|
+
export function resolveProjectDirectory(directory) {
|
|
83
|
+
try {
|
|
84
|
+
// Resolve the given path directly, without requiring separate grants for its ancestors.
|
|
85
|
+
return realpathSync.native(directory);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function diagnoseInstallation(root) {
|
|
92
|
+
return () => (root === undefined ? { project: "unavailable", packages: [] } : report(root));
|
|
93
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/server";
|
|
2
|
+
interface LaunchOptions {
|
|
3
|
+
projectDirectory: string;
|
|
4
|
+
webhookUrl?: string;
|
|
5
|
+
install?: boolean;
|
|
6
|
+
auditLog?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function createDiagnosticServer(options: LaunchOptions): Server;
|
|
9
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { Server } from "@modelcontextprotocol/server";
|
|
3
|
+
import { bindInvocationLog } from "./invocation-log.js";
|
|
4
|
+
import { catalogue } from "./catalogue.js";
|
|
5
|
+
import { diagnoseInstallation, resolveProjectDirectory } from "./diagnose.js";
|
|
6
|
+
import { bindTemplate, TEMPLATE_DIRECTORY } from "./template.js";
|
|
7
|
+
import { canonicalWebhookUrl } from "./webhook.js";
|
|
8
|
+
import { SETUP_COMMAND, SETUP_NOTICE } from "./setup/notice.js";
|
|
9
|
+
const EMPTY_INPUT = { type: "object", properties: {}, additionalProperties: false };
|
|
10
|
+
const annotations = {
|
|
11
|
+
readOnlyHint: true,
|
|
12
|
+
destructiveHint: false,
|
|
13
|
+
idempotentHint: true,
|
|
14
|
+
openWorldHint: false,
|
|
15
|
+
};
|
|
16
|
+
const tools = [
|
|
17
|
+
{
|
|
18
|
+
name: "diagnose_installation",
|
|
19
|
+
description: "Reporta la declaración y presencia local de los paquetes oficiales de PagaQR.",
|
|
20
|
+
inputSchema: EMPTY_INPUT,
|
|
21
|
+
annotations,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "canonical_webhook_url",
|
|
25
|
+
description: "Muestra el endpoint público del webhook que el operador configuró al iniciar.",
|
|
26
|
+
inputSchema: EMPTY_INPUT,
|
|
27
|
+
annotations,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
const captureTool = {
|
|
31
|
+
name: "local_key_capture",
|
|
32
|
+
description: SETUP_NOTICE + " Comando para la persona: " + SETUP_COMMAND,
|
|
33
|
+
inputSchema: EMPTY_INPUT,
|
|
34
|
+
annotations,
|
|
35
|
+
};
|
|
36
|
+
const catalogueTool = {
|
|
37
|
+
name: "installation_catalog",
|
|
38
|
+
description: "Lista las combinaciones publicadas y el perfil de preparación local.",
|
|
39
|
+
inputSchema: EMPTY_INPUT,
|
|
40
|
+
annotations,
|
|
41
|
+
};
|
|
42
|
+
const templateTool = {
|
|
43
|
+
name: "apply_installation_template",
|
|
44
|
+
description: "Genera el scaffold local del perfil indicado en un directorio nuevo de la tienda.",
|
|
45
|
+
inputSchema: {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: { profile: { type: "string", enum: [catalogue.preview.profile] } },
|
|
48
|
+
required: ["profile"],
|
|
49
|
+
additionalProperties: false,
|
|
50
|
+
},
|
|
51
|
+
annotations: { ...annotations, readOnlyHint: false },
|
|
52
|
+
};
|
|
53
|
+
function data(result) {
|
|
54
|
+
return {
|
|
55
|
+
structuredContent: result,
|
|
56
|
+
content: [{ type: "text", text: JSON.stringify(result) }],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function packageVersion() {
|
|
60
|
+
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
61
|
+
if (!manifest.version)
|
|
62
|
+
throw new Error("Missing MCP package version");
|
|
63
|
+
return manifest.version;
|
|
64
|
+
}
|
|
65
|
+
export function createDiagnosticServer(options) {
|
|
66
|
+
const webhook = options.webhookUrl === undefined ? undefined : canonicalWebhookUrl(options.webhookUrl);
|
|
67
|
+
if (options.webhookUrl !== undefined && webhook === undefined)
|
|
68
|
+
throw new Error("Invalid webhook endpoint");
|
|
69
|
+
const root = resolveProjectDirectory(options.projectDirectory);
|
|
70
|
+
const diagnose = diagnoseInstallation(root);
|
|
71
|
+
const installerEnabled = options.install === true;
|
|
72
|
+
const applyTemplate = installerEnabled ? bindTemplate(root) : undefined;
|
|
73
|
+
const log = options.auditLog === true ? bindInvocationLog(root) : undefined;
|
|
74
|
+
const available = installerEnabled ? [...tools, captureTool, catalogueTool, templateTool] : tools;
|
|
75
|
+
const server = new Server({ name: "pagaqr-mcp", version: packageVersion() }, {
|
|
76
|
+
capabilities: { tools: {} },
|
|
77
|
+
instructions: SETUP_NOTICE + " Comando para la persona: " + SETUP_COMMAND,
|
|
78
|
+
});
|
|
79
|
+
server.setRequestHandler("tools/list", () => {
|
|
80
|
+
return { tools: available };
|
|
81
|
+
});
|
|
82
|
+
server.setRequestHandler("tools/call", ({ params }) => {
|
|
83
|
+
const invoke = () => {
|
|
84
|
+
if (applyTemplate && params.name === "apply_installation_template") {
|
|
85
|
+
const args = params.arguments ?? {};
|
|
86
|
+
if (Object.keys(args).length !== 1 || args["profile"] !== catalogue.preview.profile) {
|
|
87
|
+
return { ...data({ error: "invalid_profile" }), isError: true };
|
|
88
|
+
}
|
|
89
|
+
const status = applyTemplate();
|
|
90
|
+
if (status !== "prepared")
|
|
91
|
+
return { ...data({ error: status }), isError: true };
|
|
92
|
+
return data({
|
|
93
|
+
status,
|
|
94
|
+
directory: TEMPLATE_DIRECTORY,
|
|
95
|
+
checkout: catalogue.preview.checkout,
|
|
96
|
+
privacyNotice: catalogue.preview.privacyNotice,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (Object.keys(params.arguments ?? {}).length > 0) {
|
|
100
|
+
return { ...data({ error: "arguments_not_allowed" }), isError: true };
|
|
101
|
+
}
|
|
102
|
+
if (installerEnabled && params.name === "installation_catalog")
|
|
103
|
+
return data(catalogue);
|
|
104
|
+
if (installerEnabled && params.name === "local_key_capture") {
|
|
105
|
+
return data({
|
|
106
|
+
status: "terminal_required",
|
|
107
|
+
command: SETUP_COMMAND,
|
|
108
|
+
instruction: SETUP_NOTICE + " La persona ejecuta el comando en la raíz de la tienda.",
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
switch (params.name) {
|
|
112
|
+
case "diagnose_installation":
|
|
113
|
+
return data(diagnose());
|
|
114
|
+
case "canonical_webhook_url":
|
|
115
|
+
return data({ status: webhook ? "configured" : "not_configured", url: webhook ?? null });
|
|
116
|
+
default:
|
|
117
|
+
return { ...data({ error: "unknown_tool" }), isError: true };
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
if (!options.auditLog)
|
|
121
|
+
return invoke();
|
|
122
|
+
const tool = available.some(({ name }) => name === params.name) ? params.name : "unknown";
|
|
123
|
+
const recorded = log?.(tool, invoke);
|
|
124
|
+
if (!recorded)
|
|
125
|
+
return { ...data({ error: "audit_unavailable" }), isError: true };
|
|
126
|
+
if (recorded.complete)
|
|
127
|
+
return recorded.result;
|
|
128
|
+
return {
|
|
129
|
+
...data({ ...recorded.result.structuredContent, audit: "completion_unavailable" }),
|
|
130
|
+
...("isError" in recorded.result && recorded.result.isError ? { isError: true } : {}),
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
return server;
|
|
134
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type Result = {
|
|
2
|
+
content: unknown;
|
|
3
|
+
isError?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare function bindInvocationLog(root: string | undefined): (<T extends Result>(tool: string, invoke: () => T) => {
|
|
6
|
+
result: T;
|
|
7
|
+
complete: boolean;
|
|
8
|
+
} | undefined) | undefined;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, realpathSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
const LOG_NAME = ".pagaqr-invocations.jsonl";
|
|
5
|
+
const MAX_BYTES = 1024 * 1024;
|
|
6
|
+
export function bindInvocationLog(root) {
|
|
7
|
+
if (root === undefined)
|
|
8
|
+
return undefined;
|
|
9
|
+
const directory = lstatSync(root);
|
|
10
|
+
if (!directory.isDirectory())
|
|
11
|
+
return undefined;
|
|
12
|
+
const path = join(root, LOG_NAME);
|
|
13
|
+
return function log(tool, invoke) {
|
|
14
|
+
try {
|
|
15
|
+
const current = lstatSync(root);
|
|
16
|
+
if (!current.isDirectory() ||
|
|
17
|
+
current.dev !== directory.dev ||
|
|
18
|
+
current.ino !== directory.ino ||
|
|
19
|
+
realpathSync(root) !== root)
|
|
20
|
+
return undefined;
|
|
21
|
+
const before = lstatSync(path, { throwIfNoEntry: false });
|
|
22
|
+
if (before && (!before.isFile() || before.nlink !== 1 || realpathSync(path) !== path))
|
|
23
|
+
return undefined;
|
|
24
|
+
// Sync writes at open time. Node's permission model rejects a separate fsync on the descriptor.
|
|
25
|
+
const appendOnly = constants.O_WRONLY | constants.O_APPEND | constants.O_SYNC;
|
|
26
|
+
const file = openSync(path, before ? appendOnly : appendOnly | constants.O_CREAT | constants.O_EXCL, 0o600);
|
|
27
|
+
try {
|
|
28
|
+
const opened = fstatSync(file);
|
|
29
|
+
if (!opened.isFile() ||
|
|
30
|
+
opened.nlink !== 1 ||
|
|
31
|
+
opened.size + 512 > MAX_BYTES ||
|
|
32
|
+
(before && (before.dev !== opened.dev || before.ino !== opened.ino)))
|
|
33
|
+
return undefined;
|
|
34
|
+
const invocationId = randomUUID();
|
|
35
|
+
const append = (result) => {
|
|
36
|
+
const entry = { at: new Date().toISOString(), invocationId, tool, result };
|
|
37
|
+
writeFileSync(file, JSON.stringify(entry) + "\n");
|
|
38
|
+
};
|
|
39
|
+
append("started");
|
|
40
|
+
let result;
|
|
41
|
+
try {
|
|
42
|
+
result = invoke();
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
append("error");
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
append(result.isError ? "error" : "ok");
|
|
50
|
+
return { result, complete: true };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// The operation already took effect. Its response stays authoritative if the disk fails.
|
|
54
|
+
return { result, complete: false };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
closeSync(file);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// A close failure cannot change the outcome written or returned above.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ReadStream } from "node:tty";
|
|
2
|
+
export declare const MAX_SECRET_BYTES = 512;
|
|
3
|
+
export type MaskedTerminal = Pick<ReadStream, "isTTY" | "isRaw" | "setRawMode" | "on" | "off" | "pause" | "resume">;
|
|
4
|
+
export declare function readMasked(terminal: MaskedTerminal, prompt: () => void, textInput?: boolean): Promise<Buffer | undefined>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export const MAX_SECRET_BYTES = 512;
|
|
2
|
+
export function readMasked(terminal, prompt, textInput = false) {
|
|
3
|
+
return new Promise((resolve) => {
|
|
4
|
+
const buffer = Buffer.alloc(textInput ? 8192 : MAX_SECRET_BYTES);
|
|
5
|
+
const previousMode = terminal.isRaw;
|
|
6
|
+
let length = 0;
|
|
7
|
+
let invalid = false;
|
|
8
|
+
let finished = false;
|
|
9
|
+
function finish(value) {
|
|
10
|
+
if (finished)
|
|
11
|
+
return;
|
|
12
|
+
finished = true;
|
|
13
|
+
terminal.off("data", onData);
|
|
14
|
+
terminal.off("end", finishOnEnd);
|
|
15
|
+
terminal.off("error", finishOnEnd);
|
|
16
|
+
buffer.fill(0);
|
|
17
|
+
try {
|
|
18
|
+
terminal.pause();
|
|
19
|
+
terminal.setRawMode(previousMode);
|
|
20
|
+
resolve(value);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
value?.fill(0);
|
|
24
|
+
resolve(undefined);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function finishOnEnd() {
|
|
28
|
+
finish();
|
|
29
|
+
}
|
|
30
|
+
function onData(part) {
|
|
31
|
+
if (!Buffer.isBuffer(part))
|
|
32
|
+
return finish();
|
|
33
|
+
try {
|
|
34
|
+
for (const byte of part) {
|
|
35
|
+
if (byte === 3 || byte === 4 || byte === 26)
|
|
36
|
+
return finish();
|
|
37
|
+
if (byte === 10 || byte === 13)
|
|
38
|
+
return finish(invalid ? undefined : Buffer.from(buffer.subarray(0, length)));
|
|
39
|
+
if (invalid)
|
|
40
|
+
continue;
|
|
41
|
+
if (byte === 8 || byte === 127) {
|
|
42
|
+
if (length > 0)
|
|
43
|
+
buffer[--length] = 0;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (byte < 32 || (!textInput && byte > 126) || length === buffer.length) {
|
|
47
|
+
invalid = true;
|
|
48
|
+
buffer.fill(0);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
buffer[length++] = byte;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
part.fill(0);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
terminal.setRawMode(true);
|
|
60
|
+
terminal.on("data", onData);
|
|
61
|
+
terminal.on("end", finishOnEnd);
|
|
62
|
+
terminal.on("error", finishOnEnd);
|
|
63
|
+
prompt();
|
|
64
|
+
if (!finished)
|
|
65
|
+
terminal.resume();
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
finish();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|